wkhtmltopdf (Linux)


In [5]:
!git clone https://bitbucket.org/luisfernando/html2pdf.git


正克隆到 'html2pdf'...
remote: Counting objects: 28, done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 28 (delta 7), reused 0 (delta 0)
展开对象中: 100% (28/28), 完成.
检查连接... 完成。

In [1]:
%%!
echo "Install Xvfd:"
sudo apt-get install xvfb

echo "Install Fonts:"
sudo apt-get install xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic

echo "Install wkhtmltopdf:"
sudo apt-get install wkhtmltopdf


Out[1]:
['Install Xvfd:',
 'sudo: 没有终端存在,且未指定 askpass 程序',
 'Install Fonts:',
 'sudo: 没有终端存在,且未指定 askpass 程序',
 'Install wkhtmltopdf:',
 'sudo: 没有终端存在,且未指定 askpass 程序']

python-wkhtmltopdf (Any Platform)

Development:
$ git clone https://bitbucket.org/luisfernando/html2pdf.git
$ cd html2pdf
$ virtualenv .
$ pip install -r requirements.pip

PIP:
$ pip install git+https://bitbucket.org/luisfernando/html2pdf.git

or from pypi
$ pip install html2pdf

In [2]:
%%!
source activate GISpark
pip install html2pdf


Out[2]:
['/bin/sh: 1: source: not found',
 'Collecting html2pdf',
 '  Downloading html2pdf-0.1.tar.gz',
 'Building wheels for collected packages: html2pdf',
 '  Running setup.py bdist_wheel for html2pdf: started',
 "  Running setup.py bdist_wheel for html2pdf: finished with status 'done'",
 '  Stored in directory: /home/supermap/.cache/pip/wheels/f4/77/ed/ce19a1319981f805ef074a75cc5df76d547df4894a7dffda57',
 'Successfully built html2pdf',
 'Installing collected packages: html2pdf',
 'Successfully installed html2pdf-0.1']

Render a URL:


In [1]:
from wkhtmltopdf import HTMLURLToPDF

make_pdf = HTMLURLToPDF(
    url='http://www.example.com',
    output_file='~/example.pdf',
)

make_pdf.render()


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-5779ff09c677> in <module>()
----> 1 from wkhtmltopdf import HTMLURLToPDF
      2 
      3 make_pdf = HTMLURLToPDF(
      4     url='http://www.example.com',
      5     output_file='~/example.pdf',

/home/supermap/anaconda3/envs/GISpark/lib/python3.5/site-packages/wkhtmltopdf/__init__.py in <module>()
----> 1 from main import WKhtmlToPdf, wkhtmltopdf
      2 import api

ImportError: No module named 'main'

Render a HTML string:


In [2]:
from html2pdf import HTMLToPDF

HTML = """
    <!DOCTYPE html>
    <html>
        <body>
        <h1>Hello World</h1>
        </body>
    </html>
"""

h = HTMLToPDF(HTML, self.output_file)


  File "/home/supermap/anaconda3/envs/GISpark/lib/python3.5/site-packages/html2pdf-0.1-py3.5.egg/html2pdf/main.py", line 174
    except OSError, exc:
                  ^
SyntaxError: invalid syntax

In [3]:
!wkhtmltopdf


You need to specify atleast one input file, and exactly one output file
Use - for stdin or stdout

Name:
  wkhtmltopdf 0.12.2.4

Synopsis:
  wkhtmltopdf [GLOBAL OPTION]... [OBJECT]... <output file>
  
Document objects:
  wkhtmltopdf is able to put several objects into the output file, an object is
  either a single webpage, a cover webpage or a table of content.  The objects
  are put into the output document in the order they are specified on the
  command line, options can be specified on a per object basis or in the global
  options area. Options from the Global Options section can only be placed in
  the global options area

  A page objects puts the content of a singe webpage into the output document.

  (page)? <input url/file name> [PAGE OPTION]...
  Options for the page object can be placed in the global options and the page
  options areas. The applicable options can be found in the Page Options and 
  Headers And Footer Options sections.

  A cover objects puts the content of a singe webpage into the output document,
  the page does not appear in the table of content, and does not have headers
  and footers.

  cover <input url/file name> [PAGE OPTION]...
  All options that can be specified for a page object can also be specified for
  a cover.

  A table of content object inserts a table of content into the output document.

  toc [TOC OPTION]...
  All options that can be specified for a page object can also be specified for
  a toc, further more the options from the TOC Options section can also be
  applied. The table of content is generated via XSLT which means that it can be
  styled to look however you want it to look. To get an aide of how to do this
  you can dump the default xslt document by supplying the
  --dump-default-toc-xsl, and the outline it works on by supplying
  --dump-outline, see the Outline Options section.

Description:
  Converts one or more HTML pages into a PDF document, *not* using wkhtmltopdf
  patched qt.

Global Options:
      --collate                       Collate when printing multiple copies
                                      (default)
      --no-collate                    Do not collate when printing multiple
                                      copies
      --copies <number>               Number of copies to print into the pdf
                                      file (default 1)
  -H, --extended-help                 Display more extensive help, detailing
                                      less common command switches
  -g, --grayscale                     PDF will be generated in grayscale
  -h, --help                          Display help
      --license                       Output license information and exit
  -l, --lowquality                    Generates lower quality pdf/ps. Useful to
                                      shrink the result document space
  -O, --orientation <orientation>     Set orientation to Landscape or Portrait
                                      (default Portrait)
  -s, --page-size <Size>              Set paper size to: A4, Letter, etc.
                                      (default A4)
  -q, --quiet                         Be less verbose
      --read-args-from-stdin          Read command line arguments from stdin
      --title <text>                  The title of the generated pdf file (The
                                      title of the first document is used if not
                                      specified)
  -V, --version                       Output version information and exit

Reduced Functionality:
  This version of wkhtmltopdf has been compiled against a version of QT without
  the wkhtmltopdf patches. Therefore some features are missing, if you need
  these features please use the static version.

  Currently the list of features only supported with patch QT includes:

 * Printing more then one HTML document into a PDF file.
 * Running without an X11 server.
 * Adding a document outline to the PDF file.
 * Adding headers and footers to the PDF file.
 * Generating a table of contents.
 * Adding links in the generated PDF file.
 * Printing using the screen media-type.
 * Disabling the smart shrink feature of webkit.

Contact:
  If you experience bugs or want to request new features please visit 
  <https://github.com/wkhtmltopdf/wkhtmltopdf/issues>


In [ ]: