Build a PDF¶
It is possible to build a single PDF that contains all of your book’s content. This page describes a couple ways to do so.
警告
PDF building is experimental, and may change or have bugs.
There are two approaches to building PDF files.
Build a PDF from your book HTML¶
It is possible to build a single PDF from your book’s HTML. This starts by converting all of your book’s content into a single HTML file, and then renders it as a PDF by emulating a browser from the command-line.
Installation¶
Your system will need to use pyppeteer to parse the generated HTML for
conversion to PDF.
You can install it like so:
pip install pyppeteer
You may also need to install this bundle of packages below (on *nix systems):
gconf-service
libasound2
libatk1.0-0
libatk-bridge2.0-0
libc6
libcairo2
libcups2
libdbus-1-3
libexpat1
libfontconfig1
libgcc1
libgconf-2-4
libgdk-pixbuf2.0-0
libglib2.0-0
libgtk-3-0
libnspr4
libpango-1.0-0
libpangocairo-1.0-0
libstdc++6
libx11-6
libx11-xcb1
libxcb1
libxcomposite1
libxcursor1
libxdamage1
libxext6
libxfixes3
libxi6
libxrandr2
libxrender1
libxss1
libxtst6
ca-certificates
fonts-liberation
libappindicator1
libnss3
lsb-release
xdg-utils
wget
Build¶
To build a single PDF from your book’s HTML, use the following command:
jupyter-book build mybookname/ --builder pdfhtml
or
jb build mybookname/ --builder pdfhtml
警告
If you get a “MaxRetryError” and see mentions of SSL in the error message when building the PDF,
this could be due to a bug in pyppeteer as it downloads Chromium for the first time.
See this GitHub comment
for a potential fix, and this Jupyter Book issue
where we’re tracking the issue.
Control the look of PDF via HTML¶
Because you are using HTML as an intermediary for your book’s PDF, you can control the look and feel of the HTML via your own CSS rules. Most CSS changes that you make to your HTML website will also persist in the PDF version of that website. For information about how to define your own CSS rules, see Custom CSS or JavaScript.
To add CSS rules that only apply to the printed PDF, use the @media print CSS pattern to define print-specific rules. These will only be applied when the HTML is being printed, and will not show up in your non-PDF website.
For example, to hide the right table of contents at print time, you could add this rule:
@media print {
.bd-toc {
visibility: hidden;
}
}
The right Table of Contents would be present in your live website, but hidden when someone printed a PDF of your website.
Build a PDF using LaTeX¶
You can also use LaTeX to build a PDF of your book.
This can behave differently depending on your operating system and tex setup.
This section tries to recommend a few best-practices.
注解
We recommend using the texlive distribution
The default is to build your project as a single PDF file, however it is possible to build
individual PDF files for each page of the project by enabling the --individualpages option
when using the pdflatex builder.
Installation¶
For Debian-based Linux platforms it is recommended to install the following packages:
sudo apt-get install texlive-latex-recommended texlive-latex-extra \
texlive-fonts-recommended texlive-fonts-extra \
texlive-xetex latexmk
Alternatively you can install the full TeX Live distribution.
For OSX you may want to use MacTeX which is a more
user friendly approach. Alternatively you may also use TeX Live.
For Windows users, please install TeX Live.
Build¶
jupyter-book uses the sphinx-jupyterbook-latex package
which handles much of the customised LaTeX infrastructure. A feature list of this package can be found
here.
This package enables building pdf files with full support for the file and part/chapter
structures that are defined in the _toc.yml. This builds
pdf files that are similar in structure to the html output.
If you need to turn off this package, the following config is required:
latex:
use_jupyterbook_latex: false
To build a PDF of your project using LaTeX, use the following command:
jupyter-book build mybookname/ --builder pdflatex
or
jb build mybookname/ --builder pdflatex
注解
If you would just like to generate the latex file you may use:
jb build mybookname/ --builder latex
Individual PDF Files:
警告
The current implementation of --individualpages does not make use of the improvements
introduced by sphinx-jupyterbook-latex and
uses the default latex writer included with Sphinx.
We are currently working on making improvements to how --individualpages are constructed.
You can track progress here
To build PDF files for each page of the project,
you can specify the option --individualpages for --builder=pdflatex.
The individual PDF files will be available in the _build/latex build folder.
These files will have the same name as the source file or, if nested in folders, will be named {folder}-{filename}.pdf.
注解
When specifying a page using the build command,
the --individualpages will automatically be set to True.
In the future we intend for this to produce latex documents more suitable to single pages (see issue #904).
Updating the name of the Global PDF file¶
To update the name of your PDF file you can set the following in _config.yml
latex:
latex_documents:
targetname: book.tex
This will act as an automatic override when Sphinx builds the
latex_documents. It is typically inferred by Sphinx but when
using jupyter-book naming the file in the _config.yml generally makes it
easier to find.
Using a different LaTeX engine¶
The current default is to use xelatex to build pdf files.
警告
The --individualpages option currently uses pdflatex by default.
Some users may want to switch to using a different LaTeX engine such as pdflatex.
To revert the LaTeX engine to pdflatex you can add the following to your _config.yml
latex:
latex_engine: pdflatex
注解
The Sphinx documentation for available builders
contains a full list of supported latex builders.
Other Sphinx LaTeX settings¶
Other LaTeX settings available
to Sphinx can be passed through using the config section
of Sphinx in the _config.yml file for your project.
For example, if you would like to set the latex_toplevel_sectioning option to use part instead of chapter you would use:
sphinx:
config:
latex_toplevel_sectioning: 'part'