So, it has come to this.
Reports, yes, your application will have to have reports – in brand colours, with images and logos abounding, and probably festooned with graphs of various sizes, shapes and degrees of relevance to what was once a nice, streamlined set of data. This report has just become a part of the application ‘product’, meant not just to communicate, but also to entice and enthrall. Form has become just as important as function… and, did I forget to mention? It also needs to be exportable.
Exportable, portable, downloadable, shareable – because as I mentioned, it’s not just data now. It is now something clients/users need to be able to ‘have’, to attach to emails, send to their marketing departments, and incorporate into their powerpoints.
There are a few ways to make this happen, but generally speaking, it’s time to break out the PDFs.
The Portable Document Format (PDF) has been a staple of web applications for some time, now – it’s well-suited to the sharing of information in a self-contained package, and it’s increasingly well-supported as many modern browsers bake support for the display of PDFs directly into the browser.
There are a number of ways to produce a PDF. We could use one of the various libraries for our language of choice – say, PyPDF2 or pdfrw for Python. PDF is a binary file format with a published specification, so if we really had a lot of free time on our hands and/or a masochistic streak, we could even do the necessary bit-twiddling ourselves. If we needed to just output some simple text or static images, these might be the way to go (except for the bit-twiddling. Most projects will not benefit from you suddenly deciding you need to devote the next n months to creating a new PDF library from scratch, no matter how fun that might sound).
In the scenario I posit above, however, we’re transitioning from an HTML page which displayed the data, to a full, glossy report – but it still needs to show up in the browser first, and support PDF as an export option. We could have two completely separate code-bases to support the web page display, and to build the PDF, but wouldn’t it be nice if we could just render our web page to a PDF?
Assuming this sounds just like what the doctor ordered, installing wkhtmltopdf on most Linux distros is just an
, etc. away; or you can download a pre-compiled binary if building from source is giving you trouble (and it might, particularly when attempting to build it on a server without any GUI libraries in place).
Now that we have the binary available, we could just call out to the command-line from our language of choice; we can make our lives a little easier by using a wrapper library, however. I first used pdfkit years ago with Ruby and it has been my go-to since for both Ruby and Python, but there are any number to choose from if pdfkit doesn’t strike your fancy.
For the purpose of this article, however, we’re going to assume you’re taking my suggestions, so let’s add pdfkit to our requirements.txt file (update the version as needed):
Heroku, wkhtmlopdf-pack, and which wkhtmltopdf
So, this gets you set for working in your local ‘nix dev box, but what about when it comes time to put this on a staging server or go live? If you’re using a similar environment to your development system, the setup will be much the same – but let’s add another piece to this puzzle, and posit that we’re going to need to run on the still very popular Heroku platform.
In that case, we can’t rely on the standard wkhtmltopdf binaries, as they aren’t compatible. Instead, we need to ‘vendor in’ a pre-compiled binary for their platform – that’s the purpose of the various Heroku buildpacks that you may be familiar with if you’ve used Heroku before.
We could compile the necessary binary for ourselves in a Heroku dyno via Heroku run, but it would be nicer not to spend the time/money if we don’t have to; and luckily, we’re not the first to think so.
Enter wkhtmltopdf-pack. Based on a similar project for Ruby, this python package simply installs a pre-compiled wkhtmltopdf binary that’s compatible with Heroku’s Cedar-14 platform, quick and easy, so let’s add that to our requirements.txt file as well.
Now, we’re faced with one additional hurdle, which is that we’ll want to use the wkhtmltopdf-pack binary on Heroku, but the wkhtmltopdf binary on our ‘nix dev system. So, let’s add an environment setting indicating the name of the binary we’re looking for on Heroku. Go to your dashboard, find the app to set this for, and click on settings. Then, add this config variable:
Key: WKHTMLTOPDF_BINARY Value: wkhtmltopdf-pack
And now, let’s add a little logic to ask the system which wkhtmltopdf binary we should use. This can go into whatever bootstrap config your framework might use (so, for instance, in Django’s settings.py), or simply before your use of wkhtmltopdf/pdfkit.
# Ensure virtualenv path is part of PATH env var os.environ['PATH'] += os.pathsep + os.path.dirname(sys.executable) WKHTMLTOPDF_CMD = subprocess.Popen( ['which', os.environ.get('WKHTMLTOPDF_BINARY', 'wkhtmltopdf')], # Note we default to 'wkhtmltopdf' as the binary name stdout=subprocess.PIPE).communicate().strip()
The value of WKHTMLTOPDF_CMD should now point to the binary we’ll want to use.
Finally, let’s take a look at a quick example of what using pdfkit with this binary might look like.
PDFKit offers us fairly easy access to the various configuration options for the binary, and let’s us set which binary we want to use, as shown below. See the documentation for more details.
Image via Wikimedia Commons
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.