asciidoctor-pdf 2.0.0.beta.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.adoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = Asciidoctor PDF: A native PDF converter for AsciiDoc
2
2
  Dan Allen <https://github.com/mojavelinux[@mojavelinux]>; Sarah White <https://github.com/graphitefriction[@graphitefriction]>
3
- v2.0.0.beta.1, 2022-05-04
3
+ v2.0.0, 2022-05-18
4
4
  // Settings:
5
5
  :experimental:
6
6
  :idprefix:
@@ -24,19 +24,15 @@ endif::[]
24
24
  :project-handle: asciidoctor-pdf
25
25
  // Variables:
26
26
  :release-line: 2.0.x
27
- :release-version: 2.0.0.beta.1
28
- // URIs:
29
- :url-asciidoctor: http://asciidoctor.org
30
- :url-gem: http://rubygems.org/gems/asciidoctor-pdf
27
+ :release-version: 2.0.0
28
+ // URLs:
29
+ :url-gem: https://rubygems.org/gems/asciidoctor-pdf
31
30
  :url-project: https://github.com/asciidoctor/asciidoctor-pdf
32
31
  :url-project-repo: {url-project}
33
32
  :url-project-issues: {url-project-repo}/issues
34
- :url-prawn: http://prawnpdf.org
35
- :url-prawn-gmagick: https://github.com/packetmonkey/prawn-gmagick
36
- :url-prawn-svg: https://github.com/mogest/prawn-svg
37
- :url-asciidoctor-mathematical: https://github.com/asciidoctor/asciidoctor-mathematical
38
- :url-rvm: http://rvm.io
39
- :url-graphicsmagick: http://www.graphicsmagick.org
33
+ :url-project-docs: https://docs.asciidoctor.org/pdf-converter/latest
34
+ :url-prawn: https://prawnpdf.org
35
+ :url-rvm: https://rvm.io
40
36
 
41
37
  ifdef::status[]
42
38
  image:https://img.shields.io/badge/zulip-join_chat-brightgreen.svg[project chat,link=https://asciidoctor.zulipchat.com/]
