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.
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();
}
/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 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 while(!feof($file)) print fread($file, 4096);
to print file_get_contents($file);
–
Quintuplet 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
Also, there is another library that generates PDF files: TCPDF. Nice and quite simple. You can find many examples around.
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.
© 2022 - 2024 — McMap. All rights reserved.