Using FPDI and FDP to generate slightly different pdf files
i first import a pdf using fpdi to make a fpdf object, i then perform several changes on that pdf. I clone it to make a custom pdf just adding some texts. Then i output the two files to disk but just one is created and i got a fatal error for the second output :
Fatal 开发者_JAVA百科error: Call to undefined method stdClass::closeFile() in C:\Program Files\EasyPHP 3.0\www\oursin\oursin\public\scripts\FPDI\fpdi.php on line 534
pieces of my code:
$pdf = new FPDI('L','mm',array(291.6,456));
$fichier=$repertoireGrilles.'GR_IFR.pdf';
$pdf->setSourceFile($fichier);
// add a page
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx,0,0,0);
..
...
methods on $pdf
..
..
..
$pdfCopie=clone $pdf;
methods on $pdfCopie
$pdfCopie-> Output($repertoireGrilles.'grillesQuotidiennes/'.$date.'/Grille_'.$date.'_'.$ou.'_copie.pdf','F');
$pdf-> Output($repertoireGrilles.'grillesQuotidiennes/'.$date.'/Grille_'.$date.'_'.$ou.'.pdf','F');
Anybody to help me to tackle this issue that keeps my brain under high pressure for hours (days) :) ?
Cloning, forking, copying, any of that is really dirty. You will have a very hard time with outputs if you take that route. Instead, consider this approach:
- Make multiple AJAX calls to a single PHP file, pass a
pid
value to it so as to differentiate between them. - Go through the exact same document setup for FPDI. This is far more consistent than cloning, forking, copying, etc.
- Check
pid
and do different things to different documents after all the setup is done. - Output the documents.
Here is my jQuery:
$(document).ready(function(){
var i;
for( i=0; i<=1; i++ )
{
$.ajax({
url: 'pdfpid.php',
data: {
pid: i,
pdf: 'document.pdf'
},
type: 'post'
});
}
});
As you can see, it's pretty simple. pdfpid.php
is the name of the file that will generate and process the documents. In this case, I want the document with a pid
of 0 to be my "original" and the one with a pid
of 1 to be the "cloned" document.
// Ensure that POST came in correctly
if( !array_key_exists('pid',$_POST) || !array_key_exists('pdf',$_POST) )
exit();
// Populate necessary variables from $_POST
$pid = intval($_POST['pid']);
$src = $_POST['pdf'];
// Setup the PDF document
$pdf = new FPDI();
$pdf->setSourceFile($src);
$templateID = $pdf->importPage(1);
$pdf->addPage();
$pdf->useTemplate($templateID);
$pdf->SetFont('Arial','B',24);
switch( $pid )
{
default:
break;
case 0:
// "Parent" document
$pdf->Text(10,10,"ORIGINAL");
$filename = "original.pdf";
break;
case 1:
// "Child" document
$pdf->Text(10,10,"CLONED");
$filename = "cloned.pdf";
break;
}
$pdf->Output($filename,'F');
I got both documents as an output, with the unique modifications between the "parent" and the "child" all in place.
精彩评论