A backend for mdBook written in Rust for generating PDF based on headless chrome and Chrome DevTools Protocol. (用 Rust 编写的 mdBook 后端,基于headless chrome和Chrome开发工具协议生成PDF)
mdbook-pdf
A backend for mdBook written in Rust for generating PDF based on headless chrome and Chrome DevTools Protocol.
Installation & Usage
Since it's a plugin (backend) for mdBook, first of all you should ensure thatmdbook is available.
If your machine's architecture is x8664, or you are using Linux for ARM64, check the successful build GitHub Actions workflows/release, click into the latest one, and then you can get a binary from the Artifacts (including Windows, Linux, macOS).
Otherwise, make sure the rust compiling environment is available, execute cargo install mdbook-pdf to compile and install.
If you want to compile the latest version, make sure the Rust build environment is available (cargo build). Run cargo install --git https://github.com/HollowMan6/mdbook-pdf.git, or alternatively, you can clone the repository and compile it yourself. (Run git clone https://github.com/HollowMan6/mdbook-pdf.git, in the cloned folder, run cargo build --release , get the executable in target/release/, and put it in PATH)
For running, please have Google Chrome / Chromium / Microsoft Edge already available (installed at the default location, in PATH or binary location configured). If not, and mdbook-pdf has the fetch feature enabled (It is not enabled by default, you need to use cargo install mdbook-pdf --features fetch to recompile for enabling), the program will try to automatically download the Chromium browser and run it (Note: if you are on Linux, there may be problems if chromium dependencies are not satisfied / using non-x86_64 architectures).
- On Windows 10 and above, the program can generate PDF normally without installing any additional software, because Microsoft Edge is the browser provided with Windows system. Of course, considering the support for the older versions of Windows without Edge, you can install Google Chrome on your computer.
- In MacOS, you need to install Google Chrome / Microsoft Edge or Chromium.
- In Linux, you can choose to install any of the Google Chrome / Chromium / Microsoft Edge browsers. It is recommended to install Chromium. The name of this software package in your Linux distribution is commonly
chromiumorchromium-browser(Note: for Ubuntu later than 18.04, you have to installchromium-browserthroughsnap).
book.toml:
[output.html]
[output.pdf]
And also [output.html.print] is not disabled (it should be enabled by default, so don't worry if the following lines doesn't occur in you book.toml).
[output.html.print]
enable = true
A simplest book.toml is as follows:
[book]
title = "An Example"
[output.html]
[output.pdf]
Finally you can build your book and get the PDF file with mdbook build command, your PDF file will be available at book/pdf/output.pdf.
Run with Docker
You can also use this docker image.docker run --rm -v /path/to/book:/book hollowman6/mdbook-pdf
If your book have other Rust dependencies, you can install them on your local machine (if using Linux), or if you are not using Linux, download the Linux executables of corresponding architecture to a dir, replace ~/.cargo/bin with your path.
docker run --rm -v /path/to/book:/book -v ~/.cargo/bin:/mdbook hollowman6/mdbook-pdf
Configuration
Support customize PDF paper orientation, scale of the webpage rendering, paper width and height, page margins, generated PDF page ranges, whether to display header and footer as well as customize their formats, and more.Check book.toml and comments for details for the available configurations of [output.pdf].
Common Issues
- Support for Firefox in
mdbook-pdf!
- Broken links!
mdbook (v0.5.0+).
If you have relative links that link outside the book, please provide the static hosting site URL for it to get fixed.
Can you add the bookmark to the PDF reflecting the Table of Contents, just like what wkhtmltopdf is supported?
v0.1.11+, you can control it by the generate-document-outline option.
[!NOTE]
If you dislike the Chromium generated Table of Contents, we also have support for the bookmark/outline of the PDF file (mdbook-pdf-outline). It is written in Python and is another backend for>mdbookand should be used together withmdbook-pdf.
You can install this backend by pip install mdbook-pdf-outline.
>
Remember to put the following to the end of your>book.toml, after [output.pdf], and disable thegenerate-document-outlineoption:
> > [output.pdf-outline] > >> generate-document-outline = false
If you prefer to use the table of content just like the one generated by>wkhtmltopdf(generate entries based on the headings), you can leave thebook.tomlas it is.
If you want to use the table of content just like the one shown in the>print.htmlpage for PDF file, you can turn off thelike-wkhtmltopdfoption by using the following to yourbook.toml. (Note: This is only available for mdbook version that is before 0.5.0 using the modified mdbook for fixing the broken links inprint.html, which can be installed bycargo install --git https://github.com/HollowMan6/mdBook mdbookinstead):
> > [output.pdf-outline] > like-wkhtmltopdf = false > >> generate-document-outline = false
Finally, you can find the outlined version at book/pdf-outline/output.pdf.
- Force page breaks in the markdown source that is respected by mdbook-pdf!
<div style="page-break-before:always"> </div>
<p></p>
- Failed to render my book for PDF in
mdbook-pdf!
mdbook-pdf rendering as well as your book.toml. You can also provide the link to your book's repository if it's open source.
RUSTBACKTRACE=full RUSTLOG=trace mdbook build