2

I am trying to convert html documents to pdf's using wkhtmltopdf. The command that I would use on linux: wkhtmltopdf 15.52579.html 15.52579.pdf.

This first outputs something like Loading pages (1/6) [> ] 0% [=====, loads to 100% and then shows:

Loading pages (1/6) Counting pages (2/6) Resolving links (4/6) Loading headers and footers (5/6) Printing pages (6/6) Done

I want php to execute this command. I've tried to use php's exec("wkhtmltopdf 15.52579.html 15.52579.pdf") and shell_exec("wkhtmltopdf 15.52579.html 15.52579.pdf"), both with and without capturing stderr by adding 2>&1. I've also tried proc_open functions.

Everytime, my result is Loading pages (1/6) [> ] 0% [======> ] 10%. It looks like the command returns too soon, not allowing the program to finish and actually create the pdf.

The user that php is running under has the correct permissions to execute the program. Script is executed by a webpage, and should finish in matters of seconds. What am I missing?

4
  • 1
    is php run as a webpage or CLI script ? Commented Nov 23, 2015 at 10:21
  • @Calimero as webpage Commented Nov 23, 2015 at 11:42
  • How long does the script typically run ? roughly how much seconds, minutes, hours ? Commented Nov 23, 2015 at 11:44
  • Normally it takes about 5 seconds for the script to execute. Commented Nov 23, 2015 at 11:47

1 Answer 1

1

An alternative approach is to use a library (php5) called snappy which wraps wkhtmltopdf and offers a nice and clean object oriented interface with proper error reporting.

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
$html = file_get_contents('http://www.yourdomain.tld/path/15.52579.html');
$file = '/your/local/path/15.52579.pdf';
$snappy->generateFromHtml($html, $file);

Visit github for download and further documentation: https://github.com/knplabs/snappy

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.