0

I need to fill in a PDF, in the fly, using PHP - have no idea where to start.

I'm currently doing quizzes on line using PHP - once a series of quizzes are passed the client wants to let the user download a 'certificate of completion'

The PDF of the certificate has blank lines for the users name and the area of study.

My thought is - add 2 form elements to the PDF, and have PHP pill them in when I pull of the certificate.

BUT HOW?

Is there a different, better way?

Things I need to be 'cautious' of - installing third party stuff is not reliable, UNLESS I can just drop a lib in the the site root. I can't guarantee the hosting provider will let me change a PHP config.

Any help is appreciated - the more specific, the better.

Sorry I have no code at the moment - other than how I'm currently displaying the PDF -

// show cert
echo '<iframe src="1_cert.pdf" width="1000" height="700">';

Thanks.

OK - using TCPDF - as suggested - I've installed, and example pages work... I've placed a file in the example folder.... I've included a call to import...

require_once('../tcpdf_import.php');

// create new PDF document
$pdf = new TCPDF_IMPORT('1_cert.pdf');

...other boiler plate copied form other examples...

$pdf->SetDisplayMode('fullpage', 'SinglePage', 'UseNone');

// set font
$pdf->SetFont('times', 'B', 20);

$pdf->setPage(1, true);
$pdf->SetY(50);
$pdf->Cell(0, 0, 'test text', 1, 1, 'C');

$pdf->lastPage();

The error I'm getting "TCPDF ERROR: Wrong page number on setPage() function: 1"

j-p
  • 3,698
  • 9
  • 50
  • 93

1 Answers1

0

Ok, if you're not showing code, then neither will I. ;)

I would recommend doing it with TCPDF.

  1. Import the PDF with TCPDF_Import
  2. In a PDF document, you can navigate to any x/y position, (NB: 0/0 is bottom left). Therefore, simply set your “cursor” to the position to each field, and insert a text with either the Cell or the writeHTMLCell method.
  3. Save the PDF document.
  4. Display it to the user.

Voilà.

By the way, both FPDI and TCPDF are common PHP libraries, so you can just put them somewhere in your base folder, no additional tools should be required on a common web server.

lxg
  • 12,375
  • 12
  • 51
  • 73
  • I must admit that I haven't worked with TCPDF_Import before. But TCPDF itself is a very nice PDF generator for PHP (but don't look at the source code, it causes eye cancer). So I figured that they should be handling the PDF import just fine, too. – lxg Sep 14 '14 at 20:36
  • if I HAD code at this point - I'd show... but .. I'll try what you suggest and will have code at that point – j-p Sep 14 '14 at 21:33
  • hum.. reading your instructions - maybe I missed something or left something out of my description. I want to PROGRAMMATICALLY populate a PDF... Does your step 2 imply that creating a cell in the PDF creates an element that will be fillable via PHP ? – j-p Sep 14 '14 at 21:35
  • You don't need to use PDF forms. You can simply open a PDF file, then you will have it as PHP object. Then you can use TCPDF methods to add whatever you want to the file. http://www.tcpdf.org/doc/code/classTCPDF.html – lxg Sep 14 '14 at 21:43
  • As I said, use the TCPDF_IMPORT class, it extends the TCPDF class. The class file tcpdf_import.php is in the root folder of TCPDF. After you have loaded the PDF file with TCPDF_IMPORT, you can use it as if it had been created with TCPDF, i.e. you can use all methods of the TCPDF class. – lxg Sep 14 '14 at 22:54
  • see above for code addition - The error I'm getting "TCPDF ERROR: Wrong page number on setPage() function: 1" – j-p Sep 18 '14 at 06:20