A backend for mdBook written in Rust for generating PDF based on headless chrome and Chrome DevTools Protocol.
Since it's a plugin (backend) for mdBook, first of all you should ensure that mdbook
is available.
If your machine's architecture is x86_64
, or you are using Linux for ARM64
, check the successful build GitHub Actions workflows, 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
).
Then 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, have Google Chrome / Chromium / Microsoft Edge available (installed at the default location, in PATH or binary location configured) as currently, automatically downloading Chromium binary isn't available (will update once upstream fixes such support).
- 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
chromium
orchromium-browser
(Note: for Ubuntu later than 18.04, you have to installchromium-browser
throughsnap
).
Make sure the following exists in your 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
.
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]
.
- Support for Firefox in
mdbook-pdf
!
Currently, according to Puppeteer's documentation, Chrome DevTools Protocol Page.printToPDF is only supported in Chrome headless. An issue has already been filed for this here.
- Broken Table of Contents links!
I've already submitted a PR for mdBook to fix this by making print page (print.html) links link to anchors on the print page, but it's not merged yet. You can try my PR fork for this to work.
- Can you add the bookmark to the PDF reflecting the Table of Contents, just like what wkhtmltopdf supported?
This should be realized by Chromium, and an issue has already been filed for this here.
- Failed to render my book for PDF in
mdbook-pdf
!
Will appreciate if you can report it to the issue tracker providing all the traces for mdbook-pdf
rendering.
RUST_BACKTRACE=full RUST_LOG=trace mdbook build
This project relies on headless_chrome. Because the new version has not been released, and the default timeout is not friendly to PDF generation, I use my Fork version to published mdbook-pdf-headless_chrome for expanding the relevant timeout to 300 seconds as a submodule of this project, thus enabling the project to be published on Crates.io as well.