Proof of Concept tutorial
This is a barebones guide on getting HTML to PDF / HTML to Image conversion using Windows 7, PHP and Wkhtmltopdf. This is a POC only; this tutorial does not describe server installation or configuration (using a package like WAMP makes it easy anyway). I might add those in a later revision or an extended version of this guide.The steps
- Download wkhtmltopdf
- Installation step 1
- Installation step 2
- Installation step 3
- Start WampServer
- Open www-directory
- Create a test file
- Call the test file
- Examine generated
- Compare file contents
Download wkhtmltopdf
Installation step 1
Installation step 2
Installation step 3
Start your WWW Server
I use WAMP. It is very simple, easy to install, has sensible defaults but it's also very configurable.
Open www-directory
Create a test file
<?php
// Test correct and failed output
shell_exec('c:\wkhtmltopdf\wkhtmltopdf --asdasdsadsad 2>> err1.txt 1>> out1.txt');
shell_exec('c:\wkhtmltopdf\wkhtmltopdf --version 2>> err2.txt 1>> out2.txt');
?>
<html>
<head>
</head>
<body>
<p>Magical ponies!</p>
</body>
</html>
Here we are intentionally forcing an error with the bogus parameter -asdasddas so we can test that we indeed do get stderr output.
Call the test file
Examine generated
Compare file contents
shell_exec('c:\wkhtmltopdf\wkhtmltopdf --asdasdsadsad 2>> err1.txt 1>> out1.txt');
The second test generated the standard version output as expected and nothing to standard error output.
shell_exec('c:\wkhtmltopdf\wkhtmltopdf --version 2>> err2.txt 1>> out2.txt');
If this is not the case, you need to check that you are pointing to the right path when calling wkhtmltopdf and that your PHP server is configured correctly. If you get no errors, check that PHP error reporting is on and check what the Apache and PHP error logs say. If your WAMP is installed into C:\wamp, your Apache log will by default be at C:\wamp\logs\apache_error.log and your PHP log will be at C:\wamp\logs\php_error.log.
Thank you for this, I was driving crazy tring to configure it in windows, it turned out to be so easy! :)
ReplyDeleteI have another question, I got this working correctly and converting to pdf, but how can i use the wkhtmltopdf comands, like:
ReplyDelete$pdf->output(Wkhtmltopdf::MODE_DOWNLOAD, "test.pdf");
Thanks!
Hi, sorry for the late answer! That looks like a wrapper library that's using wkhtmltopdf so that you don't use it directly like I did in the examples. I don't know which one it is. It looks a little like the standard one provided at https://code.google.com/p/wkhtmltopdf/wiki/IntegrationWithPhp but it could also be Snappy, which is found at https://github.com/KnpLabs/snappy . I hope this helps if you still have this issue.
DeleteHi great tutorial, but where's the output pdf file?
ReplyDeleteSorry to answer so late! None of the commands I used actually produce a PDF file. You would use something like this: shell_exec('c:\wkhtmltopdf\wkhtmltopdf www.google.com google.pdf');
DeleteThis is meant to be a simple proof of concept and a starting point, converting live pages is a little more complicated and depends a lot on your website so it's hard to do a generic one.