Generating PDF documents in Python — The easy way

 4 years ago
source link: https://medium.com/@cnicodeme/generating-pdf-documents-in-python-the-easy-way-bce9d1697109
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Generating PDF documents in Python — The easy way

Image for post
Image for post

Generating a PDF document can be hell. Here, we’ll try to make that simpler by using PDFShift.io powerful API. (Plot twist: I’m the founder 😲)

What if I told you it was easy as sending a POST request? Take a look:

See? Now, let me say the same thing, but longer:

As a developer, you might find yourself facing the needs to generate a PDF for your application. It could be an invoice to be sent by email, a report to share to your users, or a blog post to be shared to your user’s user.

One simple, efficient and quick way to do that is to first create your document in HTML format. You already master the HTML/CSS elements and can move them like a Jedi, so why not?

All you have to do is convert the HTML into PDF and voilà! Your work is done.

Image for post
Image for post

Once you have your HTML document ready, one way you can do to get your PDF is by using an API.

PDFShift provides just that, and getting a PDF is as easy as:

POST https://api.pdfshift.io/v2/convert/ -d source={url}

The first thing you will need is an API key to generate the documents without watermark. Create your account on PDFShift. You will get your API key by email right after hitting submit and the free account will let you convert 50 documents per month.

👉 Create your account on PDFShift here.

We’ll then use the requests library to do the work. It allows you to make network request really easy and simplify all the tedious part of it.

Will start simple, converting your local HTML document to a PDF with no special stuff involved:

A few lines of code later, your application is now generating PDF documents.

Image for post
Image for post

I could stop here, but I’m not sure you realized how lucky you are. So let me share my point of view.

Why is this better than using {wkhtmltopdf/docker/some_library}?

Good question! One easy answer:


Installing a local software, running your own Docker instance or using a PDF library made for your programming language are all possibilities to generate PDF, but with all of these come a great bargain: maintenance.

You will have to update the software and library, making sure the processes haven’t stopped, or that your Docker server has shut down for an unexpected reason.

Moreover, you might not be able to do some advanced things like the one we’ll cover next.

Ensuring all your charts are correctly loaded

Now, you are just getting started and you want to generate a PDF of your report to send to all your customers on a monthly basis.

The issue is that the PDF returned is made before the charts have all been loaded, making your PDF useless. PDFShift provides a parameter that let you wait on your document to be finished before continuing the conversion.

This is useful here because you can wait for all your charts to be rendered before generating the PDF, ensuring that the document you’ll get back are perfect. This parameter is wait_for and the value is just the name of a javascript function that will be globally present (think window).

Here’s the initial code, with the wait_for parameter:

Processing multiple documents at once

Now imagine you have tons of documents to generate, one per user. Sending a POST request for each of them will take time and waiting for the result back might takes days of running process, hoping nothing will break in the middle!

On the other hand, what you can do is send the conversion in batch, up to 50 per request, and don’t have to wait for the result to come back.

Imagine you load your entire user's database and want to generate a custom report for each of them. This script will do exactly that and will be processed in a few minutes.

All you’ll need is a publicly available endpoint that will receive a POST request from us (we love POST) containing an URL to your generated PDF content!

We’ve taken this case and made a code just for you to see:

Another real case study, sending an invoice by email

When I first started PDFShift, it was because of a recurring issue I had. I wanted my customers to receive a real, nicely designed PDF invoice of their purchase.

The issue was that I was using a library at the time that required tons of dependencies that had to be installed as a system package directly. Without them, the library wouldn’t work (and even with some of them, it would still fail). Setting all up was costing me a few hours every time and I was getting bored.

So I like to use this example as a real case study because … well … it’s a real case study :)

As you’ll see, the code is a bit long, but converting HTML to PDF only takes a few lines of code, the rest is just email stuff (but interesting, still!):


Creating a PDF document can be tedious when we don’t have the right tool at our disposal. On the other hand, most of us have been writing HTML/CSS documents for years now and can quickly build a page this way.

PDFShift lets you convert these HTML documents in PDF in just a simple POST request and works everywhere. With it, you don’t have to worry anymore about maintenance, updates, and compatibility issues.

At PDFShift, our motto is the same as the Unix philosophy: We do one thing, and we do it well.

Image for post
Image for post

About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK