python 中的 wkhtmltopdf 不工作

wkhtmltopdf in python not working

我正尝试在 python 中使用 wkhtmltopdf 做一些事情。但是,当我尝试执行命令时,我得到了这个:

>>> import subprocess
>>> cmd = ["bash", "xvfb-run", "-a", "-s", "\"-screen 0 1024x768x24\"", "wkhtmltopdf", "-B", "0", "-L", "0", "-R", "0", "-T", "0", "-O", "Portrait", "-s", "A4", "-dpi", "300", "--disable-smart-shrinking", "-zoom", "1.5", "-q", "http://127.0.0.1:9000/p/public/7caf00fa-b2ae-11e4-b194-d43d7edafecd/poster/pdf.php", "-",]
>>> subprocess.Popen(cmd,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()
("Unknown switch -i\n\nName:\n  wkhtmltopdf 0.9.6\n\nSynopsis:\n  wkhtmltopdf [OPTIONS]... <input file> [More input files] <output file>\n  \nDescription:\n  Converts one or more HTML pages into a PDF document, using wkhtmltopdf patched\n  qt.\n\nGeneral Options:\n  -b, --book                          Set the options one would usually set when                                      printing a book\n      --collate                       Collate when printing multiple copies\n      --copies <number>               Number of copies to print into the pdf                                      file (default 1)\n      --cover <url>                   Use html document as cover. It will be                                      inserted before the toc with no headers                                      and footers\n  -H, --default-header                Add a default header, with the name of the                                      page to the left, and the page number to                                      the right, this is short for:                                      --header-left='[webpage]'                                      --header-right='[page]/[toPage]' --top 2cm                                      --header-line\n      --extended-help                 Display more extensive help, detailing                                      less common command switches\n  -h, --help                          Display help\n  -O, --orientation <orientation>     Set orientation to Landscape or Portrait\n  -s, --page-size <size>              Set paper size to: A4, Letter, etc.\n      --password <password>           HTTP Authentication password\n  -p, --proxy <proxy>                 Use a proxy\n  -q, --quiet                         Be less verbose\n  -t, --toc                           Insert a table of content in the beginning                                      of the document\n      --username <username>           HTTP Authentication username\n  -V, --version                       Output version information an exit\nContact:\n  If you experience bugs or want to request new features please visit \n  <http://code.google.com/p/wkhtmltopdf/issues/list>, if you have any problems\n  or comments please feel free to contact me: see \n  <http://www.madalgo.au.dk/~jakobt/#about>\n\n\n\n\n\n\n\n\n\n\n\n\n\n", None)

相关问题似乎是 "Unknown switch -i",但是如您所见,命令中的任何地方都没有 '-i' 标志。

此外,当从命令行 运行 时,该命令工作正常:

~$ xvfb-run -a -s "-screen 0 1024x768x24" wkhtmltopdf -B 0 -L 0 -R 0 -T 0 -O Portrait -s A4 --dpi 300 --disable-smart-shrinking --zoom 1.5 -q http://127.0.0.1:9000/p/public/7caf00fa-b2ae-11e4-b194-d43d7edafecd/poster/pdf.php -
%PDF-1.4
1 0 obj
<<
/Title (??Planted baskets)
/Producer (wkhtmltopdf)
/CreationDate (D:20150219165915)
>>
endobj
4 0 obj
<<
/Type /ExtGState
/SA true
.... etc (this is the pdf output I want)

知道出了什么问题吗?

问题是你的参数列表中有 "-dpi",它应该是 "--dpi",与 --zoom 相同的问题。

您还应该从列表中删除 "bash",并将 "\"-screen 0 1024x768x24\"" 更改为 "-screen 0 1024x768x24"

你也可以试试shlex.split:

In [6]: shlex.split("xvfb-run -a -s '-screen 0 1024x768x24' wkhtmltopdf -B 0 -L 0 -R 0 -T 0 -O Portrait -s A4 --dpi 300 --disable-smart-shrinking --zoom 1.5 -q http://127.0.0.1:9000/p/public/7caf00fa-b2ae-11e4-b194-d43d7edafecd/poster/pdf.php -")
Out[6]: ['xvfb-run', '-a', '-s', '-screen 0 1024x768x24', 'wkhtmltopdf', '-B', '0', '-L', '0', '-R', '0', '-T', '0', '-O', 'Portrait', '-s', 'A4', '--dpi', '300', '--disable-smart-shrinking', '--zoom', '1.5', '-q', 'http://127.0.0.1:9000/p/public/7caf00fa-b2ae-11e4-b194-d43d7edafecd/poster/pdf.php', '-']