Creating PDF from documentation
Diplodoc can generate documentation in PDF format.
PDF document structure
A PDF document consists of three parts:
-
Title and closing pages
They are displayed at the beginning and end of the documentation and are not numbered.
Title pages are specified in the
startPagesblock intoc.yaml:pdf: startPages: - path-to-page-1.md - path-to-page-2.md - path-to-page-n.mdClosing pages are specified in the
endPagesblock intoc.yaml:pdf: endPages: - path-to-page-1.md - path-to-page-2.md - path-to-page-n.mdWarning
Pages from
pdf.startPagesandpdf.endPagesdo not support localization viayfm translate.When building documentation, title and closing pages are not transformed into files. To check their layout in a browser, use the
--pdf-debugflag during the build: it will create HTML versions of the pages fromstartPagesandendPages. -
Table of contents
Diplodoc automatically generates a table of contents based on
toc.yaml. Each item in the list is a link to a page.Section titles that group a set of articles are displayed as plain text.
Files with thehidden: trueattribute intoc.yamlare not included in the PDF. To include them, set thehiddenPolicy: falseparameter. -
Main content
All Diplodoc features are supported in PDF:
- Page Constructor blocks;
- cross-references;
- images and other media files.
Each subsequent article of the PDF document starts on a new page. Articles in the PDF are arranged in the same order as in the table of contents.
Build
Setup
-
In the
toc.yamlfile, add thestartPagessection to generate title pages. -
Enable PDF support in the
.yfmconfiguration file:pdf: enabled: true
Generation
-
Build the documentation project:
yfm build -i . -o ./docs-output --pdf- ``-i .` — path to the folder with sources (in the example, the current folder);
- ``-o ./docs-output` — path to the folder for build results;
- ``--pdf
— flag that enables data preparation for PDF generation; if PDF support is enabled in.yfm`, the flag does not need to be passed.
-
Run the PDF generator:
npx -- @diplodoc/pdf-generator@latest -i ./docs-output
Note
For each toc.yaml file, Diplodoc creates a separate single-page.pdf.
Styling
You can change the appearance of the PDF document using CSS styles.
Alert
Diplodoc removes styles added inside Markdown files during PDF generation — this is done for security purposes.
Content filtering
To show or hide elements only in the PDF version, use presets.
-
Add the required variable to
presets.yaml:pdf: version: pdf -
Use the condition in the text:
{% if version == "pdf" %} Этот текст появится только в PDF-версии. {% endif %}