A backend for mdBook written in Rust for generating PDF based on headless chrome and Chrome DevTools Protocol.
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, 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 --recursive 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.
chromium
or chromium-browser
(Note: for Ubuntu later than 18.04, you have to install chromium-browser
through snap
).Make sure the following exists in your book.toml
:
```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
).
toml
[output.html.print]
enable = true
A simplest book.toml
is as follows:
```toml [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
.
(Note: Because I found that special characters such as :
are very likely to appear in the title of the book, which will cause the generation of the related PDF file to fail, so the file name does not use the form of <book name>.pdf
but 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]
.
This project relies on headlesschrome. 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-headlesschrome 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.