6

I know that there is a lot of posts about this problem.But can someone help me to setup this http://www.rustyparts.com/pdf.php script to work on my localhost.I just spent all week on this problem.I have download imagemagic,ghostscript,activeperl,...,everything,but still can't make simple example to work.

moinudin
  • 134,091
  • 45
  • 190
  • 216
user147
  • 1,312
  • 5
  • 22
  • 41
  • 2
    That's sort of a no-longer maintained library.. its last release is from 2006! I would consider a newer -and active- one. – ncuesta Dec 27 '10 at 22:31
  • If you decide not to continue with the library you are currently using you can find a good list here: http://stackoverflow.com/q/3178448/264628. Looks like you're trying out wkhtmltopdf, but there are a number of pure PHP solutions out there as well. – BrianS Dec 28 '10 at 19:24

4 Answers4

10

Use wkhtmltopdf via a system call. See How to install wkhtmltopdf on a linux based (shared hosting) web server for installation help.

wkhtmltopdf is a command line program which permits to create a pdf from an url, a local html file or stdin. It produces a pdf like rendred with the WebKit engine.

See the sample from this page here.

php code tested on Ubuntu (you'll need to change the /tmp/ to a temporary directory on Windows):

$url = 'http://www.google.com';
$pdffile = tempnam('/tmp/', 'wkhtmltopdf_');
$handle = popen("wkhtmltopdf $url $pdffile 2>&1", "r");
while (!feof($handle)) 
  fread($handle, 4096);
pclose($handle);
header('Content-type: application/pdf');
$file = fopen($pdffile, "r");
while(!feof($file))
  print fread($file, 4096);
unlink($pdffile);

There are also php bindings which removes the need to use a system call yourself, which is a simpler (and safer!) option.

try {
    $wkhtmltopdf = new Core_Wkhtmltopdf(array('path' => APPLICATION_PATH . '/../public/uploads/'));
    $wkhtmltopdf->setTitle("Title");
    $wkhtmltopdf->setHtml("Content");
    $wkhtmltopdf->output(Wkhtmltopdf::MODE_DOWNLOAD, "file.pdf");
} catch (Exception $e) {
    echo $e->getMessage();
}
Community
  • 1
  • 1
moinudin
  • 134,091
  • 45
  • 190
  • 216
  • Ohhh,another php pdf library :((( but I need to create system for exporting html pages to pdf,user to click on link on website,and than pdf file is generated ? – user147 Dec 27 '10 at 22:28
  • Be careful with the single quotes there. If you want variables substitution, those will have to be double quotes. – ncuesta Dec 27 '10 at 22:29
  • ok,cool,it looks great,can I use this library on my website,I'm not php guru,I'm just learning some stuff – user147 Dec 27 '10 at 22:29
  • @ncuesta Fixed. @user This isn't a library at all, it's a standalone binary you can call from within PHP via a system call as I've shown. Many html2pdf's out there use there own rendering engine, this one uses the excellent WebKit engine used in Chrome, Safari and other major browsers. – moinudin Dec 27 '10 at 22:32
  • I didn't play with system calls with php so I don't know how to use that,is it simple to install on localhost,first will check this one http://html2pdf.fr/en/default – user147 Dec 27 '10 at 22:34
  • @user [Download the appropriate release](http://code.google.com/p/wkhtmltopdf/downloads/list) and give it a try on the command line first. I also found php bindings (see edit in answer). – moinudin Dec 27 '10 at 22:39
  • @marcog, does HTML needs to be valid? No that it is problem, just asking. – Dejan Marjanović Dec 27 '10 at 23:07
  • @Webarto It uses webkit, so it does a great job: [invalid html](http://marco.uctleg.net/resources/invalid.html) and [resulting pdf](http://marco.uctleg.net/resources/invalid.pdf) – moinudin Dec 27 '10 at 23:12
  • Yeah I thought so. Very nice project. Thanks for example. – Dejan Marjanović Dec 27 '10 at 23:12
  • so how to install this on localhost for testing ? – user147 Dec 27 '10 at 23:15
  • @user I already gave a download link above. Try show some effort. :) – moinudin Dec 27 '10 at 23:22
  • sorry,I just spent all week on this :S I have downloaded wkhtmltox-0.10.0_rc1-installer.exe and from github https://github.com/mreiferson/php-wkhtmltox this,just now need to connect all this,...,how to do this? sorry,I'm not php guru – user147 Dec 27 '10 at 23:28
  • I installed first thing,is there some simple tutorial for beginers? – user147 Dec 27 '10 at 23:28
  • oooo,c-rap,I will not sleap another night :(( to make this thing work without command line – user147 Dec 27 '10 at 23:38
  • @user You do not need the command line. Use the sample code I provided in my answer. – moinudin Dec 27 '10 at 23:42
  • Ok,can you help me a little bit more with this,I have setup on my localhost test.php,and copy your code,of course that will not work,what I need to include in my test.php file. – user147 Dec 27 '10 at 23:42
  • just copy this : try { $wkhtmltopdf = new Core_Wkhtmltopdf(array('path' => APPLICATION_PATH . '/../public/uploads/')); $wkhtmltopdf->setTitle("Title"); $wkhtmltopdf->setHtml("Content"); $wkhtmltopdf->output(Wkhtmltopdf::MODE_DOWNLOAD, "file.pdf"); } catch (Exception $e) { echo $e->getMessage(); } – user147 Dec 27 '10 at 23:43
  • APPLICATION_PATH - this should be path to my wkt instalation on my hard ? in c://program files... ? – user147 Dec 27 '10 at 23:45
  • @user I've included some code which I've tested and it works on Ubuntu. You'll need to change `/tmp/` as I mention in my answer to make it work on windows. It's late here, and I'm sleepy. Hopefully I wake up to see you've solved it painlessly. :) – moinudin Dec 28 '10 at 00:01
  • I have made this works with command line,just to make this works in php script,ok,thank you for your time :) I will give it a try couple more times. – user147 Dec 28 '10 at 00:03
  • No,it doesn't work for me on windows your code.I change temp folder name to C:\\Windows\Temp,c-rap :(( – user147 Dec 28 '10 at 00:24
  • It looks really great when convert html page from command line – user147 Dec 28 '10 at 09:55
  • questuion,where is that page saved ? that pdf file,where is saved ? – user147 Dec 28 '10 at 10:00
  • @user `tempnam` generates a random filename beginning with `C:\\Windows\Temp\wkhtmltopdf_`. The file is saved there, but then the `unlink` deletes it. Change `$pdffile` to something static and comment out the `unlink` if you want to test it. – moinudin Dec 28 '10 at 10:10
  • Yeah,that was the problem,file is created but not displayed in a browser.It works great,except part displaying in a browser. – user147 Dec 28 '10 at 10:18
  • @user Try change `while(!feof($file)) print fread($file, 4096);` to `print file_get_contents($file);` – moinudin Dec 28 '10 at 10:20
  • no,that,didn't fix anything.Don't worry,it is ok,will find solution for that :)) tnx for your help. – user147 Dec 28 '10 at 10:27
  • tnx mate,tnx for helping me :) – user147 Dec 28 '10 at 10:30
3

Simple but powerful: http://html2pdf.fr/en/default

$html = file_get_contents("valid.html");
require_once("html2pdf.class.php");
$html2pdf = new HTML2PDF("P", "A4", "en", array(10, 10, 10, 10));
$html2pdf->setEncoding("ISO-8859-1");
$html2pdf->WriteHTML($html);
$html2pdf->Output("pdf/PDF.pdf", "F"); //output to file
$html2pdf->Output(); //output to browser
Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
  • this looks great,is it simple to install on localhost ? then later on server ? – user147 Dec 27 '10 at 22:31
  • Yes just extract it in root folder and you are all set. – Dejan Marjanović Dec 27 '10 at 22:36
  • Cool,I will take a look,at this,tnx mate :) – user147 Dec 27 '10 at 22:39
  • I get this : preg_match() expects parameter 2 to be string, array given in C:\xampp\htdocs\cpt\html2pdf\html2pdf.class.php on line 970 – user147 Dec 27 '10 at 23:01
  • Probably your HTML is not valid, you must keep it valid in order to successfully convert it, check on this site: http://validator.w3.org/ – Dejan Marjanović Dec 27 '10 at 23:06
  • it is valid,now it just give me blank page,I use this code : $url = "http://localhost/cpt/?cat=4"; $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); require_once(TEMPLATEPATH . '/html2pdf/html2pdf.class.php'); $html2pdf = new HTML2PDF('P', 'A4', 'fr'); $content = $file_contents; $html2pdf->WriteHTML($content); $html2pdf->Output('this_shit.pdf'); – user147 Dec 27 '10 at 23:11
  • Don't cURL yourself :) Use file_get_contents. Try this path http://127.0.0.1/cpt/?cat=4 , if still no luck echo $content and see if it contains HTML. – Dejan Marjanović Dec 28 '10 at 00:22
1

DocRaptor.com is a good option - it uses Prince XML, so the quality is better than other tools, and it's a web app, so nothing to download. It also works in any language.

Julie
  • 44
  • 2
0

Also, there is another library that generates PDF files: TCPDF. Nice and quite simple. You can find many examples around.

ncuesta
  • 760
  • 4
  • 12