/markdown-to-pdf

A script to convert Markdown to PDF

Primary LanguagePython

Markdown to PDF

This repository contains a simple script that converts Markdown and CSS into PDF documents. The heavy lifting is done by Python-Markdown, a tool for generating HTML from Markdown, and WeasyPrint, a tool for generating PDF documents from HTML and CSS. With this script, my CV in Markdown is converted into a PDF document.

Getting Started

  1. Install pyenv, a tool for managing Python versions.
  2. The file .python-version in the root folder specifies the Python version required by the conversion script. Navigate to the root folder and execute pyenv install to install this Python version.
  3. Install cairo, pango, and gdk-pixbuf. On macOS, these tools can be installed with brew.
  4. Install pipenv by executing pip install pipenv. There's an issue related to language and region settings that you might run into on Macs, but it's easy to resolve.
  5. Create a new virtual environment with all dependencies by executing pipenv install --dev --ignore-pipfile. The flag ignore-pipfile is used to indicate that the exact versions of the dependencies as specified in Pipfile.lock should be installed. The flag dev is used to also install development dependencies.

Activating the virtual environment

Before executing any of the commands below, you need to activate the virtual environment. You can do so by executing pipenv shell. Your command prompt should now indicate that you've activated the virtual environment. It can be deactivated by executing exit.

Generating PDFs

The examples folder contains a CV and a quotation.

  • Execute python md2pdf.py convert examples/cv.md examples/cv.css to generate a PDF version of the CV.
  • Execute python md2pdf.py convert examples/quotation.md examples/quotation.css to generate a PDF version of the quotation.

When writing or designing documents, you can use python md2pdf.py watch <document_name>.md <stylesheet_name>.css. This will watch the given files and generate new output when they change. Both an HTML document and a PDF document are generated. The HTML document is generated because it's convenient to be able to play around with the CSS in a browser.

CSS for print

If you want to design your own documents, take a look at designing for print with CSS by Rachel Andrew.