Converting html to pdf in php?
Asked Answered
I

4

5

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.

Intermigration answered 27/12, 2010 at 22:19 Comment(2)
That's sort of a no-longer maintained library.. its last release is from 2006! I would consider a newer -and active- one.Crenate
If you decide not to continue with the library you are currently using you can find a good list here: 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.Minuend
Q
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();
}
Quintuplet answered 27/12, 2010 at 22:22 Comment(28)
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 ?Intermigration
Be careful with the single quotes there. If you want variables substitution, those will have to be double quotes.Crenate
ok,cool,it looks great,can I use this library on my website,I'm not php guru,I'm just learning some stuffIntermigration
@Crenate 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.Quintuplet
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 html2pdf.fr/en/defaultIntermigration
@user Download the appropriate release and give it a try on the command line first. I also found php bindings (see edit in answer).Quintuplet
@marcog, does HTML needs to be valid? No that it is problem, just asking.Quixotism
@Webarto It uses webkit, so it does a great job: invalid html and resulting pdfQuintuplet
Yeah I thought so. Very nice project. Thanks for example.Quixotism
so how to install this on localhost for testing ?Intermigration
@user I already gave a download link above. Try show some effort. :)Quintuplet
sorry,I just spent all week on this :S I have downloaded wkhtmltox-0.10.0_rc1-installer.exe and from github github.com/mreiferson/php-wkhtmltox this,just now need to connect all this,...,how to do this? sorry,I'm not php guruIntermigration
I installed first thing,is there some simple tutorial for beginers?Intermigration
oooo,c-rap,I will not sleap another night :(( to make this thing work without command lineIntermigration
@user You do not need the command line. Use the sample code I provided in my answer.Quintuplet
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.Intermigration
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(); }Intermigration
APPLICATION_PATH - this should be path to my wkt instalation on my hard ? in c://program files... ?Intermigration
@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. :)Quintuplet
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.Intermigration
No,it doesn't work for me on windows your code.I change temp folder name to C:\\Windows\Temp,c-rap :((Intermigration
It looks really great when convert html page from command lineIntermigration
questuion,where is that page saved ? that pdf file,where is saved ?Intermigration
@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.Quintuplet
Yeah,that was the problem,file is created but not displayed in a browser.It works great,except part displaying in a browser.Intermigration
@user Try change while(!feof($file)) print fread($file, 4096); to print file_get_contents($file);Quintuplet
no,that,didn't fix anything.Don't worry,it is ok,will find solution for that :)) tnx for your help.Intermigration
tnx mate,tnx for helping me :)Intermigration
B
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
Blood answered 27/12, 2010 at 22:25 Comment(7)
this looks great,is it simple to install on localhost ? then later on server ?Intermigration
Yes just extract it in root folder and you are all set.Quixotism
Cool,I will take a look,at this,tnx mate :)Intermigration
I get this : preg_match() expects parameter 2 to be string, array given in C:\xampp\htdocs\cpt\html2pdf\html2pdf.class.php on line 970Intermigration
Probably your HTML is not valid, you must keep it valid in order to successfully convert it, check on this site: validator.w3.orgQuixotism
it is valid,now it just give me blank page,I use this code : $url = "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');Intermigration
Don't cURL yourself :) Use file_get_contents. Try this path 127.0.0.1/cpt/?cat=4 , if still no luck echo $content and see if it contains HTML.Quixotism
C
0

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

Crenate answered 27/12, 2010 at 22:28 Comment(0)
K
0

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.

Korwin answered 19/1, 2011 at 2:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.