@@ -44,133 +40,62 @@ image:{url-project-repo}/workflows/CI/badge.svg[Build Status (GitHub Actions),li
44
40
  image:https://img.shields.io/gem/v/asciidoctor-pdf.svg[Latest Release, link={url-gem}]
45
41
  endif::[]
46
42
 
47
- Asciidoctor PDF is a native PDF converter for AsciiDoc.
43
+ Asciidoctor PDF is a native PDF converter for AsciiDoc that plugs into the `pdf` backend.
48
44
  It bypasses the requirement to generate an intermediary format such as DocBook, Apache FO, or LaTeX.
49
- Instead, you can use this extension to convert your documents directly from AsciiDoc to PDF.
50
- Its aim is to take the pain out of creating PDF documents from AsciiDoc.
45
+ Instead, you can use Asciidoctor PDF to convert your documents directly from AsciiDoc to PDF.
46
+ The aim of this library is to take the pain out of creating PDF documents from AsciiDoc.
51
47
 
52
- NOTE: You're viewing the documentation for the (unreleased) {release-line} release line.
53
- If you're looking for the documentation for another release line, please refer to one of the following branches: +
54
- {url-project-repo}/tree/v1.5.x#readme[1.5.x]
55
- -
56
- {url-project-repo}/tree/v1.6.x#readme[1.6.x]
48
+ [NOTE]
49
+ ====
50
+ The documentation for Asciidoctor PDF {release-line} is now available at {url-project-docs}/.
51
+
52
+ If you're looking for the documentation for Asciidoctor PDF 1.6, refer to the {url-project-repo}/tree/v1.6.x#readme[README] in the v1.6.x branch.
53
+ Asciidoctor PDF 1.6 is no longer being developed and will reach EOL later this year.
54
+ You are encouraged to migrate to Asciidoctor PDF 2 as soon as possible.
55
+ ====
57
56
 
58
57
  toc::[]
59
58
 
60
59
  == Overview
61
60
 
62
- {project-name} is an Asciidoctor extension that converts an AsciiDoc document directly to a PDF document.
63
- The style and layout of the PDF is controlled by a dedicated theme file.
64
- To the degree possible, this converter supports all the features of AsciiDoc that are supported by the built-in converters.
65
- However, there are <<known-limitations,certain limits>> imposed by the PDF format and the PDF library this extension uses.
61
+ Asciidoctor PDF converts an AsciiDoc document directly to a PDF document.
62
+ The style and layout of the PDF are controlled by a dedicated theme file.
63
+ To the degree possible, Asciidoctor PDF supports all the features of AsciiDoc that are supported by Asciidoctor.
64
+ It also provides {url-project-docs}/features/[PDF-specific features].
65
+ However, there are {url-project-docs}/features/#limitations[certain limits] imposed by the PDF format and the PDF library this extension uses.
66
66
 
67
- Under the covers, {project-name} uses the Prawn gem and its extensions (e.g., prawn-svg and prawn-table) to generate the PDF document.
67
+ Asciidoctor PDF uses the Prawn gem and Prawn's extensions, such as prawn-svg and prawn-table, to generate a PDF document.
68
68
  {url-prawn}[Prawn] is a general purpose PDF generator for Ruby that features high-level APIs for common needs like setting up the page and inserting images and low-level APIs for positioning and rendering text and graphics.
69
69
 
70
- Prawn takes the pain out of creating (basic) PDF documents.
71
- Likewise, {project-name} takes the pain out of creating PDF documents _directly from AsciiDoc_.
72
- Skip ahead to <<getting-started,Getting started>> to start putting {project-name} use.
73
- But don't miss the <<Highlights>> to get a preview of what's possible.
74
-
75
- == Highlights
76
-
77
- * Direct AsciiDoc to PDF conversion
78
- * <<docs/theming-guide.adoc#,Configuration-driven theme (style and layout)>>
79
- * Custom fonts (TTF or OTF)
80
- * Full SVG support (thanks to https://github.com/mogest/prawn-svg[prawn-svg])
81
- * PDF document outline (i.e., bookmarks)
82
- * Title page
83
- * Table of contents page(s)
84
- * Document metadata (title, authors, subject, keywords, etc.)
85
- * Configurable page size (e.g., A4, Letter, Legal, etc)
86
- * Internal cross reference links
87
- * Syntax highlighting with Rouge (preferred), Pygments, or CodeRay
88
- * Cover pages
89
- * Page background color or page background image with named scaling
90
- * Page numbering
91
- * Double-sided (aka prepress) printing mode (i.e., margins alternate on recto and verso pages)
92
- * Customizable running content (header and footer)
93
- * “Keep together” blocks (i.e., page breaks avoided in certain block content):
94
- ** Explicitly delimited blocks other than open blocks
95
- ** Open blocks with the "unbreakable" option `[%unbreakable]`
96
- * Orphaned section titles avoided
97
- * Autofit verbatim blocks (as permitted by base_font_size_min setting)
98
- * Table border settings honored
99
- * Font-based icons
100
- * Auto-generated index
101
- * Automatic hyphenation (when enabled)
102
- * Permissive line breaking for CJK languages
103
- * Compression / optimization of output file
104
-
105
- == Known Limitations
106
-
107
- * Footnotes are always displayed as endnotes (at the end of chapter for books; at the end of document for all other doctypes).
108
- *Footnotes cannot be displayed at the bottom of the page because the PDF generator does not support content reflows* (see {url-project-issues}/85#issuecomment-577412975[#85] for reasoning).
109
- * Table cells that exceed the height of a single page will be truncated (see https://github.com/prawnpdf/prawn-table/issues/41[prawn-table#41]).
110
- * Columns cannot be assigned a 0% width (or a width less than the width of a single character); in the same vein, a column cannot be set to autowidth if width of all other columns meets or exceeds 100%; the result is that the converter with throw a Prawn::Errors::CannotFit error.
111
- * An inline image in a table cell will not force the column wider if the width of the image exceeds the width of the column; either reduce the image width using `pdfwidth`, increase the width of the column using `cols`, or convert the cell to an AsciiDoc table cell and, preferably, use a block image (see {url-project-issues}/830).
112
- * Must use development version of prawn for error to include font name when requested font style is missing.
113
- * AsciiDoc table cell leaves padding below last block (due to lack of margin collapsing).
114
- * Prawn does not support double-wide box drawing glyphs correctly, so box drawings aren't aligned properly in verbatim blocks (see https://github.com/prawnpdf/prawn/issues/1002[prawn#1002]).
115
- * Orphan / widow support is limited; a page break can occur between a section title and its section content, a table caption and the caption, etc.; use a manual page break to avoid.
116
- * If a no-break hyphen is surrounded by formatted text on both sides (or is formatted individually), it will not prevent a line break.
117
- * Images cannot float.
118
- * You cannot use inline HTML (like a link or emphasized text) in a source block that also uses syntax highlighting.
119
- These two technologies just don't combine in the PDF generation process due to how the syntax highlighters work.
120
- * Verse blocks do not use a fixed-width font by default, but you can control this setting using the theme.
121
- * An inline image with a percentage width value in an autowidth table cell is resized relative to its intrinsic width.
122
- The space reserved for the image matches its intrinsic width.
123
- This matches the behavior of HTML.
70
+ TIP: For the latest Asciidoctor PDF features and fixes, see {url-project-docs}/whats-new/[What's New in Asciidoctor PDF].
124
71
 
125
72
  == Prerequisites
126
73
 
127
- All that's needed is Ruby 2.5 or better (or JRuby 9.2 or better) and a few Ruby gems (including at least Asciidoctor 2.0.0), which we explain how to install in the next section.
128
-
129
- To check if you have Ruby available, use the `ruby` command to query the version installed:
130
-
131
- $ ruby -e 'puts RUBY_VERSION'
132
-
133
- Make sure this command reports a Ruby version that's at least 2.5.
134
- If so, you may proceed.
135
-
136
- If you want to <<Enable Stream Compression,optimize the pdf>> using rghost or hexapdf, you'll need to install the rghost gem:
137
-
138
- $ gem install rghost
139
-
140
- or the hexapdf gem:
141
-
142
- $ gem install hexapdf
143
-
144
- Similarly, if you want to use the hyphen option you will need to install the `text-hyphen` gem:
145
-
146
- $ gem install text-hyphen
147
-
148
- You'll also need to <<Install a Syntax Highlighter (optional),install a syntax highlighter>> to use source highlighting.
74
+ Asciidoctor PDF is a Ruby application.
75
+ Therefore, to use it, you'll need a Ruby runtime.
149
76
 
150
- === System Encoding
77
+ The supported Ruby runtimes are Ruby 2.7 or greater and JRuby 9.2 or greater.
78
+ However, we always recommend using the most recent release of Ruby or JRuby.
79
+ All required libraries (i.e., gems) will be installed automatically when you install Asciidoctor PDF, which will be covered in the <<Install Asciidoctor PDF,next section>>.
151
80
 
152
- Asciidoctor assumes you're using UTF-8 encoding.
153
- To minimize encoding problems, make sure the default encoding of your system is set to UTF-8.
81
+ To check if you have Ruby available, run the `ruby` command to print the installed version:
154
82
 
155
- If you're using a non-English Windows environment, the default encoding of your system may not be UTF-8.
156
- As a result, you may get an `Encoding::UndefinedConversionError` or other encoding issues when invoking Asciidoctor.
157
- To solve these problems, we recommend at least changing the active code page in your console to UTF-8.
83
+ $ ruby -v
158
84
 
159
- chcp 65001
85
+ Make sure this command reports a Ruby version that starts with 2.7 (or a JRuby version that starts with 9.2).
86
+ If so, you're ready to proceed.
87
+ If not, head over to {url-rvm}[rvm.io^] to get RVM and use it to install Ruby.
160
88
 
161
- Once you make this change, all your Unicode headaches will be behind you.
89
+ == Install Asciidoctor PDF
162
90
 
163
- == Getting Started
91
+ You can install Asciidoctor PDF using the `gem install` command.
92
+ We'll use this command to install the Asciidoctor PDF gem named *asciidoctor-pdf* that's published on RubyGems.org.
93
+ Pass the name of the gem to the `gem install` command as follows:
164
94
 
165
- You can get {project-name} by <<install-the-published-gem,installing the published gem>>.
166
- ifndef::env-site[You can also <<development,run the code from source>> if you want to use a development version or participate in development.]
95
+ $ gem install asciidoctor-pdf
167
96
 
168
- === Install the Published Gem
169
-
170
- To install {project-name}, first make sure you have satisfied the <<Prerequisites,prerequisites>>.
171
- Then, install the gem from RubyGems.org using the following command:
172
-
173
- $ gem install asciidoctor-pdf --pre
97
+ Installing Asciidoctor PDF will install a number of other gems mentioned in these docs, including asciidoctor, prawn, prawn-svg, prawn-table, prawn-icon, and ttfunk.
98
+ For the most part, the versions of these dependencies are locked to the version of Asciidoctor PDF.
174
99
 
175
100
  If you're using Ruby 3.1 or better, you must also install the matrix gem until Prawn 2.5.0 or better is released.
176
101
 
@@ -179,90 +104,42 @@ If you're using Ruby 3.1 or better, you must also install the matrix gem until P
179
104
  The matrix gem used to be bundled in the Ruby distribution, but was split out starting in Ruby 3.1.
180
105
  This requirement will be lifted once Prawn declares it as a runtime dependency.
181
106
 
182
- ==== Installation Troubleshooting
183
-
184
- If you get a permission error while installing the gem, such as the one below, it's likely you're attempting to install the gem directly into your system.
185
- Installing gems for tech writing directly into your system is not recommended.
186
-
187
- .Permission error when attempting to install as a system gem
188
- ....
189
- ERROR: While executing gem ... (Gem::FilePermissionError)
190
- You don't have write permissions for the /Library/Ruby/Gems/2.x.x directory.
191
- ....
192
-
193
- A better practice (and one that will ensure your sanity) is to ignore any version of Ruby installed on your system and use {url-rvm}[RVM] to manage the Ruby installation instead.
194
- The benefit of this approach is that a) Ruby is guaranteed to be set up correctly, b) installing gems will in no way interfere with the operation of your system, and c) any bin scripts provided by the installed gems will be available on your PATH.
195
- All files are managed in user space (aka your home or user directory).
196
- If something gets messed up, you can simply remove the [.path]_$HOME/.rvm_ folder and start over.
197
-
198
- To learn how to install RVM, follow the https://asciidoctor.org/docs/install-asciidoctor-macos/#rvm-procedure-recommended[RVM installation procedure] covered in the Asciidoctor documentation.
199
- Once you have installed RVM and used it to install Ruby, make sure to activate the Ruby managed by RVM using `rvm use default` or a specific Ruby version like `rvm use 2.7`.
200
- (You'll need to do this each time you open a new terminal).
201
-
202
- After installing the gem, you can see where it was installed using the following command:
203
-
204
- $ gem which asciidoctor-pdf
107
+ For further installation information, see {url-project-docs}/install/[the installation documentation].
108
+ For troubleshooting help, see {url-project-docs}/install/#installation-troubleshooting[Installation troubleshooting].
205
109
 
206
- To see where the bin script is located, use this command:
110
+ === Install a prerelease or development version
207
111
 
208
- $ command -v asciidoctor-pdf
112
+ To install the latest prerelease of the *asciidoctor-pdf* gem from RubyGems.org (if a prerelease is available), use the following command:
209
113
 
210
- Both paths should be underneath the [.path]_$HOME/.rvm_ directory.
211
- If not, check your RVM setup.
212
-
213
- ==== Install a Syntax Highlighter (optional)
214
-
215
- If you want to syntax highlight source listings, you'll also want to install Rouge, Pygments, or CodeRay.
216
- Choose one (or more) of the following:
217
-
218
- .Rouge (preferred, minimum version: 2.0.0)
219
- $ gem install rouge
220
-
221
- .Pygments
222
- $ gem install pygments.rb
223
-
224
- .CodeRay
225
- $ gem install coderay
226
-
227
- You then activate syntax highlighting for a given document by adding the `source-highlighter` attribute to the document header (Rouge shown):
228
-
229
- [source,asciidoc]
230
- ----
231
- :source-highlighter: rouge
232
- ----
233
-
234
- [#use-dev-versions]
235
- ==== Upgrade prawn
114
+ $ gem install asciidoctor-pdf --pre
236
115
 
237
- {project-name} uses Prawn to handle the PDF generation.
238
- At times, there may be development features or fixes you need in Prawn or one of its extensions that's still unreleased.
239
- No problem.
240
- You can gain access to these features by installing the unreleased gems directly from GitHub.
116
+ You can also {url-project-repo}/blob/main/CONTRIBUTING-CODE.adoc[run the code from source] if you want to use a development version or participate in development.
241
117
 
242
- To get started, first create a [.path]_Gemfile_ in the root of your project.
243
- In that file, declare the gem source, the {project-handle} gem, and the prawn gem (plus any other development gems you want to use).
118
+ == Optional dependencies
244
119
 
245
- .Gemfile
246
- [source,ruby]
247
- ----
248
- source 'https://rubygems.org'
120
+ There are several optional features of this converter that require additional gems to be installed.
121
+ Those features are as follows.
249
122
 
250
- gem 'asciidoctor-pdf'
251
- gem 'prawn', github: 'prawnpdf/prawn', branch: 'HEAD'
252
- ----
123
+ Source highlighting::
124
+ You'll need to {url-project-repo}/syntax-highlighting/[install a syntax highlighter] to use source highlighting (build-time only).
253
125
 
254
- You can then install the gems into your project using the `bundle` command:
126
+ PDF optimization::
127
+ If you want to optimize your PDF, you'll need rghost or hexapdf.
128
+ See {url-project-repo}/optimize-pdf/[Optimize the PDF] for installation and usage instructions.
255
129
 
256
- $ bundle config set --local path .bundle/gems && bundle
130
+ Automatic hyphenation::
131
+ To turn on automatic hyphenation using the `hyphens` attribute, you'll need to install the `text-hyphen` gem:
257
132
 
258
- Since you're using Bundler to manage the gems, you'll need to prefix all commands with `bundle exec`.
259
- For example:
133
+ $ gem install text-hyphen
260
134
 
261
- $ bundle exec asciidoctor-pdf -v
135
+ Accelerated image decoding::
136
+ Ruby is not particularly fast at decoding images, and the image formats it supports are limited.
137
+ To help, you can install prawn-gmagick, which delegates the work of decoding images to GraphicsMagick.
138
+ Refer to {url-project-repo}/image-paths-and-formats/#other-image-formats[Supporting additional image file formats] for instructions about how to enable this integration.
262
139
 
263
- Thus, to any command present in the following sections, be sure to include this prefix.
140
+ Check the {url-project-docs}/install/#table-minimum-version[minimum supported version table] to make sure you're using a supported version of the dependency.
264
141
 
265
- === Run the Application
142
+ == Run the Application
266
143
 
267
144
  Assuming all the required gems install properly, verify you can run the `asciidoctor-pdf` script:
268
145
 
@@ -272,9 +149,12 @@ If you see the version of {project-name} printed, you're ready to use {project-n
272
149
 
273
150
  Let's grab an AsciiDoc document to distill and start putting {project-name} to use.
274
151
 
275
- === An Example AsciiDoc Document
152
+ If you don't already have an AsciiDoc document to work with, you can use the <<examples/basic-example.adoc#,basic-example.adoc>> file found in the _examples_ directory of this project.
153
+ Copy it to the current directory as follows:
276
154
 
277
- If you don't already have an AsciiDoc document, you can use the <<examples/basic-example.adoc#,basic-example.adoc>> file found in the examples directory of this project.
155
+ $ cp examples/basic-example.adoc .
156
+
157
+ Let's take a look at the contents of that file.
278
158
 
279
159
  ifeval::[{safe-mode-level} >= 20]
280
160
  See <<examples/basic-example.adoc#,basic-example.adoc>>.
@@ -293,1047 +173,30 @@ It's time to convert the AsciiDoc document directly to PDF.
293
173
 
294
174
  IMPORTANT: You'll need the `rouge` gem installed to run this example since it uses the `source-highlighter` attribute with the value of `rouge`.
295
175
 
296
- Converting to PDF is as simple as running the `asciidoctor-pdf` script using Ruby and passing our AsciiDoc document as the first argument.
176
+ Converting to PDF is as straightforward as running the `asciidoctor-pdf` script using Ruby and passing the AsciiDoc document as the first argument:
297
177
 
298
178
  $ asciidoctor-pdf basic-example.adoc
299
179
 
300
- This command is just a shorthand way of running:
180
+ This command is a shorter way of running `asciidoctor` with the PDF converter and backend enabled:
301
181
 
302
182
  $ asciidoctor -r asciidoctor-pdf -b pdf basic-example.adoc
303
183
 
304
- The `asciidoctor-pdf` command just saves you from having to remember all those flags.
305
- That's why we created it.
184
+ The `asciidoctor-pdf` command saves you from having to remember these low-level options.
185
+ That's why we provide it.
306
186
 
307
- When the script completes, you should see the file [.path]_basic-example.pdf_ in the same directory.
187
+ When the script completes, you should see the file [.path]_basic-example.pdf_ in the current directory.
188
+ Asciidoctor creates the output file in the same directory as the input file by default.
308
189
  Open the [.path]_basic-example.pdf_ file with a PDF viewer to see the result.
309
190
 
310
191
  .Example PDF document rendered in a PDF viewer
311
- image::examples/example-pdf-screenshot.png[Screenshot of PDF document,width=850,467,scaledwidth=100%]
312
-
313
- ifndef::env-site[]
314
- You're also encouraged to try converting this <<README.adoc#,README>> as well as the documents in the examples directory to see more of what {project-name} can do.
315
- endif::[]
192
+ image::docs/modules/ROOT/images/basic-example-pdf-screenshot.png[Screenshot of PDF document,960,540,pdfwidth=100%]
316
193
 
317
- The pain of the DocBook toolchain should be melting away about now.
194
+ For more information about how to use Asciidoctor PDF and PDF-specific AsciiDoc syntax, see the {url-project-docs}/[Asciidoctor PDF documentation].
318
195
 
319
196
  == Themes
320
197
 
321
198
  The layout and styling of the PDF is driven by a YAML configuration file.
322
- To learn how the theming system works and how to create and apply custom themes, refer to the <<docs/theming-guide.adoc#,Asciidoctor PDF Theming Guide>>.
323
- You can use the built-in theme files, which you can find in the [.path]_data/themes_ directory, as examples.
324
-
325
- If you've enabled a source highlighter, you can control the style (aka theme) it applies to source blocks using the `coderay-style`, `pygments-style`, and `rouge-style` attributes, respectively.
326
- For example, to configure Rouge to use the built-in monokai theme, run Asciidoctor PDF as follows:
327
-
328
- $ asciidoctor-pdf -a rouge-style=monokai basic-example.adoc
329
-
330
- It's possible to develop your own theme for Rouge.
331
- Refer to the <<docs/theming-guide.adoc#,Asciidoctor PDF Theming Guide>> for details.
332
-
333
- == Support for Non-Latin Languages
334
-
335
- Asciidoctor can process the full range of characters in the UTF-8 character set.
336
- That means you can write your document in any language, save the file with UTF-8 encoding (_that's important!_), and expect Asciidoctor to convert the text properly.
337
- But you still need a font that provides the glyphs for those characters.
338
-
339
- When converting a document with Asciidoctor PDF, you may notice that some glyphs for certain languages, such as Chinese, are missing from the PDF.
340
- PDF is a "`bring your own font`" kind of system.
341
- In other words, the font you provide must provide glyphs for all the characters used.
342
- There's no one font that supports all the world's languages (though some, like Noto Serif, certainly come close).
343
- Even if there were such a font, bundling that font with the main gem would make it enormous.
344
- It would also severely limit the style choices in the default theme, which targets Latin-based languages.
345
- Therefore, we're taking the strategy of creating separate dedicated theme gems that target each language family, such as CJK.
346
- Read on to find out how to use these themes.
347
-
348
- Asciidoctor PDF provides a built-in theme that provides a broad range of characters in the CJK charsets, so you can start with that theme:
349
-
350
- $ asciidoctor-pdf -a scripts=cjk -a pdf-theme=default-with-fallback-font document.adoc
351
-
352
- Notice the `-a scripts=cjk` option.
353
- That's important.
354
- It tells the converter to insert break opportunities between CJK characters so that the line wraps properly when mixing English and a CJK language like Japanese.
355
-
356
- If the built-in theme with the fallback font doesn't go far enough, you'll need to use a theme that is optimized for CJK text.
357
- You can get such a theme by installing the `asciidoctor-pdf-cjk-kai_gen_gothic` gem.
358
- The https://github.com/chloerei/asciidoctor-pdf-cjk-kai_gen_gothic[asciidoctor-pdf-cjk-kai_gen_gothic] project provides themes optimized for CJK languages based on the kai_gen_gothic font.
359
- See the https://github.com/chloerei/asciidoctor-pdf-cjk-kai_gen_gothic[asciidoctor-pdf-cjk-kai_gen_gothic] project README for detailed setup instructions.
360
-
361
- WARNING: The theme provided by the `asciidoctor-pdf-cjk-kai_gen_gothic` gem is no longer compatiable with the stable release of Asciidoctor PDF.
362
- However, you can still use it to create a <<Create a Custom CJK Theme,custom CJK theme>>.
363
-
364
- Once you have that gem installed (and the fonts), you need to tell Asciidoctor PDF to use one of the themes.
365
- If you're converting a document that is primarily written in Japanese, you'd run Asciidoctor PDF as follows:
366
-
367
- asciidoctor-pdf -r asciidoctor-pdf-cjk-kai_gen_gothic -a pdf-theme=KaiGenGothicJP document.adoc
368
-
369
- If that command fails, you may have better luck creating your own CJK theme.
370
-
371
- === Create a Custom CJK Theme
372
-
373
- You also have to option of creating your own theme gem that uses fonts of your choice.
374
- For example, if you want to use the `asciidoctor-pdf-cjk-kai_gen_gothic` gem to fetch fonts, but then use them in your own theme, here's how you'd do it.
375
-
376
- . Install the `asciidoctor-pdf-cjk-kai_gen_gothic` gem:
377
-
378
- $ gem install asciidoctor-pdf-cjk-kai_gen_gothic
379
-
380
- . Download / install the fonts:
381
-
382
- $ asciidoctor-pdf-cjk-kai_gen_gothic-install
383
-
384
- . Create a theme file named [.path]_cjk-theme.yml_ that extends the default theme to override the fonts:
385
- +
386
- [source,yml]
387
- ----
388
- extends: default
389
- font:
390
- catalog:
391
- merge: true
392
- KaiGen Gothic CN:
393
- normal: KaiGenGothicCN-Regular.ttf
394
- bold: KaiGenGothicCN-Bold.ttf
395
- italic: KaiGenGothicCN-Regular-Italic.ttf
396
- bold_italic: KaiGenGothicCN-Bold-Italic.ttf
397
- fallbacks:
398
- - KaiGen Gothic CN
399
- base:
400
- font-family: KaiGen Gothic CN
401
- heading:
402
- font-family: $base-font-family
403
- abstract:
404
- title:
405
- font-family: $heading-font-family
406
- sidebar:
407
- title:
408
- font-family: $heading-font-family
409
- ----
410
-
411
- . Load your theme when running Asciidoctor PDF:
412
-
413
- $ asciidoctor-pdf -a scripts=cjk -a pdf-theme=./cjk-theme.yml -a pdf-fontsdir=GEM_FONTS_DIR,`ruby -r asciidoctor-pdf-cjk-kai_gen_gothic -e "print File.expand_path '../fonts', (Gem.datadir 'asciidoctor-pdf-cjk-kai_gen_gothic')"` document.adoc
414
-
415
- The `-a pdf-fontsdir` option is important to tell Asciidoctor PDF where to find your custom fonts.
416
- (Note that the inclusion of GEM_FONTS_DIR in the value is only required when using Asciidoctor PDF 1.5).
417
-
418
- Rather than using the installer from the `asciidoctor-pdf-cjk-kai_gen_gothic` gem, you can download fonts whatever way you choose.
419
- When using your own fonts, be sure to consult the <<docs/theming-guide.adoc#preparing-a-custom-font,Preparing a Custom Font>> section of the theming guide to find recommended modifications.
420
-
421
- == Font-Based Icons
422
-
423
- You can use icons in your PDF document using any of the following icon sets:
424
-
425
- * *fa* - https://fontawesome.com/v4.7.0/icons (default)
426
- * *fas* - https://fontawesome.com/icons?d=gallery&s=solid[Font Awesome - Solid^]
427
- * *fab* - https://fontawesome.com/icons?d=gallery&s=brands[Font Awesome - Brands^]
428
- * *far* - https://fontawesome.com/icons?d=gallery&s=regular[Font Awesome - Regular^]
429
- * *fi* - http://zurb.com/playground/foundation-icon-fonts-3[Foundation Icons^]
430
- * *pf* - https://paymentfont.com/[Payment font^]
431
-
432
- The fa icon set is deprecated.
433
- Please use one of the other three FontAwesome icon sets.
434
-
435
- You can enable font-based icons by setting the following attribute in the header of your document:
436
-
437
- [source,asciidoc]
438
- ----
439
- :icons: font
440
- ----
441
-
442
- If you want to override the font set globally, also set the `icon-set` attribute:
443
-
444
- [source,asciidoc]
445
- ----
446
- :icons: font
447
- :icon-set: pf
448
- ----
449
-
450
- Here's an example that shows how to use the Amazon icon from the payment font (pf) icon set in a sentence (assuming the `icon-set` is set to `pf):
451
-
452
- [source,asciidoc]
453
- ----
454
- Available now at icon:amazon[].
455
- ----
456
-
457
- You can use the `set` attribute on the icon macro to override the icon set for a given icon.
458
-
459
- [source,asciidoc]
460
- ----
461
- Available now at icon:amazon[set=pf].
462
- ----
463
-
464
- You can also specify the font set using the following shorthand.
465
-
466
- [source,asciidoc]
467
- ----
468
- Available now at icon:amazon@pf[].
469
- ----
470
-
471
- In addition to the sizes supported in the HTML backend (lg, 1x, 2x, etc), you can enter any relative value in the size attribute (e.g., 1.5em, 150%, etc).
472
-
473
- [source,asciidoc]
474
- ----
475
- icon:android[size=40em]
476
- ----
477
-
478
- You can enable use of fonts during PDF generation (instead of in the document header) by passing the `icons` attribute to the `asciidoctor-pdf` command.
479
-
480
- $ asciidoctor-pdf -a icons=font -a icon-set=pf sample.adoc
481
-
482
- Icon-based fonts are handled by the `prawn-icon` gem.
483
- To find a complete list of available icons, consult the https://github.com/jessedoyle/prawn-icon/tree/master/data/fonts[prawn-icon] repository.
484
-
485
- == Image Paths
486
-
487
- Images are resolved at the time the converter runs.
488
- That means they need to be located where the converter can find them.
489
-
490
- Relative images paths in the document are resolved relative to the value of the `imagesdir` attribute.
491
- This is effectively the same as how the built-in HTML converter works when the `data-uri` attribute is set.
492
- The `imagesdir` is blank by default, which means relative images paths are resolved relative to the input document.
493
- Relative images paths in the theme are resolved relative to the value of the `pdf-themesdir` attribute (which defaults to the directory of the theme file).
494
- The `imagesdir` attribute is not used when resolving an image path in the theme file.
495
- Absolute image paths are used as is.
496
-
497
- If the image is an SVG, and the SVG includes a nested raster image (PNG or JPG) with a relative path, that path is resolved relative to the directory that contains the SVG.
498
-
499
- The converter will refuse to embed an image if the target is a URI (including image references in an SVG) unless the `allow-uri-read` attribute is enabled via the CLI or API.
500
-
501
- If you use a linked image in an SVG, the width and height of that image must be specified.
502
- Otherwise, the SVG library will fail to process it.
503
-
504
- === Asciidoctor Diagram Integration
505
-
506
- Asciidoctor PDF provides seamless integration with Asciidoctor Diagram by setting the `data-uri` attribute by default.
507
- When the `data-uri` attribute is set, Asciidoctor Diagram returns the absolute path to the generated image, which Asciidoctor PDF can then locate.
508
- (This makes sense since technically, Asciidoctor Diagram must embed the image in the document, similar in spirit to the `data-uri` feature for HTML).
509
- This means the input directory and the output directory (and thus the imagesoutdir) can differ and Asciidoctor PDF will still be able to locate the generated image.
510
-
511
- == Image Scaling
512
-
513
- Since PDF is a fixed-width canvas, you almost always need to specify a width to get the image to fit properly on the page.
514
- There are five ways to specify the width of an image, listed here in order of precedence:
515
-
516
- [cols="1s,3"]
517
- |===
518
- |Attribute{nbsp}Name | Description
519
-
520
- |pdfwidth
521
- |The display width of the image as an absolute size (e.g., 2in), percentage of the content area width (e.g., 75%), or percentage of the page width (e.g., 100vw).
522
- If a unit of measurement is not specified (or not recognized), pt (points) is assumed.
523
- _Intended to be used for the PDF converter only._
524
-
525
- |scaledwidth
526
- |The display width of the image as an absolute size (e.g., 2in) or percentage of the content area width (e.g., 75%).
527
- If a unit of measurement is not specified, % (percentage) is assumed.
528
- If a unit of measurement is recognized, pt (points) is assumed.
529
- _Intended to be used for print output such as PDF._
530
-
531
- |image_width key from theme
532
- |Accepts the same values as pdfwidth.
533
- _Only applies to block images._
534
-
535
- |width
536
- |The unitless display width of the image (assumed to be pixels), typically matching the intrinsic width of the image.
537
- If the value ends in % (not recommended), it's assumed to be the percentage of the available content area width.
538
- If the width exceeds the content area width, the image is scaled down to the content area width.
539
-
540
- |_unspecified_
541
- |If you don't specify one of the aforementioned width settings, the intrinsic width of the image is used (the px value is multiplied by 75% to convert to pt, assuming canvas is 96 dpi) unless the width exceeds the content area width, in which case the image is scaled down to the content area width.
542
- |===
543
-
544
- The image is always sized according to the explicit or intrinsic width, then its height is scaled proportionally.
545
- The height of the image is ignored by the PDF converter unless the height of the image exceeds the content height of the page.
546
- In this case, the image is scaled down to fit on a single page.
547
-
548
- If you want a block image to align to the boundaries of the page (not the content margin), specify the `align-to-page` option (e.g., `opts="align-to-page"`).
549
- This is most useful when using vw units because you can make the image cover the entire width of the page.
550
-
551
- Images in running content and page background images also support the `fit` attribute (when specified using the image macro).
552
- See <<Background Image Sizing>> for details.
553
-
554
- === Using the pdfwidth Attribute
555
-
556
- The pdfwidth attribute is the recommended way to set the image size for the PDF output.
557
- This attribute is provided for two reasons.
558
- First, the fixed-width canvas often calls for a width that is distinct from other output formats, such as HTML.
559
- Second, this attribute allows the width to be expressed using a variety of units.
560
-
561
- The pdfwidth attribute supports the following units:
562
-
563
- * pt (default)
564
- * in
565
- * cm
566
- * mm
567
- * px
568
- * pc
569
- * vw (percentage of page width)
570
- * % (percentage of content area width)
571
-
572
- In all cases, the width is converted to pt.
573
-
574
- === Specifying a Default Width
575
-
576
- To scale all block images that don't define either a `pdfwidth` or `scaledwidth` attribute on the image macro, assign a value to the image-width key in your theme file.
577
-
578
- [source,yaml]
579
- ----
580
- image:
581
- width: 100%
582
- ----
583
-
584
- The image-width key accepts the same values as the `pdfwidth` attribute.
585
- Thus, you can think of it as the fallback value for the `pdfwidth` attribute.
586
- If specified, this value takes precedence over the `width` attribute on the image macro.
587
-
588
- === Inline Image Sizing
589
-
590
- Inline images can be sized in much the same way as block images (using the pdfwidth, scaledwidth or width attributes), with the following exceptions:
591
-
592
- * The viewport width unit (i.e., vw) is not recognized in this context.
593
- * The image will be scaled down, if necessary, to fit the width and height of the content area.
594
- * Inline images do not currently support a default width controlled from the theme.
595
-
596
- To confine the inline image to the height of the line while preserving the aspect ratio, use the attribute `fit=line`.
597
-
598
- If the resolved height of the image is less than or equal to 1.5 times the line height, the image won't disrupt the line height and is centered vertically in the line.
599
- This is done to maximize the use of available space.
600
- Once the resolved height exceeds this amount, the height of the line is increased (by increasing the font size of the invisible placeholder text) to accommodate the image.
601
- In this case, the surrounding text will be aligned to the bottom of the image.
602
- If the image height exceeds the height of the page, the image will be scaled down to fit on a single page (this may cause the image to advance to the subsequent page).
603
-
604
- === Background Image Sizing
605
-
606
- In addition to the width-related attributes previously covered, cover and background images can be sized relative to the page using the `fit` attribute of the image macro.
607
- The `fit` attribute works similarly to the `object-fit` property in CSS.
608
- Its value must be specified as a single keyword, chosen from the table below.
609
- The starting size of the image is determined by the explicit width, if specified, or the implicit width.
610
- The height is always derived from the width while respecting the implicit aspect ratio of the image.
611
- The available space for a background image (i.e., the canvas) is the page.
612
- If the `fit` attribute is not specified, it defaults to `contain` (i.e., the image is automatically scaled to fit the bounds of the page).
613
-
614
- [cols="1s,3"]
615
- |===
616
- | Value | Purpose
617
-
618
- | contain
619
- | The image is scaled up or down while retaining its aspect ratio to fit within the available space. (default)
620
-
621
- | cover
622
- | The image is scaled up or down while retaining its aspect ratio so the image completely covers the available space, even if it means the image must be clipped in one direction.
623
-
624
- | scale-down
625
- | The image is scaled down while retaining its aspect ratio to fit within the available space.
626
- If the image already fits, it is not scaled.
627
-
628
- | fill
629
- | The image is scaled to fit the available space even if it means modifying the aspect ratio of the image.
630
- Does not apply to SVG images.
631
-
632
- | none
633
- | The image is not scaled.
634
- |===
635
-
636
- The `fit` attribute is often combined with the `position` attribute, covered next, to control the placement of the image on the canvas.
637
-
638
- == Background Image Positioning
639
-
640
- In addition to scaling, background images for cover pages, content pages, and the title page support positioning via the `position` attribute.
641
- The `position` attribute accepts a syntax similar to the `background-position` property in CSS, except only the keyword positions are supported.
642
- The position consists of two values, the vertical position and the horizontal position (e.g., `top center`).
643
- If only one value is specified (e.g., `top`), the other value is assumed to be `center`.
644
- If the `position` attribute is not specified, the value is assumed to be `center center` (i.e., the image is centered on the page).
645
-
646
- The following table provides a list of the vertical and horizontal positioning keywords that are supported.
647
- You can use any combination of these keywords to position the image.
648
-
649
- |===
650
- | Vertical Positions | Horizontal Positions
651
-
652
- | top +
653
- center +
654
- bottom
655
-
656
- | left +
657
- center +
658
- right
659
- |===
660
-
661
- Here's an example of how to place a background image at the top center of every page:
662
-
663
- ----
664
- :page-background-image: image:bg.png[fit=none,pdfwidth=50%,position=top]
665
- ----
666
-
667
- Here's how to move it to the bottom right:
668
-
669
- ----
670
- :page-background-image: image:bg.png[fit=none,pdfwidth=50%,position=bottom right]
671
- ----
672
-
673
- If an image dimension matches the height or width of the page, the positioning keyword for that axis has no effect.
674
-
675
- == Fonts in SVG Images
676
-
677
- Asciidoctor PDF uses {url-prawn-svg}[prawn-svg] to embed SVGs in the PDF document, including SVGs generated by Asciidoctor Diagram.
678
-
679
- Actually, it's not accurate to say that prawn-svg embeds the SVG.
680
- Rather, prawn-svg is an SVG _renderer_.
681
- prawn-svg translates an SVG into native PDF text and graphic objects.
682
- You can think of the SVG as a sequence of drawing commands.
683
- The result becomes indistinguishable from other PDF objects.
684
-
685
- What that means for text is that any font family used for text in the SVG _must_ be registered in the Asciidoctor PDF theme file (and thus with Prawn).
686
- Otherwise, Prawn will fallback to using the closest matching built-in (afm) font from PDF (e.g., sans-serif becomes Helvetica).
687
- Recall that afm fonts only support basic Latin.
688
- As we like to say, PDF is <<docs/theming-guide.adoc#built-in-afm-fonts,bring your own font>>.
689
-
690
- If you're using Asciidoctor Diagram to generate SVGs to embed in the PDF, you likely need to specify the default font the diagramming tool uses.
691
- Let's assume you are making a plantuml diagram.
692
-
693
- To set the font used in the diagram, first create a file named [.path]_plantuml.cfg_ and populate it with the following content:
694
-
695
- ----
696
- skinparam defaultFontName Noto Serif
697
- ----
698
-
699
- TIP: You can choose any font name that is registered in your Asciidoctor PDF theme file.
700
- When using the default theme, your options are "Noto Serif", "M+ 1mn", and "M+ 1p Fallback".
701
-
702
- Next, pass that path to the `plantumlconfig` attribute in your AsciiDoc document (or set the attribute via the CLI or API):
703
-
704
- ----
705
- :plantumlconfig: plantuml.cfg
706
- ----
707
-
708
- Clear the cache of your diagrams and run Asciidoctor PDF with Asciidoctor Diagram enabled.
709
- The diagrams will be generated using Noto Serif as the default font, and Asciidoctor PDF will know what to do.
710
-
711
- The bottom line is this:
712
- If you're using fonts in your SVG, and you want those fonts to be preserved, those fonts must be defined in the Asciidoctor PDF theme file.
713
-
714
- == Supporting Additional Image File Formats
715
-
716
- In order to embed an image into a PDF, Asciidoctor PDF must understand how to decode it.
717
- To perform this work, Asciidoctor delegates to the underlying libraries.
718
- {url-prawn}[Prawn] provides support for decoding JPG and PNG images.
719
- {url-prawn-svg}[prawn-svg] brings support for translating SVG images to PDF commands.
720
- Without any additional libraries, those are the only image file formats supported by Asciidoctor PDF.
721
-
722
- If you need support for additional image formats, such as GIF, TIFF, WebP, or interlaced PNG--and you don't want to convert those images to a supported format like JPG--you must install the {url-prawn-gmagick}[prawn-gmagick] (>= 0.0.9) Ruby gem.
723
- prawn-gmagick is an extension for Prawn that delegates image decoding to {url-graphicsmagick}[GraphicsMagick] to add support for all image formats recognized by that library.
724
-
725
- prawn-gmagick has the additional benefit of *significantly* reducing the processing time, power, and memory necessary to generate a PDF that contains a lot of PNG images.
726
- For large books (such as Pro Git), you might see the conversion time drop by as much as half.
727
- Decoding PNG images requires a lot of mathematical computation, a task Ruby is not particularly efficient at performing.
728
- That's why adding the prawn-gmagick gem to the converter makes such a substantial difference.
729
-
730
- As an alternative to using prawn-gmagick, you could optimize the images you pass into Asciidoctor PDF, either by scaling them down or converting them to an uncompressed format like JPG.
731
-
732
- The prawn-gmagick gem uses native extensions to compile against GraphicsMagick.
733
- This system prerequisite limits installation to C Ruby running on Linux and macOS.
734
- Please refer to the {url-prawn-gmagick}[README for prawn-gmagick] to learn how to install it.
735
-
736
- $ gem install prawn-gmagick
737
-
738
- When this gem is installed, Asciidoctor automatically detects and loads it, then delegates all image decoding to GraphicsMagick by way of the bridge it provides.
739
- We highly recommend using this gem with Asciidoctor PDF if you're able to install it.
740
-
741
- The one downside of delegating to GraphicsMagick is that it can mangle certain PNG images.
742
- If this happens, you can instruct Asciidoctor PDF to not delegate to GraphicsMagick to load PNG images by requiring `asciidoctor/pdf/nopngmagick` when calling Asciidoctor PDF, as follows:
743
-
744
- $ asciidoctor-pdf -r asciidoctor/pdf/nopngmagick doc.adoc
745
-
746
- You can also tell Asciidoctor PDF not to use prawn-gmagick at all by requiring `asciidoctor/pdf/nogmagick` when calling Asciidoctor PDF, as follows:
747
-
748
- $ asciidoctor-pdf -r asciidoctor/pdf/nogmagick doc.adoc
749
-
750
- Granted, bypassing prawn-gmagick means you no longer get support for additional image formats that Prawn cannot handle or the PNG acceleration.
751
-
752
- == Importing PDF Pages
753
-
754
- In addition to using a PDF page for the front or back cover, you can also insert a PDF page at an arbitrary location.
755
- This technique is useful to include pages that have complex layouts and graphics prepared in a specialized design program (such as Inkscape), which would otherwise not be achievable using this converter.
756
- One such example is an insert such as an advertisement or visual interlude.
757
-
758
- To import the first page from a PDF file, use the block image macro with the PDF filename as the image target.
759
-
760
- [source,asciidoc]
761
- ----
762
- image::custom-page.pdf[]
763
- ----
764
-
765
- The converter will insert the page from the PDF as a dedicated page that matches the size and layout of the page being imported (no matter where the block image occurs).
766
- Therefore, there's no need to put a manual page break (i.e., `<<<`) around the image macro.
767
-
768
- By default, this macro will import the first page of the PDF.
769
- To import a different page, specify it as a 1-based index using the `page` attribute.
770
-
771
- [source,asciidoc]
772
- ----
773
- image::custom-pages.pdf[page=2]
774
- ----
775
-
776
- You can import multiple pages either using multiple image macros or using the `pages` attribute.
777
- The `pages` attribute accepts individual page numbers or page number ranges (two page numbers separated by `..`).
778
- The values can be separated either by commas or semi-colons.
779
- (The syntax is similar to the syntax uses for the `lines` attribute of the AsciiDoc include directive).
780
-
781
- [source,asciidoc]
782
- ----
783
- image::custom-pages.pdf[pages=3;1..2]
784
- ----
785
-
786
- Pages are imported in the order listed.
787
-
788
- To see a practical example of how to use this feature, refer to the blog post https://fromplantoprototype.com/blog/2019/08/07/importing-pdf-pages-in-asciidoctor-pdf/[Importing PDF Pages in asciidoctor-pdf].
789
-
790
- CAUTION: An image macro used to imports PDF pages should never be nested inside a delimited block or table cell.
791
- It should be a direct descendant of the document or a section.
792
- That's because what it imports are entire pages.
793
- If it's used inside a delimited block or table cell, the behavior is unspecified.
794
-
795
- == Interdocument Xrefs
796
-
797
- An xref to another AsciiDoc document (i.e., an interdocument xref) will either become a link to the PDF file generated from that source document or an internal link to an anchor within the current document.
798
- Which one it becomes depends on whether the target has been included into the current document.
799
- These section describes these two scenarios.
800
-
801
- === Referencing Another Document
802
-
803
- If the target PDF is generated from an AsciiDoc file, you can make a reference to that PDF using the xref macro.
804
-
805
- Let's assume the current document is [.path]_a.adoc_ and the PDF you want to reference is generated from [.path]_b.adoc_.
806
- Here's how you can make a reference from the PDF generated from [.path]_a.adoc_ to the PDF generated from [.path]_b.adoc_.
807
-
808
- [source,asciidoc]
809
- ----
810
- A link to xref:b.adoc[b].
811
- ----
812
-
813
- This xref macro is translated to a link that refers to [.path]_b.pdf_.
814
-
815
- If there's an anchor you want to target in [.path]_b.pdf_, for example _chapter-b_, you can describe it using a URL fragment just like you would with any URL.
816
-
817
- [source,asciidoc]
818
- ----
819
- A link to xref:b.adoc#chapter-b[b].
820
- ----
821
-
822
- WARNING: Linking to a named anchor isn't supported by all PDF viewers.
823
- Some viewers (like Firefox) only support relative links when the PDF is accessed through a web server.
824
- To verify it's working, test the PDF in Firefox and served through a local web server.
825
-
826
- PDF supports a variety of PDF link open parameters you can control using the URL fragment.
827
- For example, you can configure the PDF to open on a specific page using the special fragment `page=<N>`, where `<N>` is the 1-based page number.
828
-
829
- [source,asciidoc]
830
- ----
831
- A link to page 2 of xref:b.adoc#page=2[b].
832
- ----
833
-
834
- You can find a list of all the special fragment parameters in the https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf#G4.1500549[PDF Open Parameters^] reference.
835
-
836
- === Converting Interdocument Xrefs to Internal Xrefs
837
-
838
- If you're using this converter to generate a single PDF file from multiple source documents (combined using the include directive), references between those included documents must become internal references.
839
- Interdocument cross references (i.e., xrefs) will only successfully make that transition if you structure your document in accordance with the rules.
840
-
841
- Those rules are as follows:
842
-
843
- . The path segment of the interdocument xref must match the project-relative path of the included document
844
- . The reference must include the ID of the target element
845
-
846
- For instance, if your primary document contains the following include:
847
-
848
- [source,asciidoc]
849
- ----
850
- \include::chapters/chapter-1.adoc[]
851
- ----
852
-
853
- Then an interdocument xref to an anchor in that chapter must be expressed as:
854
-
855
- [source,asciidoc]
856
- ----
857
- <<chapters/chapter-1.adoc#_anchor_name,Destination in Chapter 1>>
858
- ----
859
-
860
- This rule holds regardless of which document the xref is located in.
861
-
862
- To resolve the interdocument xref, the converter first checks if the target matches the `docname` attribute.
863
- It then looks to see if the target matches one of the included files.
864
- (In both cases, it ignores the file extension).
865
- If Asciidoctor cannot resolve the target of an interdocument xref, it simply makes a link (like the HTML converter).
866
-
867
- Let's consider a complete example.
868
- Assume you are converting the following book document at the root of the project:
869
-
870
- [source,asciidoc]
871
- ----
872
- = Book Title
873
- :doctype: book
874
-
875
- \include::chapters/chapter-1.adoc[]
876
-
877
- \include::chapters/chapter-2.adoc[]
878
- ----
879
-
880
- Where the contents of chapter 1 is as follows:
881
-
882
- [source,asciidoc]
883
- ----
884
- == Chapter 1
885
-
886
- We cover a little bit here.
887
- The rest you can find in <<chapters/chapter-2.adoc#_chapter_2,Chapter 2>>.
888
- ----
889
-
890
- And the contents of chapter 2 is as follows:
891
-
892
- [source,asciidoc]
893
- ----
894
- == Chapter 2
895
-
896
- Prepare to be educated.
897
- This chapter has it all!
898
-
899
- To begin, jump to <<chapters/chapter-2/first-steps.adoc#_first_steps,first steps>>.
900
-
901
- <<<
902
-
903
- \include::chapter-2/first-steps.adoc[]
904
- ----
905
-
906
- And, finally, the contents of the nested include is as follows:
907
-
908
- [source,asciidoc]
909
- ----
910
- === First Steps
911
-
912
- Let's start small.
913
- ----
914
-
915
- You'll find when you run this example that all the interdocument xrefs become internal references in the PDF.
916
-
917
- The reason both the path and anchor are required (even when linking to the top of a chapter) is so the interdocument xref works independent of the converter.
918
- In other words, it encodes the complete information about the reference so the converter can sort out where the target is in all circumstances.
919
-
920
- == STEM Support
921
-
922
- Unlike the built-in HTML converter, Asciidoctor PDF does not provide native support for STEM blocks and inline macros (i.e., asciimath and latexmath).
923
- That's because Asciidoctor core doesn't process the STEM content itself.
924
- It simply passes it through to the converter.
925
- When converting to HTML, Asciidoctor relies on the JavaScript-based MathJax library to parse and render the STEM expressions in the browser when the page is loaded.
926
- The HTML converter wraps the expressions in special markup so MathJax can find and process them.
927
-
928
- In order to insert a rendered expression into the PDF, the toolchain must parse the expressions and convert them to a format the PDF writer (Prawn) can understand.
929
- That typically means converting to an image.
930
-
931
- One solution that provides this capability is an extension named Asciidoctor Mathematical, which we'll cover in the next section.
932
-
933
- ////
934
- Another solution, which is still under development, uses Mathoid to convert STEM equations to images.
935
- Mathoid is a library that invokes MathJax using a headless browser, so it supports both asciimath and latexmath equations.
936
- That prototype can be found in the https://github.com/asciidoctor/asciidoctor-extensions-lab#extension-catalog[Asciidoctor extensions lab].
937
- ////
938
-
939
- === Asciidoctor Mathematical
940
-
941
- {url-asciidoctor-mathematical}[Asciidoctor Mathematical] is an extension for Asciidoctor that provides alternate processing of STEM blocks and inline macros.
942
- After the document has been parsed, the extension locates each asciimath, latexmath, and stem block and inline macro, converts the expression to an image, and replaces the expression with an image.
943
- It uses Mathematical to render the LaTeX notation as an image.
944
- If the expression is AsciiMath, it first uses AsciiMath gem to convert to LaTeX.
945
- Conversion then proceeds as normal.
946
-
947
- Asciidoctor Mathematical is a Ruby gem that uses native extensions.
948
- It has a few system prerequisites which limit installation to Linux and macOS.
949
- Please refer to the {url-asciidoctor-mathematical}#installation[installation section] in the Asciidoctor Mathematical README to learn how to install it.
950
-
951
- Once Asciidoctor Mathematical is installed, you can enable it when invoking Asciidoctor PDF using the `-r` flag:
952
-
953
- $ asciidoctor-pdf -r asciidoctor-mathematical sample.adoc
954
-
955
- If you're invoking Asciidoctor via the API, you need to require the gem before invoking Asciidoctor:
956
-
957
- [source,ruby]
958
- ----
959
- require 'asciidoctor-mathematical'
960
- require 'asciidoctor-pdf'
961
-
962
- Asciidoctor.convert_file 'sample.adoc', backend: 'pdf', safe: :safe
963
- ----
964
-
965
- To get the best quality output and maximize speed of conversion, we recommend configuring Asciidoctor Mathematical to convert equations to SVG.
966
- You control this setting using the `mathematical-format` AsciiDoc attribute:
967
-
968
- $ asciidoctor-pdf -r asciidoctor-mathematical -a mathematical-format=svg sample.adoc
969
-
970
- Refer to the {url-asciidoctor-mathematical}#readme[README] for Asciidoctor Mathematical to learn about additional settings and options.
971
-
972
- == Skipping Passthrough Content
973
-
974
- Asciidoctor PDF does not support arbitrary passthrough content.
975
- While the basebackend for the PDF converter is html, it only recognizes a limited subset of inline HTML elements that can be mapped to PDF (e.g., a, strong, em, code, ).
976
- Therefore, if your content contains passthrough blocks or inlines, you most likely have to use a conditional preprocessor to skip them (and make other arrangements).
977
-
978
- Here's an example of how to skip a passthrough block when converting to PDF:
979
-
980
- [source,asciidoc]
981
- ----
982
- \ifndef::backend-pdf[]
983
- <script>
984
- //...
985
- </script>
986
- \endif::[]
987
- ----
988
-
989
- Here's an example of how to only enable a passthrough block when converting to HTML5:
990
-
991
- [source,asciidoc]
992
- ----
993
- \ifdef::backend-html5[]
994
- <script>
995
- //...
996
- </script>
997
- \endif::[]
998
- ----
999
-
1000
- == Shaded Blocks and Performance
1001
-
1002
- Certain blocks are rendered with a shaded background, such as verbatim (listing, literal, and source), sidebar, and example blocks.
1003
- In order to calculate the dimensions of the shaded region, Asciidoctor PDF has to "`dry run`" the block to determine how many pages it consumes.
1004
- While this strategy has a low impact when processing shorter blocks, it can drastically deteriorate performance when processing a block that spans dozens of pages.
1005
- As a general rule of thumb, you should avoid using shaded blocks which span more than a handful of pages.
1006
-
1007
- == Autofitting Text
1008
-
1009
- Verbatim blocks often have long lines that don't fit within the fixed width of the PDF canvas.
1010
- And unlike on the web, the PDF reader cannot scroll horizontally to reveal the overflow text.
1011
- Therefore, the long lines are forced to wrap.
1012
- Wrapped lines can make the verbatim blocks hard to read or even cause confusion.
1013
-
1014
- To help address this problem, Asciidoctor PDF provides the `autofit` option on all verbatim (i.e., literal, listing and source) blocks to attempt to fit the text within the available width.
1015
- When the `autofit` option is enabled, Asciidoctor PDF will decrease the font size (as much as it can) until the longest line fits without wrapping.
1016
-
1017
- CAUTION: The converter will not shrink the font size beyond the value of the `base_font_size_min` key specified in the PDF theme.
1018
- If that threshold is reached, lines may still wrap.
1019
- To allow `autofit` to handle all cases, set `base_font_size_min` to `0` in your theme.
1020
-
1021
- Here's an example of the autofit option enabled on a source block:
1022
-
1023
- [source,asciidoc]
1024
- ....
1025
- [source%autofit,java]
1026
- ----
1027
- @SessionScoped
1028
- public class WidgetRepository {
1029
- @GET
1030
- @Produces("application/json")
1031
- public List<String> listAll(@QueryParam("start") Integer start, @QueryParam("max") Integer max) {
1032
- ...
1033
- }
1034
- }
1035
- ----
1036
- ....
1037
-
1038
- If you want to enable the autofit option globally, set the `autofit-option` document attribute in the document header (or somewhere before the relevant blocks):
1039
-
1040
- [source,asciidoc]
1041
- ----
1042
- :autofit-option:
1043
- ----
1044
-
1045
- == Autowidth Tables
1046
-
1047
- Asciidoctor PDF does support autowidth tables.
1048
- However, the behavior differs from HTML when the content forces the table to the page boundary.
1049
- The behavior, which is handled by the prawn-table library, is explained in this section.
1050
-
1051
- If the natural width of all columns (based on the width of the cell content) is less than the width of the page, it behaves as you'd expect.
1052
- Each column is assigned the width it needs to prevent the content from wrapping.
1053
-
1054
- However, when the natural width of all columns exceeds the width of the page, the behavior may not be what you expect.
1055
- What prawn-table does is compute how to arrange the table on an infinite canvas, where each column can have a width no greater than the width of the page.
1056
- Then, it reduces the width of the table by reducing the width of each column proportionally.
1057
- As a result, columns which reported the width necessary to render without wrapping now no longer do.
1058
-
1059
- The reason this compression is not performed like in HTML is because prawn-table has no awareness of words.
1060
- Thus, it doesn't know how to redistribute with width intelligently.
1061
-
1062
- To protected against truncation or insufficient width errors, prawn-table wraps text by character.
1063
- That's why the last character in the cell can end up getting wrapped.
1064
- (There's a small amount of tolerance built in to prawn-table to address some edge cases, but it's not sufficient to handle all of them).
1065
-
1066
- For the reason just explained, you should be extremely careful with relying on autowidth tables in Asciidoctor PDF, especially when the natural content of the cells forces the table to page boundary.
1067
- Let experience be your guide.
1068
-
1069
- == Printing Page Ranges
1070
-
1071
- The print dialog doesn't understand the page numbers labels (which appear in the running content).
1072
- Instead, it only considers physical pages.
1073
- Therefore, to print a range of pages as they are labeled in the document, you need to add the number of front matter pages (i.e., the non-numbered pages) to the page number range in the print dialog.
1074
-
1075
- For example, if you only want to print the first 5 pages labeled with a page number (e.g., 1-5), and there are 2 pages before the page labeled as page 1, you need to add 2 to both numbers in the range, giving you a physical page range of 3-7.
1076
- That's the range you need to enter into the print dialog.
1077
-
1078
- == Title Page
1079
-
1080
- Unlike other converters, the PDF converter introduces a dedicated title page at the start of the document.
1081
- The title page contains the doctitle, author, date, and revision info.
1082
- If a front cover image is specified, the title page comes after the front cover.
1083
- The title page can be styled using the theme and/or reserved page attributes.
1084
-
1085
- The title page is enabled if either of these conditions are met:
1086
-
1087
- * The document has the `book` doctype.
1088
- * The `title-page` attribute is set (with an empty value) in the document header.
1089
-
1090
- When the title page is enabled, the table of contents (aka TOC) also gets is own page (or pages, if necessary).
1091
-
1092
- == Table of Contents
1093
-
1094
- The table of contents (TOC) is not included by default.
1095
- The TOC is only included if the `toc` attribute is set on the document.
1096
- The value of this attribute determines the placement.
1097
- If a value is not specified, the placement defaults to `auto`, which is directly after the document title.
1098
-
1099
- For documents that have the book doctype, the TOC is inserted using discrete pages between the title page and the first page of content.
1100
- For all other doctypes (unless the `title-page` attribute is set), the TOC is inserted in the flow of text.
1101
- If a placement is not specifie, that location is between the document title and the first block of content.
1102
-
1103
- While the table of contents is not included by default, the PDF outline is always included.
1104
- The `toclevels` attribute controls the depth of both the TOC and the PDF outline (regardless of whether the TOC is enabled).
1105
- The depth of the outline can be controlled independently using the `outlinelevels` attribute.
1106
- Both attributes can also be set on individual sections to override the depth for a given section and its children.
1107
-
1108
- NOTE: If a document that has the book doctype includes a preface, an entry for the preface is only included in the TOC if the `preface-title` is assigned a value (e.g., `preface-title=Preface`).
1109
- This value is used as the heading of the preface and as the text of the entry in the TOC.
1110
-
1111
- == Index Catalog
1112
-
1113
- Asciidoctor PDF supports generating an index catalog that itemizes all index terms defined in the document, allowing the reader to navigate the document by keyword.
1114
-
1115
- To get Asciidoctor PDF to generate an index, add a level-1 section annotated with the `index` style near the end of your document.
1116
- The converter will automatically populate the catalog with the list of index terms in the document, organized by first letter.
1117
-
1118
- [source,asciidoc]
1119
- ----
1120
- [index]
1121
- = Index
1122
- ----
1123
-
1124
- You can use any text you want for the title of this section.
1125
- The only restriction is that no index terms may be defined below this section.
1126
-
1127
- NOTE: Although the catalog is generated automatically, you have to mark the index terms manually.
1128
- However, you could use an extension, such as a TreeProcessor, to automatically mark index terms.
1129
-
1130
- === How Index Terms are Grouped and Sorted
1131
-
1132
- By default, the converter groups index terms by the first letter of the primary term (e.g., A), which we call the category.
1133
- These categories are displayed in alphabetically order in the index.
1134
- Within the category, the converter sorts the terms alphabetically.
1135
-
1136
- The exception to this rule is if the primary term does not start with a letter.
1137
- In this case, the converter group the term (along with its secondary and tertiary terms) in a special category named @.
1138
- The @ category is displayed before all other categories in the index.
1139
-
1140
- If you want to modify this behavior, you must extend the index catalog and apply your own grouping and sorting rules.
1141
-
1142
- For example, let's say that all your functions begin with the prefix `fn`, but you want to group and sort them by the function name that follows.
1143
- Here's rudimentary code you can use to do that:
1144
-
1145
- .index-customizer.rb
1146
- [source,ruby]
1147
- ----
1148
- require 'asciidoctor-pdf'
1149
-
1150
- module Asciidoctor::PDF
1151
- IndexCatalog.prepend (::Module.new do
1152
- def store_primary_term name, dest = nil
1153
- store_dest dest if dest
1154
- category = (name.delete_prefix 'fn').upcase.chr
1155
- (init_category category).store_term name, dest
1156
- end
1157
- end)
1158
-
1159
- IndexTermGroup.prepend (::Module.new do
1160
- def <=> other
1161
- this = @name.delete_prefix 'fn'
1162
- that = other.name.delete_prefix 'fn'
1163
- (val = this.casecmp that) == 0 ? this <=> that : val
1164
- end
1165
- end)
1166
- end
1167
- ----
1168
-
1169
- You load this code when calling Asciidoctor PDF as follows:
1170
-
1171
- $ asciidoctor-pdf -r ./index-customizer.rb doc.adoc
1172
-
1173
- Now the index terms will be grouped and sorted according to your custom rules.
1174
-
1175
- == Optimizing the Generated PDF
1176
-
1177
- By default, Asciidoctor PDF does not optimize the PDF it generates or compress its streams.
1178
- This section covers several approaches you can take to optimize your PDF.
1179
-
1180
- IMPORTANT: If you're creating a PDF for Amazon's Kindle Direct Publishing (KDP), GitLab repository preview, or other online publishers, you'll likely need to optimize the file before uploading.
1181
- In their words, you must tidy up the reference tree and https://kdp.amazon.com/en_US/help/topic/G201953020#check[flatten all transparencies^] (mostly likely referring to images).
1182
- If you don't do this step, the platform may reject your upload or fail to display it properly.
1183
- (For KDP, `-a optimize` works best.
1184
- For GitLab repository preview, either `-a optimize` or `hexapdf optimize` will do the trick.)
1185
-
1186
- === Enable Stream Compression
1187
-
1188
- The simplest way to reduce the size of the PDF file is to enable stream compression (using the FlateDecode method).
1189
- You can enable this feature by setting the `compress` attribute on the document:
1190
-
1191
- $ asciidoctor-pdf -a compress document.adoc
1192
-
1193
- For a more thorough optimization, you can use the integrated optimizer or hexapdf.
1194
- Read on to learn how.
1195
-
1196
- === rghost
1197
-
1198
- {project-name} also provides a flag (and bin script) that uses GhostScript (via rghost) to optimize and compress the generated PDF (with minimal impact on quality).
1199
- You must have Ghostscript (command: `gs`) and the `rghost` gem installed to use it.
1200
-
1201
- Here's an example usage that converts your document and optimizes it:
1202
-
1203
- $ asciidoctor-pdf -a optimize basic-example.adoc
1204
-
1205
- The command will generate an optimized PDF 1.4 file.
1206
- In addition to optimizing the PDF file, it also converts it from plain PDF to a PDF/X-1a.
1207
-
1208
- If this command fails because the `gs` command cannot be found, you'll need to set it using the `GS` environment variable.
1209
- On Windows, this step is almost always required since the Ghostscript installer does not install the `gs` command into a standard location.
1210
- Here's an example that shows how you can override the `gs` command path:
1211
-
1212
- $ GS=/path/to/gs asciidoctor-pdf -a optimize basic-example.adoc
1213
-
1214
- You'll need to use the technique for assigning an environment variable that's relevant for your system.
1215
-
1216
- The one limitation of generating a PDF/X-1a file is that it does not allow non-ASCII characters in the document metadata fields (i.e., title, author, subject, etc).
1217
- To workaround this limitation, you can force Ghostscript to generate a PDF 1.3 file using the `pdf-version` attribute:
1218
-
1219
- $ asciidoctor-pdf -a optimize -a pdf-version=1.3 basic-example.adoc
1220
-
1221
- CAUTION: Downgrading the PDF version may break the PDF if it contains an image that uses color blending or transparency.
1222
- Specifically, the text on the page can become rasterized, which causes links to stop working and prevents text from being selected.
1223
- If you're in this situation, it might be best to try <<hexapdf>> instead.
1224
-
1225
- If you're looking for a smaller file size, you can try reducing the quality of the output file by passing a quality keyword to the `optimize` attribute (e.g., `--optimize=screen`).
1226
- The `optimize` attribute accepts the following keywords: `default` (default, same if value is empty), `screen`, `ebook`, `printer`, and `prepress`.
1227
- Refer to the https://www.ghostscript.com/doc/current/VectorDevices.htm#PSPDF_IN[Ghostscript documentation^] to learn what settings these presets affect.
1228
-
1229
- If you've already generated the PDF, and want to optimize it directly, you can use the bin script:
1230
-
1231
- $ asciidoctor-pdf-optimize basic-example.pdf
1232
-
1233
- The command will overwrite the PDF file with an optimized version.
1234
- You can also try reducing the quality of the output file using the `--quality` flag (e.g., `--quality screen`).
1235
- The `--quality` flag accepts the following keywords: `default` (default), `screen`, `ebook`, `printer`, and `prepress`.
1236
-
1237
- In both cases, if a file is found with the extension `.pdfmark` and the same rootname as the input file, it will be used to add metadata to the generated PDF document.
1238
- This file is necessary when using versions of Ghostscript < 8.54, which did not automatically preserve this metadata.
1239
- You can instruct the converter to automatically generate a pdfmark file by setting the `pdfmark` attribute (i.e., `-a pdfmark`)
1240
- When using a more recent version of Ghostscript, you do not need to generate a `.pdfmark` file for this purpose.
1241
-
1242
- IMPORTANT: The `asciidoctor-pdf-optimize` is not guaranteed to reduce the size of the PDF file.
1243
- It may actually make the PDF larger.
1244
- You should probably only consider using it if the file size of the original PDF is several megabytes.
1245
-
1246
- If you have difficulty getting the `rghost` gem installed, or you aren't getting the results you expect, you can try the optimizer provided by hexapdf instead.
1247
-
1248
- === hexapdf
1249
-
1250
- Another option to optimize the PDF is https://hexapdf.gettalong.org/[hexapdf] (gem: hexapdf, command: hexapdf).
1251
- Before introducing it, though, it's important to point out that its license is AGPL.
1252
- If that's okay with you, read on to learn how to use it.
1253
-
1254
- First, install the hexapdf gem using the following command:
1255
-
1256
- $ gem install hexapdf
1257
-
1258
- You can then use it to optimize your PDF as follows:
1259
-
1260
- $ hexapdf optimize --compress-pages --force basic-example.pdf basic-example.pdf
1261
-
1262
- This command does not manipulate the images in any way.
1263
- It merely compresses the objects in the PDF and prunes any unreachable references.
1264
- But given how much waste Prawn leaves behind, this turns out to reduce the file size substantially.
1265
-
1266
- You can hook this command directly into the converter by providing your own implementation of the `Optimizer` class.
1267
- Start by creating a Ruby file named [.path]_optimizer-hexapdf.rb_, then populate it with the following code:
1268
-
1269
- .optimizer-hexapdf.rb
1270
- [source,ruby]
1271
- ----
1272
- require 'hexapdf/cli'
1273
-
1274
- class Asciidoctor::PDF::Optimizer
1275
- def initialize(*)
1276
- app = HexaPDF::CLI::Application.new
1277
- app.instance_variable_set :@force, true
1278
- @optimize = app.main_command.commands['optimize']
1279
- end
1280
-
1281
- def optimize_file path
1282
- options = @optimize.instance_variable_get :@out_options
1283
- options.compress_pages = true
1284
- #options.object_streams = :preserve
1285
- #options.xref_streams = :preserve
1286
- #options.streams = :preserve # or :uncompress
1287
- @optimize.execute path, path
1288
- nil
1289
- rescue
1290
- # retry without page compression, which can sometimes fail
1291
- options.compress_pages = false
1292
- @optimize.execute path, path
1293
- nil
1294
- end
1295
- end
1296
- ----
1297
-
1298
- 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.
1299
-
1300
- $ asciidoctor-pdf -r ./optimizer-hexapdf.rb -a optimize basic-example.adoc
1301
-
1302
- Now you can convert and optimize all in one go.
1303
-
1304
- To see more options that `hexapdf optimize` offers, run:
1305
-
1306
- $ hexapdf help optimize
1307
-
1308
- 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).
1309
- You can also disable page compressioin (e.g., `--no-compress-pages` from the CLI or `options.compress_pages = false` from the API).
1310
-
1311
- hexapdf also allows you to add password protection to your PDF, if that's something you're interested in doing.
1312
-
1313
- === Rasterizing the PDF
1314
-
1315
- Instead of optimizing the objects in the vector PDF, you may want to rasterize the PDF instead.
1316
- Rasterizing the PDF prevents any of the text or other objects from being selected, similar to a scanned document.
1317
-
1318
- Asciidoctor PDF doesn't provide built-in support for rasterizing the generated PDF.
1319
- However, you can use Ghostscript to flatten all the text in the PDF, thus preventing it from being selected.
1320
-
1321
- $ gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dNoOutputFonts -r300 -o output.pdf input.pdf
1322
-
1323
- You can adjust the value of the `-r` option (the density) to get a higher or lower quality result.
1324
-
1325
- Alternately, you can use the `convert` command from ImageMagick to convert each page in the PDF to an image.
1326
-
1327
- $ convert -density 300 -quality 100 input.pdf output.pdf
1328
-
1329
- Yet another option is to combine Ghostscript and ImageMagick to produce a PDF with pages converted to images.
1330
-
1331
- $ gs -dBATCH -dNOPAUSE -sDEVICE=png16m -o /tmp/tmp-%02d.png -r300 input.pdf
1332
- convert /tmp/tmp-*.png output.pdf
1333
- rm -f /tmp/tmp-*.png
1334
-
1335
- Using Ghostscript to handle the rasterization produces a much smaller output file.
1336
- The drawback of using Ghostscript in this way is that it has to use intermediate files.
199
+ To learn how the theming system works and how to create and apply custom themes, refer to the {url-project-docs}/theme/[Asciidoctor PDF theming documentation].
1337
200
 
1338
201
  ifndef::env-site[]
1339
202
  == Contributing