vb64 / markdown-pdf

Markdown to pdf renderer
MIT License
22 stars 2 forks source link
markdown markdown-it pdf pymupdf

Module markdown-pdf

GitHub Workflow Status GitHub Workflow Status Codacy Badge Codacy Badge PyPI - Downloads

The free, open source Python module markdown-pdf will create a PDF file from your content in markdown format.

When creating a PDF file you can:

The module utilizes the functions of two great libraries.

Installation

pip install markdown-pdf

Usage

Create a pdf with TOC (bookmarks) from headings up to level 2.

from markdown_pdf import MarkdownPdf

pdf = MarkdownPdf(toc_level=2)

Add the first section to the pdf. The title is not included in the table of contents.

from markdown_pdf import Section

pdf.add_section(Section("# Title\n", toc=False))

Add a second section. In the pdf file it starts on a new page. 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.

pdf.add_section(
  Section("# Head1\n\n![python](img/python.png)\n\nbody\n"),
  user_css="h1 {text-align:center;}"
)

Add a third 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.

pdf.add_section(Section("## Head2\n\n### Head3\n\n", paper_size="A4-L"))

Add a fourth section with a table.


text = """# Section with Table

|TableHeader1|TableHeader2|
|--|--|
|Text1|Text2|
|ListCell|<ul><li>FirstBullet</li><li>SecondBullet</li></ul>|
"""

pdf.add_section(Section(text))

Set the properties of the pdf document.

pdf.meta["title"] = "User Guide"
pdf.meta["author"] = "Vitaly Bogomolov"

Save to file.

pdf.save("guide.pdf")

Pdf

Settings and options

The Section class defines a portion of markdown data, which is processed according to the same rules. The next Section data starts on a new page.

The Section class can set the following attributes.

The following document properties are available for assignment (dictionary MarkdownPdf.meta) with the default values indicated.

Example

As an example, you can download the pdf file created from this md file. This Python script was used to create the PDF file.