-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from timvink/version2
Version2
- Loading branch information
Showing
34 changed files
with
1,701 additions
and
198 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
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
This file was deleted.
Oops, something went wrong.
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,60 @@ | ||
# Customize the print site banner | ||
|
||
When a user visits the print page, it might not be immediately obvious how to use it. You can set the `add_print_site_banner` option to `true` to add a banner to the top of the HTML print page that will be hidden when printing. | ||
|
||
You might want to customize this banner, for example by translating it to your language. You can do that by specifying the path to a custom banner template in the `mkdocs.yml` file. This file should be a standard [jinja2 template](https://jinja.palletsprojects.com/en/2.11.x/templates/) where you can combine HTML and jinja2 variables. The information specified in `mkdocs.yml` will already by available as jinja2 variables (see [mkdocs project information](https://www.mkdocs.org/user-guide/configuration/#project-information)). | ||
|
||
_Example_: | ||
|
||
=== "mkdocs.yml" | ||
|
||
```yaml | ||
plugins: | ||
- print-site: | ||
add_print_site_banner: true | ||
print_site_banner_template: "docs/assets/templates/custom_banner.tpl" | ||
``` | ||
|
||
=== "docs/assets/templates/custom_banner.tpl" | ||
|
||
```jinja | ||
<p> | ||
<em>This box will disappear when printing</em> | ||
<span style="float: right"><a href="https://timvink.github.io/mkdocs-print-site-plugin/">mkdocs-print-site-plugin</a></span> | ||
</p> | ||
<p>This page has combined all site pages into one. You can export to PDF using <b>File > Print > Save as PDF</b>.</p> | ||
<p>See also [export to PDF](https://timvink.github.io/mkdocs-print-site-plugin/how-to/export-PDF.html) and [export to standalone HTML](https://timvink.github.io/mkdocs-print-site-plugin/how-to/export-HTML.html).</p> | ||
``` | ||
|
||
As an example, have a look at the default [print_site_banner.tpl](https://github.com/timvink/mkdocs-print-site-plugin/tree/master/mkdocs_print_site_plugin/templates/print_site_banner.tpl). | ||
|
||
## Adding configurable content | ||
|
||
You might want to add some content to your cover page that's not yet specified in your `mkdocs.yml` file. | ||
Of course you could just hard-code it in your custom template file, but you could also make use of MkDocs's [extra context](https://www.mkdocs.org/user-guide/custom-themes/#extra-context) feature, allowing you to use custom variables from your config file with `{{ config.extra.<your variable> }}`. | ||
|
||
_Example_: | ||
|
||
=== "mkdocs.yml" | ||
|
||
```yaml | ||
plugins: | ||
- print-site: | ||
add_print_site_banner: true | ||
print_site_banner_template: "docs/assets/templates/custom_banner.tpl" | ||
|
||
extra: | ||
banner_message: "Save this page using File > Print > Save as PDF" | ||
``` | ||
|
||
=== "docs/assets/templates/custom_banner.tpl" | ||
|
||
```jinja | ||
<p> | ||
<em>This box will disappear when printing</em> | ||
<span style="float: right"><a href="https://timvink.github.io/mkdocs-print-site-plugin/">mkdocs-print-site-plugin</a></span> | ||
</p> | ||
<p>{{ config.extra.banner_message }}</p> | ||
``` | ||
|
||
|
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
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, styling and scripts 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 'title' | ||
``` | ||
|
||
??? 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(); | ||
})(); | ||
``` |
Oops, something went wrong.