是否有任何直接的方法通过python从markdown文件生成pdf

时间:2022-08-20 00:25:53

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.

作为标题,我想使用markdown作为我的主要写入格式,我需要使用纯python从markdown生成PDF文件。

3 个解决方案

#1


15  

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.

我已经完成并将分两步完成。首先,我使用python-markdown从我的Markdown中制作HTML,然后我使用xhtml2pdf制作PDF文件。

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.

如果我现在这样做,我可能会选择WeasyPrint作为我的HTML-to-PDF工具;它做得很漂亮,最近我在几个项目中使用过它。

#2


9  

Update for 2015:

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.

我会结合使用pdfkit和Python-Markdown。虽然这不是一个纯粹的Python解决方案,但我发现它最好用,特别是如果你使用的是Python 3。

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

首先,安装一个先决条件(或在这里下载:http://wkhtmltopdf.org/downloads.html):

# Ubuntu
apt-get install wkhtmltopdf

Then, the necessary Python packages:

然后,必要的Python包:

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)

#3


0  

For anybody like me trying to find an answer in 2018:

对于像我这样试图在2018年找到答案的人:

I have not tested it but Markdown2PDF seems to be an option https://pypi.org/project/Markdown2PDF/

我没有测试过,但Markdown2PDF似乎是一个选项https://pypi.org/project/Markdown2PDF/

#1


15  

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.

我已经完成并将分两步完成。首先,我使用python-markdown从我的Markdown中制作HTML,然后我使用xhtml2pdf制作PDF文件。

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.

如果我现在这样做,我可能会选择WeasyPrint作为我的HTML-to-PDF工具;它做得很漂亮,最近我在几个项目中使用过它。

#2


9  

Update for 2015:

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.

我会结合使用pdfkit和Python-Markdown。虽然这不是一个纯粹的Python解决方案,但我发现它最好用,特别是如果你使用的是Python 3。

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

首先,安装一个先决条件(或在这里下载:http://wkhtmltopdf.org/downloads.html):

# Ubuntu
apt-get install wkhtmltopdf

Then, the necessary Python packages:

然后,必要的Python包:

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)

#3


0  

For anybody like me trying to find an answer in 2018:

对于像我这样试图在2018年找到答案的人:

I have not tested it but Markdown2PDF seems to be an option https://pypi.org/project/Markdown2PDF/

我没有测试过,但Markdown2PDF似乎是一个选项https://pypi.org/project/Markdown2PDF/