Print PDFs from Markdown Files using Weasyprint
Project description
md2weasypdf
Print PDFs from Markdown files and a HTML layout using Weasyprint.
Installation
pip install md2weasypdf
Usage
python -m md2weasypdf <input_folder_or_file> <output_path>
When a layout is not specified in the files frontmatter (see below), the --layout option has to be passed.
Watch Mode
The watch mode is intended for creation of layouts. The given layouts directory and input directory will be watched for changes.
For VSCode the extension vscode-pdf can be recommended, as it refreshes the displayed PDF automatically.
python -m md2weasypdf <input_folder_or_file> <output_path> --watch
Input
Input files are expected in markdown format with several markdown extensions. The markdown documents can utilize Jinja2 for templating inside the document (e. g. reusing texts).
Bundling
The bundling feature allows to bundle multiple documents into one PDF. This is useful when you want to create one PDF file from multiple source files. The bundling feature is enabled by adding the --bundle flag to the command. The specified input folder will be searched recursively for *.md files, files starting with an underscore will be ignored.
When using the bundle option, a layout has to be specified using --layout and a title for the whole document using --title.
Options
YAML Frontmatter can be used to customize the document layout or add other options which will be passed to the template. The following example shows how a document with frontmatter section could look like:
---
title: My Document Title
layout: doc1
---
Lorem ipsum...
Templating
Markdown files may contain Jinja2 templating, such as including other files.
In addition to just markdown files, yaml files are also rendered when there is a _template.md file present in the same or parent folder, which then uses Jinja2 to render its contents with values passed from the yaml file.
Output / Layout
The document layout must be given via the command option --layout or in the frontmatter of the single file. As layout a directory name inside the ./layouts directory (default, can be changed using --layouts-dir) is expected. In the layout directory, a index.html.j2 or index.html file is expected, which is loaded as entrypoint. The file is parsed using Jinja2.
Variables
Document
A document is, when --bundle option is used, a collection of articles, otherwise contains just one article.
The following variables are passed to the Jinja2 renderer:
date: current date in ISO formatcommit: the current commit (with suffix-dirtywhen the working directory has uncommitted changes)articles: list of articles, will be a list of only one article when not using--bundleoptiontitle: the current filename of the article stripped by it's suffix, values in braces()removed and underscores_replaced with spaces; or the value passed in--title(required and used value when--bundleoption is set)meta: metadata as provided in the articles frontmatter and/or as passed in--meta(will be combined with frontmatter taking precedence)
Article
The article represents the single file which is used as input.
It has the following attributes for use in the document rendering:
title: see abovecontentrendered HTML, use with| savein Jinja2 to prevent unwanted escapingsourcepath to the filemetasee abovehashgit object hash of the file or, when other files were included in the article, a new hash over all used filesmodified_datedate of last modification of the file or, when other files were included in the article, the latest date of all used fileshas_custom_headlineindicates if the document starts with an h1 level heading
Markdown Extensions
Table of Contents
Insert a table of contents using [TOC]. The table of contents will be generated automatically based on the headlines (lines starting with one or multiple #) in the document.
Which levels of headlines should be included in the TOC can be defined by declaring toc_depth in meta passed or the articles frontmatter.
Table of Abbreviations
Insert a table of abbreviations using [TOA]. The table of abbreviations will be generated automatically based on defined abbreviations (using *[Abbreviation]: Explanation) in the document.
Footnotes
Footnotes let you reference relevant information without disrupting the flow of what you're trying to say:
Here's a simple footnote,[^1] and here's a longer one.[^bignote]
[^1]: This is the first footnote.
[^bignote]: Here's one with multiple paragraphs and code.
Indent paragraphs to include them in the footnote.
`{ my code }`
Add as many paragraphs as you like.
It is possible to reference to the same footnote by using the same footnote label.
Subscript
Use tildes ~ around text to create a subscript formatting.
Checkboxes
Use [ ] to create a checkbox. Use [x] to mark a checkbox as checked.
Fields
Use [>input_id] to create a text input. To create a textarea, add |textbox after the input id. To create a date field, add |YYYY-MM-DD after the input id.
To add a placeholder, append the placeholder text within parens to the end of the input id: [>input_id] (placeholder text).
Mermaid.js
Mermaid.js can be used in code blocks with the language mermaid. To convert the mermaid code into an image, mermaid-cli is required to be installed on the system.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file md2weasypdf-0.0.49.tar.gz.
File metadata
- Download URL: md2weasypdf-0.0.49.tar.gz
- Upload date:
- Size: 27.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74792cf1d4d719cb063f97034a03307a3363f9b9aa4f7d157ee6810443fac818
|
|
| MD5 |
dd892d396ac20c349794fd27d407b16b
|
|
| BLAKE2b-256 |
87e39582e98e94c1c80ea7e96903d51455e91f3b0946ab39c10af2a0ea618c60
|
Provenance
The following attestation bundles were made for md2weasypdf-0.0.49.tar.gz:
Publisher:
pypi_publish.yml on mstingl/md2weasypdf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
md2weasypdf-0.0.49.tar.gz -
Subject digest:
74792cf1d4d719cb063f97034a03307a3363f9b9aa4f7d157ee6810443fac818 - Sigstore transparency entry: 1202870205
- Sigstore integration time:
-
Permalink:
mstingl/md2weasypdf@fd75871e95c6553b45bc3893d29e0771b4e6ddf7 -
Branch / Tag:
refs/tags/0.0.49 - Owner: https://github.com/mstingl
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi_publish.yml@fd75871e95c6553b45bc3893d29e0771b4e6ddf7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file md2weasypdf-0.0.49-py3-none-any.whl.
File metadata
- Download URL: md2weasypdf-0.0.49-py3-none-any.whl
- Upload date:
- Size: 28.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff9010c382e68bdd9c08faf394ff47b5c12a6332caaa3d07203bd3167d7b88ac
|
|
| MD5 |
c976e109b789db07771b07cc0eb2b628
|
|
| BLAKE2b-256 |
4f8e64086845eb8fe54b6e7ea92211581d3768ebba11306284e813f4ad980ff1
|
Provenance
The following attestation bundles were made for md2weasypdf-0.0.49-py3-none-any.whl:
Publisher:
pypi_publish.yml on mstingl/md2weasypdf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
md2weasypdf-0.0.49-py3-none-any.whl -
Subject digest:
ff9010c382e68bdd9c08faf394ff47b5c12a6332caaa3d07203bd3167d7b88ac - Sigstore transparency entry: 1202870211
- Sigstore integration time:
-
Permalink:
mstingl/md2weasypdf@fd75871e95c6553b45bc3893d29e0771b4e6ddf7 -
Branch / Tag:
refs/tags/0.0.49 - Owner: https://github.com/mstingl
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi_publish.yml@fd75871e95c6553b45bc3893d29e0771b4e6ddf7 -
Trigger Event:
release
-
Statement type: