Skip to content

Instantly share code, notes, and snippets.

Last active Nov 21, 2016
What would you like to do?
md2pdf - Command line Markdown to PDF converter with support for CSS stylesheets and custom fonts
#!/usr/bin/env python
import os
import sys
from markdown2 import markdown
from xhtml2pdf import pisa
## Inspired by
## Documentation of underlying tools
Required packages: markdown2, xhtml2pdf, html5lib
def main(md_file, css_file):
# read css file
from xhtml2pdf.default import DEFAULT_CSS
if css_file is not None:
with open(css_file) as f:
# read markdown
with open(md_file) as f:
md =
# to html
html = markdown(md, extras=DEFAULT_EXTRAS)
base_with_path = os.path.splitext(sys.argv[1])[0]
basename = os.path.basename(base_with_path)
with open('%s.pdf'%basename, "wb") as fp:
# convert HTML to PDF
pisa.CreatePDF(html, dest=fp, default_css=DEFAULT_CSS)
if __name__ == '__main__':
if len(sys.argv) == 1:
print('Usage: md2pdf style.css')
print(' You can also set a CSS file as env var.')
print(' E.g. export MD2PDF_CSS=style.css')
if len(sys.argv) == 2:
css = os.getenv('MD2PDF_CSS')
if len(sys.argv) == 3:
css = sys.argv[2]
main(sys.argv[1], css)

This comment has been minimized.

Copy link

@srugano srugano commented Nov 8, 2014

When I put a css file, I have

python style.css

Traceback (most recent call last): File "", line 63, in <module> main(sys.argv[1], css) File "", line 50, in main pisa.CreatePDF(html, dest=fp, default_css=DEFAULT_CSS) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/", line 89, in pisaDocument encoding, context=context, xml_output=xml_output) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/", line 57, in pisaStory pisaParser(src, context, default_css, xhtml, encoding, xml_output) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/", line 685, in pisaParser context.parseCSS() File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/", line 499, in parseCSS self.cssDefault = self.cssParser.parse(self.cssDefaultText) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/", line 434, in parse src, stylesheet = self._parseStylesheet(src) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/", line 538, in _parseStylesheet src, ruleset = self._parseRuleset(src) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/", line 835, in _parseRuleset src, selectors = self._parseSelectorGroup(src) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/", line 846, in _parseSelectorGroup src, selector = self._parseSelector(src) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/", line 860, in _parseSelector src, selector = self._parseSimpleSelector(src) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/", line 914, in _parseSimpleSelector src, selector = self._parseSelectorPseudo(src, selector) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/", line 989, in _parseSelectorPseudo raise self.ParseError('Selector Pseudo Function closing \')\' not found', src, ctxsrc) xhtml2pdf.w3c.cssParser.CSSParseError: Selector Pseudo Function closing ')' not found:: (u':not(', u'[controls]) {\n disp')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment