简单的HTML到PDF的python库

2024-04-28 05:32:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用这个pydf在服务器上将HTML转换成PDF。这是一个来自他们文档的例子,说明了问题:

import pydf

pdf = pydf.generate_pdf('<h1>this is html</h1>')
with open('test_doc.pdf', 'wb') as f:
    f.write(pdf)

当我运行此文件时,每次都会出现相同的错误:

(pdf) <computer>:<folder> <user>$ python pdf.py 
Traceback (most recent call last):
  File "pdf.py", line 3, in <module>
    pdf = pydf.generate_pdf('<h1>this is html</h1>')
  File "/Users/nilesbrandon/Projects/pdf/pdf/lib/python2.7/site-packages/pydf/wkhtmltopdf.py", line 121, in generate_pdf
    return gen_pdf(html_file.name, cmd_args)
  File "/Users/nilesbrandon/Projects/pdf/pdf/lib/python2.7/site-packages/pydf/wkhtmltopdf.py", line 105, in gen_pdf
    _, stderr, returncode = execute_wk(*cmd_args)
  File "/Users/nilesbrandon/Projects/pdf/pdf/lib/python2.7/site-packages/pydf/wkhtmltopdf.py", line 22, in execute_wk
    p = subprocess.Popen(wk_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception

我在一个virtualenv中运行这个,我的pip freeze只有以下内容:

python-pdf==0.30

你知道这里出了什么问题吗?你知道吗


Tags: inpypdflibhtmllinesiteh1
1条回答
网友
1楼 · 发布于 2024-04-28 05:32:11

在使用macOS时,您需要自行下载wkhtmltopdf二进制文件:

pydf comes bundled with a wkhtmltopdf binary which will only work on Linux amd64 architectures. If you're on another OS or architecture your milage may vary, it is likely that you'll need to supply your own wkhtmltopdf binary and point pydf towards it by setting the WKHTMLTOPDF_PATH variable.

相关问题 更多 >