Skip to main content

txt2pdf : Convert Markdown files to PDF with styles.

Initially forked from https://github.com/jmaupetit/md2pdf

Installation

Available through pip:

$ pip install txt2pdf

Usage

As a CLI

Usage:
    txt2pdf [options] INPUT.MD OUTPUT.PDF

Options:
    --css=STYLE.CSS

For example, try to generate the project documentation with:

$ txt2pdf README.md README.pdf

Optionally, you may load an external style (restricted to CSS2):

$ txt2pdf --css tests/resources/input.css README.md README.pdf

You may also include a HTML header and footer :

$ txt2pdf --header tests/resources/header.html README.md README.pdf
$ txt2pdf --footer tests/resources/footer.html README.md README.pdf

Optional debug by printing HTML to stdout, includes optional header and footer :

$ txt2pdf --html-to-stdout README.md README.pdf

As a library

from txt2pdf.core import txt2pdf

txt2pdf(
    pdf_file_path,
    md_content=None,
    md_file_path=None,
    css_file_path=None,
    html_header_content=None,
    html_header_file_path=None,
    html_footer_content=None,
    html_footer_file_path=None,
    base_url=None,
    print_html_to_stdout=False,
)

Function arguments:

  • pdf_file_path: output PDF file path
  • md_content: input markdown raw string content
  • md_file_path: input markdown file path
  • css_file_path: input styles path (CSS)
  • html_header_content: input HTML header raw string content
  • html_header_file_path: input HTML header file path
  • html_footer_content: input HTML footer raw string content
  • html_footer_file_path: input HTML footer file path
  • base_url: absolute base path for markdown linked content (as images)
  • print_html_to_stdout: print HTML to stdout for debug purposes

With Docker

Install Docker

Build the image:

$ docker build -t txt2pdf .

Now run your image:

$ docker run --rm -v $PWD:/app txt2pdf --css styles.css INPUT.MD OUTPUT.PDF

Troubleshooting on MacOSX

Ensure, Weasyprint is fully functional before using txt2pdf. You will find installation instructions in the project documentation: https://weasyprint.readthedocs.io/en/latest/install.html

In a few words, here are the few steps you will need to follow:

$ brew install cairo pango gdk-pixbuf libxml2 libxslt libffi

Misc

Using custom fonts in styles

WeasyPrint does not support the @font-face property yet (see project issue 28). If you use want to use custom fonts, you should use system fonts and define them with the font-family CSS property, like:

font-family: 'Neutraface Condensed';

Note that you should only define one single custom font, not a substitution list.

Using code blocks

Generate default css defining a class for every code item type:

(venv) $ pygmentize -S default -f html -a .codehilite

Contributing

Hacking

Clone this project first:

$ git clone git@github.com:c4ffein/txt2pdf.git

Install it with its dependencies (ideally in a virtual environment):

$ cd txt2pdf
$ python -m venv venv
$ . venv/bin/activate
(venv) $ pip install .[dev]

Running the test suite

To run the test suite with your active python version (virtual environment):

(venv) $ pytest

Lint the code via:

(venv) $ black -l 120

Release a new version

(venv) $ pip install .[deploy]  # install flit if needed
(venv) $ flit publish --format sdist

License

txt2pdf is released under the MIT License. See the LICENSE file for details.

Metadata

Release files for txt2pdf 0.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for txt2pdf 0.7.1
File Size Uploaded Provenance
txt2pdf-0.7.1.tar.gz 14.1 kB Without provenance Details

Release files / txt2pdf-0.7.1.tar.gz

Download URL txt2pdf-0.7.1.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
29c9d448bca248f990d577d19dcdd1b72cf4c2b95cdaa96e9f0de0a82d35780c
BLAKE2b-256 checksum
How to use checksums
43e4363024599ba8d94b6061f82932f00aad934158186d5fc5bcd2bfb14362b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Security for txt2pdf 0.7.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page