Skip to content

Instantly share code, notes, and snippets.

@m3nu
Last active Nov 21, 2016
Embed
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
https://github.com/walle/gimli
https://gist.github.com/SpotlightKid/0efb4d07f28af1c8fc1b
## Documentation of underlying tools
http://xhtml2pdf.appspot.com/static/pisa-en.html
Required packages: markdown2, xhtml2pdf, html5lib
"""
DEFAULT_EXTRAS = [
'fenced-code-blocks',
'footnotes',
'metadata',
'pyshell',
'smarty-pants',
'tag-friendly',
'wiki-tables'
]
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:
DEFAULT_CSS += f.read()
# read markdown
with open(md_file) as f:
md = f.read()
# 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 input.md style.css')
print(' You can also set a CSS file as env var.')
print(' E.g. export MD2PDF_CSS=style.css')
sys.exit()
if len(sys.argv) == 2:
css = os.getenv('MD2PDF_CSS')
if len(sys.argv) == 3:
css = sys.argv[2]
main(sys.argv[1], css)
@srugano

This comment has been minimized.

Copy link

@srugano srugano commented Nov 8, 2014

When I put a css file, I have

python md2pdf.py README.md style.css

Traceback (most recent call last): File "md2pdf.py", line 63, in <module> main(sys.argv[1], css) File "md2pdf.py", 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/document.py", line 89, in pisaDocument encoding, context=context, xml_output=xml_output) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/document.py", 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/parser.py", line 685, in pisaParser context.parseCSS() File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/context.py", 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/cssParser.py", line 434, in parse src, stylesheet = self._parseStylesheet(src) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/cssParser.py", line 538, in _parseStylesheet src, ruleset = self._parseRuleset(src) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/cssParser.py", line 835, in _parseRuleset src, selectors = self._parseSelectorGroup(src) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/cssParser.py", line 846, in _parseSelectorGroup src, selector = self._parseSelector(src) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/cssParser.py", line 860, in _parseSelector src, selector = self._parseSimpleSelector(src) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/cssParser.py", line 914, in _parseSimpleSelector src, selector = self._parseSelectorPseudo(src, selector) File "/home/username/.virtualenvs/burundi_ureport/local/lib/python2.7/site-packages/xhtml2pdf/w3c/cssParser.py", 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