You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/modules/ROOT/pages/optimize-pdf.adoc
+29-34
Original file line number
Diff line number
Diff line change
@@ -135,52 +135,47 @@ This command does not manipulate the images in any way.
135
135
It merely compresses the objects in the PDF and prunes any unreachable references.
136
136
But given how much waste Prawn leaves behind, this turns out to reduce the file size substantially.
137
137
138
-
You can hook this command directly into the converter by providing your own implementation of the `Optimizer` class.
139
-
Start by creating a Ruby file named [.path]_optimizer-hexapdf.rb_, then populate it with the following code:
138
+
To see all the options that `hexapdf optimize` offers, run:
139
+
140
+
$ hexapdf help optimize
141
+
142
+
For example, to make the source of the PDF a bit more readable (though less optimized), set the stream-related options to `preserve` (e.g., `--streams preserve` from the CLI or `options.streams = :preserve` from the API).
143
+
You can also disable page compression (e.g., `--no-compress-pages` from the CLI or `options.compress_pages = false` from the API).
144
+
145
+
hexapdf also allows you to add password protection to your PDF, if that's something you're interested in doing.
146
+
147
+
=== Define an optimizer
148
+
149
+
You can hook HexaPDF directly into the conversion process by providing your own implementation of the `Optimizer` class.
150
+
Start by creating a Ruby file named [.path]_optimizer-hexapdf.rb_ where you will define the optimizer.
# retry without page compression, which can sometimes fail
163
-
options.compress_pages = false
164
-
@optimize.execute path, path
165
-
nil
166
-
end
167
-
end
156
+
include::example$optimizer-hexapdf.rb[]
168
157
----
169
158
170
-
To activate your custom optimizer, load this file when invoking the `asciidoctor-pdf` using the `-r` flag and set the `optimize` attribute as well using the `-a` flag.
159
+
To activate your custom optimizerwhen using the `asciidoctor-pdf` command, load the optimizer code using the `-r` flag, then set both the `optimize` and `pdf-optimizer` attributes using the `-a` flag.
171
160
172
-
$ asciidoctor-pdf -r ./optimizer-hexapdf.rb -a optimize filename.adoc
161
+
$ asciidoctor-pdf -r ./optimizer-hexapdf.rb -a optimize -a pdf-optimizer=hexapdf filename.adoc
173
162
174
-
Now you can convert and optimize all in one go.
163
+
If you're calling Asciidoctor PDF using the API, you can pass in the optimizer class directly with the `:pdf_optimizer` option:
175
164
176
-
To see more options that `hexapdf optimize` offers, run:
165
+
[,ruby]
166
+
----
167
+
require 'asciidoctor/pdf'
168
+
require_relative 'optimizer-hexapdf'
177
169
178
-
$ hexapdf help optimize
170
+
Asciidoctor.convert_file 'filename.adoc',
171
+
safe: :safe,
172
+
attributes: 'optimize',
173
+
pdf_optimizer: OptimizerHexaPDF
174
+
----
179
175
180
-
For example, to make the source of the PDF a bit more readable (though less optimized), set the stream-related options to `preserve` (e.g., `--streams preserve` from the CLI or `options.streams = :preserve` from the API).
181
-
You can also disable page compression (e.g., `--no-compress-pages` from the CLI or `options.compress_pages = false` from the API).
176
+
TIP: When you pass the optimizer class directly to the API, the `register_for` call in the class declaration to self-register the class with a keyword is not required.
182
177
183
-
hexapdf also allows you to add password protection to your PDF, if that's something you're interested in doing.
178
+
You've now converted the input file to PDF and optimized it all in one go!
0 commit comments