Export to PDF↵
After enabling the print-site plugin in your mkdocs.yml, exporting to PDF is as simple as browsing to /print_page or /print_page.html (url depends on your mkdocs setting use_directory_urls). From your browser you can use File > Print > Save as PDF.
If you want to automatically create PDFs of your mkdocs website, you can automate the process using headless chrome (see below).
To change page numbers, page size or page breaks, see customize the PDF layout. If you need even more control over the PDF layout, have a look at mkdocs-exporter or mkdocs-to-pdf.
Avoid creating PDFs with Firefox
Use Chrome or another Chromium-based browser (such as Edge) to create PDFs. Firefox has these issues:
- Print margins can cut off content, and anchor links may not work. See mkdocs-print-site-plugin#56.
- Firefox does not support CSS page margin boxes (bug 856371), so the page numbers of the plugin do not show.
- The headers and footers of Firefox (Print headers and footers in the print dialog) are drawn at the edge of the paper, and CSS cannot move them. To move them inward, open
about:configand setprint.print_edge_top,print.print_edge_bottom,print.print_edge_leftandprint.print_edge_right. The values are in hundredths of an inch, for example40.
PDF bookmarks↵
A PDF can have bookmarks (also called an outline): a clickable table of contents that PDF viewers show in their sidebar. The print page sets the heading levels so the bookmarks follow your site navigation, with each page nested under its section.
Whether the PDF gets bookmarks depends on the tool that creates it. With Chrome on the command line, add --generate-pdf-document-outline:
google-chrome --headless --no-pdf-header-footer --generate-pdf-document-outline \
--print-to-pdf=out.pdf http://localhost:8000/print_page.html
With puppeteer, use the outline: true option (see the script below).
Automated export using nodejs and chrome↵
We can use nodejs together with the puppeteer headless chrome node.js package:
- Install nodejs
- Download puppeteer in the root of your project using the node package manager:
npm i --save puppeteer - Save the script
export_to_pdf.js(see below) in the root of your project - Start a webserver with your site (
mkdocs serve) - In a separate terminal window, you can now run the PDF export with
url(to your print page),pdfPath(name of output file) andtitlearguments:
node export_to_pdf.js http://localhost:8000/print_page.html out.pdf 'title'
export_to_pdf.js
// Credits for script: https://github.com/majkinetor/mm-docs-template/blob/master/source/pdf/print.js
// Requires: npm i --save puppeteer
const puppeteer = require('puppeteer');
var args = process.argv.slice(2);
var url = args[0];
var pdfPath = args[1];
var title = args[2];
console.log('Saving', url, 'to', pdfPath);
// date – formatted print date
// title – document title
// url – document location
// pageNumber – current page number
// totalPages – total pages in the document
headerHtml = `
<div style="font-size: 10px; padding-right: 1em; text-align: right; width: 100%;">
<span>${title}</span> <span class="pageNumber"></span> / <span class="totalPages"></span>
</div>`;
footerHtml = ` `;
(async() => {
const browser = await puppeteer.launch({
headless: true,
executablePath: process.env.CHROME_BIN || null,
args: ['--no-sandbox', '--headless', '--disable-gpu', '--disable-dev-shm-usage']
});
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle2' });
await page.pdf({
path: pdfPath, // path to save pdf file
format: 'A4', // page format
displayHeaderFooter: true, // display header and footer (in this example, required!)
printBackground: true, // print background
outline: true, // add PDF bookmarks from the headings
landscape: false, // use horizontal page layout
headerTemplate: headerHtml, // indicate html template for header
footerTemplate: footerHtml,
scale: 1, //Scale amount must be between 0.1 and 2
margin: { // increase margins (in this example, required!)
top: 80,
bottom: 80,
left: 30,
right: 30
}
});
await browser.close();
})();