简单的HTML到PDF python库错误

时间:2017-12-15 21:32:41

标签: python pdf

我使用此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

知道这里可能出现什么问题吗?

1 个答案:

答案 0 :(得分:2)

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

  pydf与wkhtmltopdf二进制文件捆绑在一起,只能在Linux amd64体系结构上运行。如果你在另一个操作系统或架构上,你的milage可能会有所不同,那么你可能需要提供自己的wkhtmltopdf二进制文件,并通过设置WKHTMLTOPDF_PATH变量为它指向pydf。