Skip to main content

Markdown to pdf renderer

Project description

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:

  • Use UTF-8 encoded text in markdown in any language
  • Embed images used in markdown
  • Break text into pages in the desired order
  • Create a TableOfContents (bookmarks) from markdown headings
  • Tune the necessary elements using your CSS code
  • Use different page sizes within single pdf
  • Create tables in markdown

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>|
"""

css = "table, th, td {border: 1px solid black;}"

pdf.add_section(Section(text), user_css=css)

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.

  • toc: whether to include the headers <h1> - <h6> of this section in the TOC. Default is True.
  • root: the name of the root directory from which the image file paths starts in markdown. Default ".".
  • paper_size: name of paper size, as described here. Default "A4".
  • borders: size of borders. Default (36, 36, -36, -36).

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

  • creationDate: current date
  • modDate: current date
  • creator: "PyMuPDF library: https://pypi.org/project/PyMuPDF"
  • producer: ""
  • title: ""
  • author: ""
  • subject: ""
  • keywords: ""

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown_pdf-1.3.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

markdown_pdf-1.3.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file markdown_pdf-1.3.1.tar.gz.

File metadata

  • Download URL: markdown_pdf-1.3.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.10

File hashes

Hashes for markdown_pdf-1.3.1.tar.gz
Algorithm Hash digest
SHA256 125d25240acc9c68a02df11b5249b301eea0dffdb0e13ed77ed5f387e1f22fa4
MD5 1615f322015f5c16beecc00a290de06f
BLAKE2b-256 1ed1cb887e9eaa382f04e520c6893b9ab475f7f6db65b439b1293a0fc1b6b957

See more details on using hashes here.

File details

Details for the file markdown_pdf-1.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for markdown_pdf-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 877674b0cd93b96d62024e564862461172faf15344a8bfffb0a7fbc37b18ba3d
MD5 0b7637293b84b4a5990791d6cbe3466b
BLAKE2b-256 9e7093c7af924be07046ac0652352d2bde93f36fd55b40cac4f155e7eea555ff

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page