How to create a pdf file for download with PHP

Hi there,
been looking a straight forward way of doing the following but am getting lost with all the difference options and would prefer to ask the experts.

My users do a test, and once they pass they get a certificate stating that they have passed this test. I would like to create a pdf certificate file they can download. It will be a simple file with a certificate template  and the users details added dynamically (user name, test, result  and date).

What is the easiest way of doing this?

Thanks in advance
Williery
williery10Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GauthierCommented:
A few years back in 2008, after the same struggle you had, I chose fpdf_eps
http://valentin.dasdeck.com/php/fpdf/fpdf_eps/
which extend tfpdf
https://github.com/rev42/tfpdf
Essentially, I made the template in adobe illustrator.
Loaded that in the pdf with fpdf_eps then placed my variable text on the page with the underlying tfpdf.

The advantage are:
- I do not had to purchase the php pdflib (I did not have to master it either...)
- I can make the AI document as complex as I want.
- I can keep the file to a vector version which is small. (most other free library only support images)

It would be unsuitable to complex variable text document, but I managed to do an invoice with a single line item with no problem and make it a 40Kb pdf instead of a 500Kb monster while writing less than 100 lines of php code.

Since then, the situation for php may have improved, but the doc doesn't suggest it...

If I had not found the fpdf_eps, I would have chose to patch an existing pdf with text substitution.
Put the fact that the pdf encode it's stream made that a last resort idea.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
I've used both FPDF and TCPDF to do similar things.  You can download FPDF here:
http://www.fpdf.org/

Here is my "hello world" example in FPDF.
<?php // demo/fpdf_hello_world.php
error_reporting(E_ALL);


// DEMONSTRATE SOME OF THE BASICS OF FPDF


// SOME VARIABLES FOR OUR TESTS (COULD COME FROM DATA BASE, ETC)
$font = 'Arial';
$text = 'Hello World!';

// BRING IN THE PDF THING
require_once('fpdf16/fpdf.php');

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= 'storage'
. DIRECTORY_SEPARATOR
. 'temp_pdf_blue'
. '.pdf'
;
$pdf_file_name
= getcwd()
. DIRECTORY_SEPARATOR
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN WHITE ON A BLUE BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 16);
$pdf->SetFillColor(  0,   0, 255);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(40, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Blue PDF</a>';



// START OVER FOR A NEW PDF
unset($pdf);
echo PHP_EOL . "<br/>";

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= 'storage'
. DIRECTORY_SEPARATOR
. 'temp_pdf_red'
. '.pdf'
;
$pdf_file_name
= getcwd()
. DIRECTORY_SEPARATOR
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN BLACK ON A RED BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 24);
$pdf->SetFillColor(255,   0,   0);
$pdf->SetTextColor(  2,   2,   2);
$pdf->Cell(52, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Red PDF</a>';

Open in new window

0
GauthierCommented:
@Ray Pasteur
fPDF and some of their fork (mPDF) were the first choices I examined. Using them, I would either end up with a 2MB pdf with a big jpg background or 50000 lines of php code to draw all the lines, arcs, texts I needed to maintain the corporate identity, svg logo etc.
Support of EPS to act as a template is critical to this kind of work.
0
Ray PaseurCommented:
Whatever.  

I've never seen anything that created a 2MB PDF or a need for "50000 lines of php code" -- in fact, I've used these classes in production deployments with great satisfaction.  They require a work effort that takes meticulous attention to detail (all document layouts require this), and once they are working, they are both fast and accurate, down to 1/100 inch.  This seems to be one of those technical hurdles that the industry has already jumped, and does not need to jump again.

I guess the central issue is "Does the client need to embed vector-based Adobe Illustrator (AI) or AI compatible EPS files?"  For a certificate application with a template and some text-based insertions, the base classes might be enough to do the job.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

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.