Is there any direct way to generate pdf from markdown file by python [closed]

旧街凉风 提交于 2019-12-02 20:15:52
JasonFruit

I have done and would do it in two steps. First, I'd use python-markdown to make HTML out of my Markdown, and then I'd use xhtml2pdf to make a PDF file.

Edit (2014):

If I were doing this now, I might choose WeasyPrint as my HTML-to-PDF tool; it does a beautiful job, and I've used it on a couple projects recently.

FlipperPA

Update for 2015:

I would use a combination of pdfkit and Python-Markdown. While this isn't a pure Python solution, but I've found it works best, especially if you're using Python 3.

First, install a prereq (or download here: http://wkhtmltopdf.org/downloads.html):

# Ubuntu
apt-get install wkhtmltopdf

Then, the necessary Python packages:

pip install pdfkit
pip install markdown

Then it is really simple:

from markdown import markdown
import pdfkit

input_filename = 'README.md'
output_filename = 'README.pdf'

with open(input_filename, 'r') as f:
    html_text = markdown(f.read(), output_format='html4')

pdfkit.from_string(html_text, output_filename)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!