Git Product home page Git Product logo

python-wkhtmltopdf's Introduction

NO LONGER MAINTAINED

python-wkhtmltopdf

A simple python wrapper for the wkhtmltopdf lib (https://github.com/wkhtmltopdf/wkhtmltopdf)

Requirements

System:

  • Linux 32/64 or OSX only (Windows is not supported at this stage)
  • Xvfd
  • wkhtmltopdf
  • python 2.5+

Installation

wkhtmltopdf (Linux)

  1. Install Xvfd:

    $ sudo apt-get install xvfb
    
  2. Install Fonts:

    $ sudo apt-get install xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic
    
  3. Install wkhtmltopdf:

    $ sudo apt-get install wkhtmltopdf
    

wkhtmltopdf (OSX)

  1. Install wkhtmltopdf:

    $ brew install wkhtmltopdf
    

python-wkhtmltopdf (Any Platform)

  1. Development:

    $ git clone [email protected]:qoda/python-wkhtmltopdf.git
    $ cd python-wkhtmltopdf
    $ virtualenv .
    $ pip install -r requirements.pip
    
  2. PIP:

    $ pip install git+https://github.com/qoda/python-wkhtmltopdf.git
    
    or from pypi
    
    $ pip install python-wkhtmltopdf
    

Usage

Simple Usage::

  1. Use from class:

    from wkhtmltopdf import WKHtmlToPdf
    
    wkhtmltopdf = WKHtmlToPdf(
        url='http://www.example.com',
        output_file='~/example.pdf',
    )
    wkhtmltopdf.render()
    
  2. Use from method:

    from wkhtmltopdf import wkhtmltopdf
    
    wkhtmltopdf(url='example.com', output_file='~/example.pdf')
    
  3. Use from commandline (installed):

    $ python -m wkhtmltopdf.main example.com ~/example.pdf
    
  4. Use the api (installed):

    $ python -m wkhtmltopdf.api &
    $ wget http://localhost:8888/?url=example.com&output_file=example.pdf
    

Required Arguments:

  • url - the url to convert to pdf
  • output_file - the pdf file that you want to create

Optional Arguments:

  • enable-plugins (default: True)
  • disable-javascript (default: False)
  • no-background (default: False)
  • grayscale (default: False)
  • orientation (default: Portrait)
  • dpi (default: 100)
  • username (default: None)
  • password (default: None)
  • margin-bottom (default: 10)
  • margin-top (default: 10)
  • margin-left (default: 10)
  • margin-right (default: 10)
  • disable-smart-shrinking (default: False)

python-wkhtmltopdf's People

Contributors

kisabaka avatar knightsamar avatar mitechie avatar qoda avatar zeitkunst avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-wkhtmltopdf's Issues

Options

Why you use '-' symbol in options, if need use '_'? For example 'margin-left' (must be 'margin_left'). Please fix that in docs.

Python3 support?

It's not clear from the README whether Python 3 is supposed to be supported. Is this the case?

any method to catch/get error output

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?

add wkhtmltoimage support

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",
    )
]

Exception when using the example mentioned in readme file.

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.

Modifications to make usable for Windows

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:

  1. close_fds was set to True but on Windows this is apparently not an option when stderr etc are set.

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

  1. 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".

  2. 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.