Solved

PHP : writing to PDF using FPDI

Posted on 2013-01-15
25
1,848 Views
Last Modified: 2013-01-21
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

0
Comment
Question by:1Cougar
  • 13
  • 12
25 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38779441
Where's the $thisID ?
Isn't the script writing to the same file twice ?
You need to make sure that variable changes.
0
 

Author Comment

by:1Cougar
ID: 38779514
$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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38779551
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38779566
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
 

Author Comment

by:1Cougar
ID: 38779625
I have changed the code and it only makes one PDF, not two....

??
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38779643
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
 

Author Comment

by:1Cougar
ID: 38779677
Yes, two different names.
0
 

Author Comment

by:1Cougar
ID: 38779683
DOMNINE DIDIER
SYLVIE DUGARD-WENCEWIEZ
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38779704
How abot (inside the function doPDF you DO NOT create an object with a reference ?
$pdf =& new FPDI();
to
$pdf = new FPDI();
0
 

Author Comment

by:1Cougar
ID: 38779715
OK, have changed it and it still only prints one PDF....
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38779745
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38779749
This way it outputs two files (writing them on the disk instead of output directly to the browser).
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38779750
Check you application dir for two pdf files then.
0
 

Author Comment

by:1Cougar
ID: 38779754
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38779782
Yup. You canot use / in you filename
do this:
$pdf->Output("$traineeFullName.pdf", 'F');
inside the doPDF function
0
 

Author Comment

by:1Cougar
ID: 38779810
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38779828
Is the file readable (fpdf.php) and the directory can be written ? Set proper rights to user.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38779829
This seems to be the last issue...
0
 

Author Comment

by:1Cougar
ID: 38779830
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
 

Author Comment

by:1Cougar
ID: 38779864
fpdf17 octal is 777 and fpdf.php octal is 666.
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 400 total points
ID: 38779871
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
 

Author Comment

by:1Cougar
ID: 38779884
What is the "sent header"...
0
 

Assisted Solution

by:1Cougar
1Cougar earned 0 total points
ID: 38782563
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38782610
Thanks
0
 

Author Closing Comment

by:1Cougar
ID: 38800476
I was unable to fix my initial code and had to create a workaround on my end.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Power PDF (http://www.nuance.com/for-business/document-imaging-and-scanning/power-pdf-converter/index.htm) is the newest product from the Document Imaging division of Nuance Communications (http://www.nuance.com/). It is available in two editions — …
In a previous article published here at Experts Exchange, Signature Image with Transparent Background (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_12380-Signature-Image-with-Transparent-Background.html), I explained how to cre…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
We often encounter PDF files that are pure images, that is, they do not have text characters, but instead contain only raster graphics. The most common causes of this are document scanning software and faxing software/services that create image-only…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now