Forum Moderators: coopster
our goal is to allow our visitors to generate PDF version of the article on the spot.
we looked over the web for such applications, and we cant make a smart decision. anyone to offer his/her insight or experience?
Be forewarned: making PDFs is no cakewalk. If your documents have fancy formatting, layout, multiple columns, images, tables etc... you might be spending a lot of time writing your own functions and fiddling with spacing and margins...
But outputting text articles with a nice header and footer, that's a breeze.
May I suggest... if you have 2000 unchanging articles to show as PDF, it might be easiest to generate 2000 PDF files and host them. Or at least cache & save the PDF files as they're created, so at the next request they don't have to be re-created!
On the other hand, if the PDFs are personalized, i.e. the content is not identical every time, then I have the solution for you.
Use THIS METHOD. I learned this the hard way, by trial and error, with emphasis on the "error"
1) create a PHP file called "show_pdf.php".
2) Put in all the code that creates the PDF contents - I do recommend the FPDF class, so try that.
$pdf=new FPDF();
$pdf->Open();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$text="hello world";
$pdf->Write(5,$text);
etc etc
3) At the bottom of your script, you'll have an object $pdf. Create a temp file *.PDF, and output that object into it.
//Determine a temporary file name in the current directory
$file=tempnam('acrobats/','acrobat_tmp');
rename($file,$file.'.pdf');
$file.='.pdf';
//Save PDF to file
$pdf->Output($file);
The code above chooses a temporary name for the PDF and saves the PDF as a file on your server (remember to set permissions on the "acrobats" folder).
4) Now the PDF file exists, and you can send your visitor directly to it. Note that this method is a lot more reliable than any other method you'll read about - I tried a few tricks to send the PDF data right to the browser without using a Temp file, and frankly they work maybe half the time, the other half you're confused as heck. This javascript is at the END of the script, thus will be printed to the browser after the PDF exists, and everything else is done.
//JavaScript redirection
echo "
<HTML>
<p>Loading PDF file...
<br>If the PDF document does not automatically load in this window, <a href='$file'>click here</a>
<SCRIPT>
document.location='$file';
</SCRIPT>
</HTML>";
4) The problem with temp files is that they accumulate on the server. Now you want something that will clean out old files so they don't accumulate. I don't like relying on CronJobs for these kinds of things - I'd rather if the cleanup was triggered by the request of a new PDF. In a metaphor, it's not doing the dishes after every meal and thus keeping a clean kitchen... instead you're washing *before* every meal, scrubbing the plates from the previous feast. Sure, the kitchen isn't as tidy, but the mess never gets get out of hand. This function looks for any files that are older than half an hour, and deletes them.
Put this code at the top of "show_pdf.php"
CleanFiles('acrobats');
function CleanFiles($dir){
//Delete temporary files
$t=time();
$h=opendir($dir);
while($file=readdir($h)){
if(substr($file,0,11)=='acrobat_tmp' and substr($file,-4)=='.pdf'){
$path=$dir.'/'.$file;
//print($path." ".($t-filemtime($path))."<BR>");
if($t-filemtime($path)>360){
//print("erasing ".$path."<br>");
unlink($path);
}
}
}
closedir($h);
} Be sure to put this tag on all your web pages, or nothing will work:
<!-- I got advice from httpwebwitch. What a swell guy! -->