PHP : writing to PDF using FPDI

Hello,

I am successfully using FPDI to create PDFs with PHP.  What I would like to do is loop through a recordset and create separate PDFs for each record.  

I was able to create one PDF with several pages, but I would like to be able to output to a pdf after looping through each record.  It does it once and then stops.  I can't figure out what I am doing wrong and I am rather new to PHP.  I think I have to perhaps reset the FPDI object?

I am sure there are two records as it was working fine when I outputted both to 1 PDF, but I would like 2 separate PDFs now.

My code is below.

Thanks for any suggestions as to why this would execute just once.

Cheers,

$rs=$conn->execute($query1);


for ($i=1; $i<=2; $i++) 
//carry on looping through while there are records
{
$companyName = strval($rs['CompanyName']);
$firstLesson = strval($rs['FirstLesson']);
$lastLesson = strval($rs['LastLesson']);
$contractHours = intval($rs['RevisionUnits']);
$today = date("d/m/Y");
$traineeName = strval($rs['TraineeFullName']);
$traineeName = explode(',',$traineeName);
$traineeFullName = strtoupper($traineeName[0])." ".$traineeName[1];
$traineeLastName = $traineeName[1];

doPDF($thisID,$companyName,$firstLesson,$lastLesson,$contractHours,$today,$traineeFullName);
$rs->MoveNext();	
}


function doPDF($thisID,$companyName,$firstLesson,$lastLesson,$contractHours,$today,$traineeFullName){


//initialize PDF
$pdf =& new FPDI();
//$pdf =& new FPDI(); 
// add a page 
$pdf->AddPage();

// set the sourcefile 
$pdf->setSourceFile('AttestationTemplate.pdf'); 
// import page 1 
$tplIdx = $pdf->importPage(1); 
// use the imported page as the template 
$pdf->useTemplate($tplIdx, 0, 0); 

// write the Company Name, Contract ID, Hours and Year


$pdf->SetTextColor(0,0,0);
$pdf->SetFont('Arial', 'B', '14');
$pdf->SetXY(32,125);
//$pdf->SetFillColor(21,64,185);
$pdf->Cell(150,10,$traineeFullName,0,1,"C");
$pdf->SetXY(32,135);
//$pdf->SetFillColor(21,64,185);
$pdf->SetFont('Arial','', '10.5');
$pdf->Cell(150,10,"de",0,1,"C");
$pdf->SetXY(32,145);
$pdf->SetFont('Arial', 'B', '14');
//$pdf->SetFillColor(21,64,185);
$pdf->Cell(150,10,$companyName,0,1,"C");

$pdf->SetXY(185,50);
$pdf->SetFont('Arial', 'B', '14'); 
//$pdf->Write(0,$currYear);
$pdf->SetXY(35,50);
//$pdf->Write(0,$teacherName);
$pdf->SetFont('Arial','', '10.5'); 
$pdf->SetXY(40,167);
$pdf->Write(0,$contractHours);
$pdf->SetXY(123.75,167);
$pdf->Write(0,$firstLesson);
$pdf->SetXY(149.5,167);
$pdf->Write(0,$lastLesson);
$pdf->SetXY(38.9,194.75);
$pdf->Write(0,$today);

$x = 35;
$x1 = 80;
$x2 =173;
$y2 = 85;
$y = 25;
$y2base = 85;

$pdf->Output("$companyName-$thisID-$traineeFullName-Attestation-$today", 'D');
$pdf = NULL;
unset($pdf);
return;
}

Open in new window

1CougarAsked:
Who is Participating?
 
Lukasz ChmielewskiConnect With a Mentor Commented:
Then I doubt if you can output sent headers twice.
Possibly, the first pdf will be created and outputted to browser
The possibilities of outputting the file id I,D,F,S
standard output, download file, save to file, return as string
0
 
Lukasz ChmielewskiCommented:
Where's the $thisID ?
Isn't the script writing to the same file twice ?
You need to make sure that variable changes.
0
 
1CougarAuthor Commented:
$thisID is the same for the two PDFs that the code should be creating--it is only used in the filename.  It is the traineeFullName that is different for each one.   Even if the file was being written twice, I would have two identical files, wouldn't I?  Only one file is created.  It does not output twice even though if I put an "echo" in the code I see that the function is called twice.  I thought I had to somehow destroy the pdf object after it was outputted.

Any other thoughts?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Lukasz ChmielewskiCommented:
This seems inconsistent

$rs=$conn->execute($query1);

for ($i=1; $i<=2; $i++)
//carry on looping through while there are records
{
...
$rs->MoveNext();      
}

maybe you loop unnecessarily, when you have $rs->MoveNext();
0
 
Lukasz ChmielewskiCommented:
Can you try

