forked from timvink/mkdocs-print-site-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update documentation with info on PDF and HTML export
- Loading branch information
Showing
14 changed files
with
1,358 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
|
||
docs/assets/*.pdf | ||
|
||
# nodejs stuff | ||
package-lock.json | ||
package.json | ||
|
||
.vscode/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Export to HTML | ||
|
||
After enabling the `print-site` plugin in your `mkdocs.yml`, you will have your entire site combined into a single page. That allows you to create a standalone HTML page: a single self-contained file that has all images and script embedded. This means you could send a site as an email attachment, a use case common within companies where deploying static sites might be more involved. | ||
|
||
In order to create a self-contained, standalone HTML file from the print page, we will need to embed images, CSS and javascript using [data URLs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs). We can do this quite easily using the [htmlark](https://github.com/BitLooter/htmlark) python package: | ||
|
||
|
||
```shell | ||
pip install http html5lib requests | ||
pip install htmlark | ||
``` | ||
|
||
To create the export: | ||
|
||
```shell | ||
mkdocs build | ||
htmlark site/print_page.html -o standalone.html | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# 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 a headless chrome plugin. | ||
|
||
## Automated export using nodejs | ||
|
||
We can use [nodejs](https://nodejs.org/en/) together with the [puppeteer](https://github.com/puppeteer/puppeteer) headless chrome node.js package: | ||
|
||
- Install [nodejs](https://nodejs.org/en/) | ||
- 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) and `title` arguments: | ||
|
||
```shell | ||
node exportpdf.js http://localhost:8000/print_page.html out.pdf 'your website name' | ||
``` | ||
|
||
??? example "export_to_pdf.js" | ||
|
||
```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 | ||
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(); | ||
})(); | ||
``` |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.