wkhtmltopdf for Python on Heroku
Project description
wkhtmltopdf is a command line program that converts HTML to PDF using the WebKit rendering engine, as provided by Qt. Because it uses WebKit, you can use wkhtmltopdf to generate screenshots, or generate PDFs from Django or Rails or whatever, and the resulting PDFs will look awesome.
The typical install process includes downloading and compiling Qt, followed by downloading and installing wkhtmltopdf.
But you can’t really download and compile packages inside a Heroku dyno, so you need to vendor in any external binaries you might want to use (that aren’t Python packages that compile themselves). This package provides a simple way to include and use a wkhtmltopdf binary that has been compiled for the Heroku (Cedar stack) dyno environment.
Installation
Install it in the usual way:
pip install pywkher
Usage
One easy way of using wkhtmltopdf in your Python program is to use the included generate_pdf command. The generate_pdf command takes either a URL or an HTML document and returns a Python NamedTemporaryFile object referencing the generated PDF (which will be stored in the Heroku instance’s temporary directory).
Here’s an example of using the generate_pdf command to render a Django template as a PDF and return the resulting PDF as part of the HTTP response:
from os.path import basename from wsgiref.util import FileWrapper from django.http import HttpResponse from django.template import RequestContext from django.template.loader import get_template from pywkher import generate_pdf def return_a_pdf(request): template = get_template('my_awesome_template.html') html = template.render(RequestContext(request)) pdf_file = generate_pdf(html=html) response = HttpResponse(FileWrapper(pdf_file), content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename=%s.zip' % basename(pdf_file.name) response['Content-Length'] = pdf_file.tell() pdf_file.seek(0) return response
Usage in Development
If you want to test your app locally, obviously the binary that’s compiled for Heroku isn’t going to work, unless your development or test environment closely mimics Heroku’s. However, the generate function will look for an environment variable named WKHTMLTOPDF_CMD and will only use the internally-bundled wkhtmltopdf binary if that environment variable is not set.
Therefore, if you’re on a Mac, do something like:
brew install wkhtmltopdf (Homebrew required for this)
export WKHTLMTOPDF_CMD=wkhtmltopdf
python my_python_program.py (just an example)
Acknowledgements
Brad Phelan generated the binary that’s included here as part of his wkhtmltopdf-heroku gem. It’s thanks to his efforts that I didn’t have to go through the whole Heroku vulcan build process myself.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pywkher-new-params-1.0.4.tar.gz
.
File metadata
- Download URL: pywkher-new-params-1.0.4.tar.gz
- Upload date:
- Size: 10.9 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed4d54da7dcbfced1fc7e4854509dbcd7f29fdff32ff4757dd1837bc0bbcd83e |
|
MD5 | f7ca29ad3d79191446361a1a40d0bc65 |
|
BLAKE2b-256 | 53e609cc8687ed96729e967c24813acdc85afad12c34b63ea41aacc41581d094 |