$rs=$conn->execute($query1);

if ($rs) 
while (!$rs->EOF) { 

$companyName = strval($rs['CompanyName']);
$firstLesson = strval($rs['FirstLesson']);
$lastLesson = strval($rs['LastLesson']);
$contractHours = intval($rs['RevisionUnits']);
$today = date("d/m/Y");
$traineeName = strval($rs['TraineeFullName']);
$traineeName = explode(',',$traineeName);
$traineeFullName = strtoupper($traineeName[0])." ".$traineeName[1];
$traineeLastName = $traineeName[1];

doPDF($thisID,$companyName,$firstLesson,$lastLesson,$contractHours,$today,$traineeFullName);
                
$rs->MoveNext(); 
}

Open in new window

0
 
1CougarAuthor Commented:
I have changed the code and it only makes one PDF, not two....

??
0
 
Lukasz ChmielewskiCommented:
Could you just check this:

$rs=$conn->execute($query1);

if ($rs) 
while (!$rs->EOF) { 

$companyName = strval($rs['CompanyName']);
$firstLesson = strval($rs['FirstLesson']);
$lastLesson = strval($rs['LastLesson']);
$contractHours = intval($rs['RevisionUnits']);
$today = date("d/m/Y");
$traineeName = strval($rs['TraineeFullName']);
$traineeName = explode(',',$traineeName);
$traineeFullName = strtoupper($traineeName[0])." ".$traineeName[1];
$traineeLastName = $traineeName[1];

doPDF($thisID,$companyName,$firstLesson,$lastLesson,$contractHours,$today,$traineeFullName);

echo $traineeFullName."<br />";
                
$rs->MoveNext(); 
}

Open in new window


Does this show two full names ?
0
 
1CougarAuthor Commented:
Yes, two different names.
0
 
1CougarAuthor Commented:
DOMNINE DIDIER
SYLVIE DUGARD-WENCEWIEZ
0
 
Lukasz ChmielewskiCommented:
How abot (inside the function doPDF you DO NOT create an object with a reference ?
$pdf =& new FPDI();
to
$pdf = new FPDI();
0
 
1CougarAuthor Commented:
OK, have changed it and it still only prints one PDF....
0
 
Lukasz ChmielewskiCommented:
I think I got that:
$pdf->Output("$companyName-$thisID-$traineeFullName-Attestation-$today", 'D');
change it to
$pdf->Output("$companyName-$thisID-$traineeFullName-Attestation-$today", 'F');
0
 
Lukasz ChmielewskiCommented:
This way it outputs two files (writing them on the disk instead of output directly to the browser).
0
 
Lukasz ChmielewskiCommented:
Check you application dir for two pdf files then.
0
 
1CougarAuthor Commented:
Error:

Warning: fopen(Clear Channel France-1599-DOMNINE DIDIER -Attestation-15/01/2013) [function.fopen]: failed to open stream: No such file or directory in C:\HostingSpaces\...\fpdf17\fpdf.php on line 1025
FPDF error: Unable to create output file: Clear Channel France-1599-DOMNINE DIDIER -Attestation-15/01/2013
0
 
Lukasz ChmielewskiCommented:
Yup. You canot use / in you filename
do this:
$pdf->Output("$traineeFullName.pdf", 'F');
inside the doPDF function
0
 
1CougarAuthor Commented:
Still this error:

Warning: fopen(DOMNINE DIDIER .pdf) [function.fopen]: failed to open stream: Permission denied in C:\HostingSpaces\...fpdf17\fpdf.php on line 1025
FPDF error: Unable to create output file: DOMNINE DIDIER .pdf
0
 
Lukasz ChmielewskiCommented:
Is the file readable (fpdf.php) and the directory can be written ? Set proper rights to user.
0
 
Lukasz ChmielewskiCommented:
This seems to be the last issue...
0
 
1CougarAuthor Commented:
I remember now why I used "D".  Since our site is hosted remotely, we do not have permission to add files.  So, "F" argument did not work for us.
0
 
1CougarAuthor Commented:
fpdf17 octal is 777 and fpdf.php octal is 666.
0
 
1CougarAuthor Commented:
What is the "sent header"...
0
 
1CougarConnect With a Mentor Author Commented:
Hello,

I did solve my problem but was unable to output the pdf twice directly from the php code above, as I had wanted to do.  I decided to send the trainee name from my code that calls the php file that creates the pdf, which works instead of trying to output twice from within the php code.

Thank you VERY MUCH for your help and I will give you credit for part of the solution as I think you are right that one cannot send headers twice.

Cheers,
0
 
Lukasz ChmielewskiCommented:
Thanks
0
 
1CougarAuthor Commented:
I was unable to fix my initial code and had to create a workaround on my end.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.