Oscar Robinson
Oscar Robinson

Reputation: 1003

wkhtmltopdf in python not working

I'm trying to do some stuff with wkhtmltopdf in python. However, when I try and execute a command I get this:

>>> 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)

The relevant problem seems to be "Unknown switch -i", however as you can see, there is no '-i' flag anywhere in the command.

Furthermore, the command works fine when run from the command line:

~$ 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)

Any idea what's going wrong?

Upvotes: 0

Views: 927

Answers (1)

Gandaro
Gandaro

Reputation: 3443

The problem is that you have "-dpi" in your argument list, it should be "--dpi", same issue with --zoom.

You should also remove "bash" from the list, and change "\"-screen 0 1024x768x24\"" to just "-screen 0 1024x768x24".

What you can also try is 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', '-']

Upvotes: 1

Related Questions