Docusaurus PDF Generator

open source Apr 8, 2022

Synopsis

When I have the time, I have been helping out with the Meshtastic project. If you don't know what it is, stop reading this and check it out! The Meshtastic project documentation is served by the software Docusaurus. There was a request on the Meshtastic discord server about the possibility of converting the documentation to a PDF hanbook. I have built PDF generators for other projects, so I decided to take on the task.

PDF Generator

Instead of reinventing the wheel and coding right away, I did a quick a internet search for a project that fulfills my needs. I came across docusaurus-prince-pdf. It is a simple script based on Prince PDF that creates a map of any docusaurus instance, scrapes the HTML and generates a PDF. After reviwing the code, I decided to fork the project and re-write it to use wkhtmltopdf for the PDF generating engine instead. I don't like the fact that Prince PDF is a closed source project. That is why I decided to switch to something else and wkhtmltopdf was the natural choice. The reasons were simple: I already have some prior experience using it in different projects, the project is open source and very well documented.

Docusausrus-Wkhtmltopdf

After refactoring the 99% of the code, I integrated wkhtmltopdf as the PDF generator engine. You can play with the source code in my GitHub Repo. I have also published to to npm npm version

Usage

Install Wkhtmltopdf first.

Run the following commands to generate PDF:

# Genrate PDF from specific site under `docs` scope
npx docusaurus-wkhtmltopdf -u https://meshtastic.org/docs/getting-started

# Change generating scope to `/docs/xxx/`
npx docusaurus-wkhtmltopdf -u https://meshtastic.org/docs/xxx

# Custom working (output) directory
npx docusaurus-wkhtmltopdf -u https://meshtastic.org/docs/getting-started --dest ./pdf-output

# Custom output file name
npx docusaurus-wkhtmltopdf -u https://meshtastic.org/docs/getting-started --output docs.pdf

To generate PDF from a local Docusaurus instance. You need to first build the site locally:

# Build the site
yarn build

# Serve built site locally
yarn serve

# Generate PDF from local Docusaurus instance
npx docusaurus-wkhtmltopdf -u http://localhost:4000/docs # Change port to your serving port

See help screen for more usages:

npx docusaurus-wkhtmltpdf -h

Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.