qoda / python-wkhtmltopdf Goto Github PK
View Code? Open in Web Editor NEWA simple python wrapper for the wkhtmltopdf lib.
License: Other
A simple python wrapper for the wkhtmltopdf lib.
License: Other
Why you use '-' symbol in options, if need use '_'? For example 'margin-left' (must be 'margin_left'). Please fix that in docs.
The usage documentation on pypi has a class name PWKhtmlToPd with a capital "H" whereas the actual class name in github has a lower case "h".
http://pypi.python.org/pypi?:action=display&name=wkhtmltopdf&version=0.1
vs.
https://github.com/qoda/python-wkhtmltopdf/blob/master/wkhtmltopdf/main.py#L113
$ brew search wkhtmltopdf
No formula found for "wkhtmltopdf".
When i insert footer_html and/or header_html in settings, footer nor header are not shown at all
I copied the exampe mentioned in the readme file and now when I try to run my python script I get the following error:
Traceback (most recent call last):
File "test.py", line 3, in
wkhtmltopdf(url='http://google.com',output_file='~/out.pdf',)
File "/usr/local/lib/python2.7/dist-packages/wkhtmltopdf-master-py2.7.egg/wkhtmltopdf/main.py", line 190, in wkhtmltopdf
wkhp = WKhtmlToPdf(_args, *_kwargs)
File "/usr/local/lib/python2.7/dist-packages/wkhtmltopdf-master-py2.7.egg/wkhtmltopdf/main.py", line 131, in init
raise Exception("Missing url and output file arguments")
Exception: Missing url and output file arguments
Here is the code:
from wkhtmltopdf import wkhtmltopdf
wkhtmltopdf(url='http://google.com',output_file='~/out.pdf',)
Any idea what is wrong here. I also tried the other example(Use from class) but it also raised the same error.
wkhtmltopdf
and wkhtmltoimage
is one project and will be useful use wrappers for both also with same codebase.
Base options:
OPTIONS = [
WKOption('enable-plugins', '-F', default=True, help="Use flash and other plugins."),
WKOption('disable-javascript', '-J', default=False, help="Disable javascript."),
WKOption('no-background', '-b', default=False, help="Do not print background."),
WKOption('grayscale', '-g', default=False, help="Make greyscale."),
WKOption('print-media-type', '', default=False, help="Set print media type."),
WKOption('username', '-U', default="", help="Set the HTTP username"),
WKOption('password', '-P', default="", help="Set the HTTP password"),
]
wkhtmltopdf
options:
OPTIONS_PDF = OPTIONS + [
WKOption(
'orientation', '-O', default="Portrait", help="Set page orientation.",
validate=lambda x: x in ['Portrait', 'Landscape'],
validate_error="Orientation argument must be either Portrait or Landscape"
),
WKOption(
'page-size', '-s', default="A4", help="Set page size.",
validate=lambda x: x in ['A4', 'Letter'],
validate_error="Page size argument must be A4 or Letter"
),
WKOption('dpi', '-D', default=100, help="Set DPI"),
WKOption('margin-bottom', '-B', default=10, help="Bottom page margin."),
WKOption('margin-top', '-T', default=10, help="Top page margin."),
WKOption('margin-left', '-L', default=10, help="Left page margin."),
WKOption('margin-right', '-R', default=10, help="Right page margin."),
WKOption(
'disable-smart-shrinking', None, default=False,
help="Disable the intelligent shrinking strategy used by WebKit that makes the pixel/dpi ratio none constant",
)
]
wkhtmltoimage
options:
OPTIONS_IMAGE = OPTIONS + [
WKOption('transparent', None, default=False, help="Make the background transparent in pngs."),
WKOption('width', None, default=1024, help="Set screen width, note that this is used only as a guide line."),
WKOption('height', None, default=-1, help="Set screen height (default is calculated from page content)."),
WKOption(
'format', '-f', default='png', help="Output file format.",
validate=lambda x: x in ['png', 'jpg', 'bmp', 'svg'],
validate_error="Output image format argument must be png, jpg, bmp or svg."
),
WKOption(
'quality', '-q', default=94, help="Output image quality.",
validate=lambda x: x in range(101),
validate_error="Output image quality argument must be between 0 and 100."
),
WKOption('crop-h', None, default=-1, help="Set height for cropping."),
WKOption('crop-w', None, default=-1, help="Set width for cropping."),
WKOption('crop-x', None, default=-1, help="Set x coordinate for cropping."),
WKOption('crop-y', None, default=-1, help="Set y coordinate for cropping."),
WKOption(
'disable-smart-width', None, default=False,
help="Use the specified width even if it is not large enough for the content",
)
]
I had a couple of issues with this package on Windows XP so I cracked open the egg and looked at main.py. I had 3 main problems:
If close_fds is true, all file descriptors except 0, 1 and 2 will be closed before the child process is executed. (Unix only). Or, on Windows, if close_fds is true then no handles will be inherited by the child process. Note that on Windows, you cannot set close_fds to true and also redirect the standard handles by setting stdin, stdout or stderr.
http://docs.python.org/library/subprocess.html
close_fds can be set to False
Using popen to call "wkhtmltopdf %s etc" doesn't seem to work on Windows. On Ubuntu (my main system) it does and I wouldn't be surprised if it also worked on Mac. On Windows I seemed to need to specify the path to wkhtmltopdf.exe e.g. "C:/Program Files/wkhtmltopdf/wkhtmltopdf.exe".
Options were fed into the command statement even if no arguments were set e.g. --redirect-delay. This caused the following sort of error:
g@grant-dev-pc:~$ wkhtmltopdf "a.html" --redirect-delay "b.pdf"
Invalid argument(s) parsed to --redirect-delay
If I manually edited the main.py file in the egg to address these 3 issues I could get a pdf generated.
It might also help to direct people to: http://code.google.com/p/wkhtmltopdf/downloads/list which provides installers to make it esasier to get past the wkhtmltopdf step and onto the python wkhtmltopdf step.
I hope this helps.
I'm working on wrapping this and if there's an issue writing the output (say the output directory doesn't exist) I'm not finding a way to catch the output.
I can see it ouput to stdout, but there's no method after I call .render() to get any of it.
Sample output
wkhtmltopdf --enable-plugins --redirect-delay 1 --orientation Landscape --dpi 100 "http://google.com" "/home/rharding/tmp/pdf/google.pdf" >> /tmp/wkhtp.log
Loading pages (1/5)
QPainter::begin(): Returned false============================] 100%
Error: Unable to write to destination
Is there any way I'm missing to catch that Error: line? Possible to do it in the wrapper?
It's not clear from the README whether Python 3 is supposed to be supported. Is this the case?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.