Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

As the title, I want to use markdown as my main write format and I need to generate PDF files from markdown using pure python.

like image 454
guilin 桂林 Avatar asked Nov 09 '10 15:11

guilin 桂林


People also ask

How do I turn a Markdown into a PDF?

Usage. Just focus the window containing your markdown file and use the convert command ( Packages > Markdown PDF > Convert ). The output PDF will be styled similar to the markdown on github.com , as well as any user styles you have added.

Can Python produce a PDF?

Fortunately, the Python ecosystem has some great packages for reading, manipulating, and creating PDF files. In this tutorial, you'll learn how to: Read text from a PDF.

How do you close a PDF in Python?

open() to open files and f. close() to close it.


2 Answers

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) 
like image 39
FlipperPA Avatar answered Sep 28 '22 19:09

FlipperPA


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.

like image 74
JasonFruit Avatar answered Sep 28 '22 19:09

JasonFruit