Search code examples
pythonflaskpdfkit

Python + pdfkit: Generate stream with pdfkit


I'm trying to use pdfkit to turn html pages into PDFs, and then return a stream of that PDF as this code is a Flask API called by a webpage.

As far as I can tell, when you use pdfkit.from_file, you have provide both an input and output path:

pdfkit.from_file("input.html", "output.pdf")

The problem is, I don't want to write these files to my server, as there could be hundreds of users generating these PDFs. Is there any option to return that as a stream instead? This comment suggests there is, but I couldn't get it to work: https://stackoverflow.com/a/73821712/4339010

If there is no stream option, what the best alternative? Write the file temporarily with a uuid or some transactional id I do have, write that to a stream and then delete the pdf and return the stream? Just looking for some options. Thanks.


Solution

  • Upgrading to 1.0.0 of pdfkit solved my problem. At that point, the output file is an optional parameter. If you are on an earlier version like I was (0.6.1), if you just provided False instead of a file, it will work the same as 1.0.0.

    Regardless of your version, when you call it as above the file is returned as a string. You can take that and send that back in your webservice without having to save anything.

    file_str = pdfkit.from_file("hello.html")
    return Response(file_str, mimetype="application/pdf", headers={"Content-Disposition": "attachment; filename=hello.pdf"})