Package: pdfkit-async
Async Python-PDFKit: HTML to PDF wrapperPython 3.5+ async wrapper for wkhtmltopdf utility to convert HTML to PDF
using Webkit.NOTE:All the public API functions are adapted to async coroutines, so use them with await!This is adapted version ofruby
PDFKitlibrary, so big thanks to
them!InstallationInstall python-pdfkit:$pipinstallpdfkit-asyncInstall wkhtmltopdf:Windows and other options: check wkhtmltopdfhomepagefor binary installersDebian/Ubuntu:$sudoapt-getinstallwkhtmltopdfmacOS:$brewinstallcaskroom/cask/wkhtmltopdfWarning!Version in debian/ubuntu repos have reduced functionality
(because it compiled without the wkhtmltopdf QT patches), such as adding
outlines, headers, footers, TOC etc. To use this options you should
install static binary fromwkhtmltopdfsite
or you can usethis
script.UsageFor simple tasks:importpdfkitasyncdeff():awaitpdfkit.from_url('http://google.com','out.pdf')awaitpdfkit.from_file('test.html','out.pdf')awaitpdfkit.from_string('Hello!','out.pdf')You can pass a list with multiple URLs or files:awaitpdfkit.from_url(['google.com','yandex.ru','engadget.com'],'out.pdf')awaitpdfkit.from_file(['file1.html','file2.html'],'out.pdf')Also you can pass an opened file:withopen('file.html')asf:awaitpdfkit.from_file(f,'out.pdf')If you wish to further process generated PDF, you can read it to a
variable:# Use False instead of output path to save pdf to a variablepdf=awaitpdfkit.from_url('http://google.com',False)You can specify all wkhtmltopdfoptions. You can drop
'--' in option name. If option without value, useNone, Falseor''for dict value:. For repeatable options (incl. allow, cookie,
custom-header, post, postfile, run-script, replace) you may use a list
or a tuple. With option that need multiple values (e.g. --custom-header
Authorization secret) we may use a 2-tuple (see example below).options={'page-size':'Letter','margin-top':'0.75in','margin-right':'0.75in','margin-bottom':'0.75in','margin-left':'0.75in','encoding':"UTF-8",'custom-header':[('Accept-Encoding','gzip')]'cookie':[('cookie-name1','cookie-value1'),('cookie-name2','cookie-value2'),],'no-outline':None}awaitpdfkit.from_url('http://google.com','out.pdf',options=options)By default, PDFKit will show allwkhtmltopdfoutput. If you don't want
it, you need to passquietoption:options={'quiet':''}awaitpdfkit.from_url('google.com','out.pdf',options=options)Due to wkhtmltopdf command syntax,TOCandCoveroptions must be
specified separately. If you need cover before TOC, usecover_firstoption:toc={'xsl-style-sheet':'toc.xsl'}cover='cover.html'awaitpdfkit.from_file('file.html',options=options,toc=toc,cover=cover)awaitpdfkit.from_file('file.html',options=options,toc=toc,cover=cover,cover_first=True)You can specify external CSS files when converting files or strings
usingcssoption.WarningThis is a workaround forthis
bugin
wkhtmltopdf. You should try--user-style-sheetoption first.# Single CSS filecss='example.css'awaitpdfkit.from_file('file.html',options=options,css=css)# Multiple CSS filescss=['example.css','example2.css']awaitpdfkit.from_file('file.html',options=options,css=css)You can also pass any options through meta tags in your HTML:body="""<html><head><meta name="pdfkit-page-size" content="Legal"/><meta name="pdfkit-orientation" content="Landscape"/></head>Hello World!</html>"""awaitpdfkit.from_string(body,'out.pdf')#with --page-size=Legal and --orientation=LandscapeConfigurationEach API call takes an optional configuration paramater. This should be
an instance ofpdfkit.configuration()API call. It takes the
configuration options as initial paramaters. The available options are:wkhtmltopdf- the location of thewkhtmltopdfbinary. By defaultpdfkitwill attempt to locate this usingwhich(on UNIX type
systems) orwhere(on Windows).meta_tag_prefix- the prefix forpdfkitspecific meta tags - by
default this ispdfkit-Example - for whenwkhtmltopdfis not on$PATH:config=pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf')awaitpdfkit.from_string(html_string,output_file,configuration=config)TroubleshootingIOError: 'No wkhtmltopdf executable found':Make sure that you have wkhtmltopdf in your $PATH or set via custom
configuration (see preceding section).where wkhtmltopdfin
Windows orwhich wkhtmltopdfon Linux should return actual path to
binary.IOError: 'Command Failed'This error means that PDFKit was unable to process an input. You can
try to directly run a command from error message and see what error
caused failure (on some wkhtmltopdf versions this can be cause by
segmentation faults)Changelog0.7.2Fixed Project description for latest rulesFixed Travis buildUpdated README0.7.0Dropped support of Python2.7 till Python3.4Supports Python3.5+AsyncIO supportProject took over byshivanshs90.6.1Fix regression on python 3+ when trying to decode pdf output0.6.0Support repeatable optionsSupport multiple values for some optionsFix some corner cases when specific argument order is
requiredSome Python 3+ compatibility fixesUpdate README0.5.0Allow passing multiple css filesFix problems with external file encodingsRise an error when X server is missing on *nix systemsFix tests that was broken with latest wkhtmltopdf releaseUpdate README0.4.1More easier custom configuration settingUpdate README0.4.0Allow passing file-like objectsAbility to return PDF as a stringAllow user specification of configurationAPI calls now returns True on successbugfixes0.3.0Python 3 support0.2.4Add HistoryUpdate setup.py0.2.3Fix installing with setup.pyUpdate README