Create a compressed pdf with TOC (bookmarks) from headings up to level 2.
from markdown_pdf import MarkdownPdf
pdf = MarkdownPdf(toc_level=2, optimize=True)
Add the first section to the pdf. The title is not included in the table of contents.
After adding a section to a pdf, the page_count property in the section contains the number of pdf pages created for the added section.
Add a second section with external and internal hyperlinks.
In the pdf file it starts on a new page.
text = """# Section with links
- [External link](https://github.com/vb64/markdown-pdf)
- [Internal link to Head1](#head1)
- [Internal link to Head3](#head3)
"""
pdf.add_section(Section(text))
Add a third section.
The title is centered using CSS, included in the table of contents of the pdf file, and an image from the file img/python.png is embedded on the page.
Add a next section. Two headings of different levels from this section are included in the TOC of the pdf file.
The section has landscape orientation of A4 pages.
The module allows you to process specially marked sections of code using plugins.
For example, you convert the following Markdown text to PDF:
# Title plantuml
Document with plantuml code.
```plantuml
@startuml
Alice -> Bob: Hello Bob
Bob --> Alice: Hi!
@enduml
```
End of document
Without using the plugin, you will get the following result in pdf:
You can use a plugin to render plantuml code into an image.
from markdown_pdf import MarkdownPdf, Section
from markdown_pdf.pligins import Plugin
plantuml_text = """# Title plantuml
Document with plantuml code.
```plantuml
@startuml
Alice -> Bob: Hello Bob
Bob --> Alice: Hi!
@enduml
```
End of document
"""
plugins = {
Plugin.Plantuml: {'url': 'http://www.plantuml.com/plantuml/img/'}
}
pdf = MarkdownPdf(plugins=plugins)
pdf.add_section(Section(plantuml_text))
pdf.save("plantuml.pdf")
In this case, the plugin will send the code marked as plantuml to the specified internet server
and replace the code text with an image generated by the server www.plantuml.com.
In the created file plantuml.pdf, you will see the following result:
You can find a more detailed description of the plugins here.
Command Line Interface script
As an example, you can download the pdf file created from this README.
This Python script was used to create the PDF file.
You can create your own CLI script, tailored to your needs, using this script as a starting point.
Development
git clone git@github.com:vb64/markdown-pdf.git
cd markdown-pdf
make setup PYTHON_BIN=/path/to/python3
make tests
Alternatives
Use instead of markdown-pdf: tools that do the same job, closest first.