Skip to content
Advertisement

wkhtmltopdf without creating a file in php

I have the wkhtmltopdf module in my Drupal which generates the pdf file by running the 'wkhtmltopdf --options URL filename.pdf' command using shell_exec function.

The output of the file is fine, but I don’t want to store the pdf in the file system. I just want to show the output on the browser so the user can choose whether or not to download it.

As far as I searched, I couldn’t find a way to get the output in the buffer rather than storing it in the pdf file. Is it possible to generate a pdf without creating a file in wkhtmltopdf?

Advertisement

Answer

GIF Demonstration (Over-engineered)

Here is an over-engineered piece of code I wrote just for you 🙂
It includes everything from the function to the demo form you can test out.

I do not guarantee stability with this code, you are free to check it out and modify it for your own use but I can’t guarantee 100% stability or security.

Read the documentation about functions such as shell_exec and why it is a bad practice due to potential security risks.

My recommendation is to write a PHP library in C++ and load that and use it in PHP.

I am not sure if one exists for wkhtmltopdf, someone in the comments correct me if I’m wrong.


Update 1

I tested this script on http://ifconfig.me and it returns a malformed PDF document.
So you perhaps have 3 choices, either write a PHP library in C++, wait for someone to come up with a better solution, or just download the file into /tmp and read the file using PHP then delete it.

GIF Demonstration (Simple)

Code (Simple)

JavaScript

Code (Over-engineered)

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement