I have a php file with html forms and variables in php.

I generate a policy document that is displayed in the web page and is then created in PDF and emailed to the client.
I create the PSF via using EZPDF but This runs slow when creating the policy and whenever we have to make changes to the document it becomes a huge mission as each element of the PDF file has to be drawn on X and Y coordinates.

Is there a way that I could print this what is displayed on the web page directly to a PDF?

I have added a little excerpt from my current method below:

HTML File Displayed

            <td class="<?php print $Title_error;?>">Title</td>
            <td><input name="Title" type="text" id="Title" tabindex=2 style="color: <?php print $TextColour;?>;" value="<?php print $_SESSION['Title'] ;?>" size="30" readonly/></td>

            <td class="<?php print $Gender_error;?>">Gender</td>
            <td><input name="Gender" type="text" id="Gender" tabindex=5 style="color: <?php print $TextColour;?>;" value="<?php print $_SESSION['Gender'] ;?>" size="30" readonly/></td>
            <td class="<?php print $FirstName_error;?>">First Name</td>
            <td><input name="FirstName" type="text" id="FirstName" tabindex=3 style="color: <?php print $TextColour;?>;" value="<?php print $_SESSION['FirstName'] ;?>" size="30" readonly/></td>

            <td class="<?php print $Surname_error;?>">Surname</td>
            <td><input name="Surname" type="text" id="Surname" tabindex=4 style="color: <?php print $TextColour;?>;" value="<?php print $_SESSION['Surname'] ;?>" size="30" readonly /></td>

PDF Creation


//Set Color to White 

$pdf->addText(230,744,10,"DETAILS OF MAIN MEMBER");

//Set Color back to black 



$pdf->addText(80,704,8,"First Name");


So As you can see in the pdf create snippet above, If I had to add a new field in between firstName and Surname the whole page's coordinates would have to change, there has to be a better way.

2 Years
Discussion Span
Last Post by fabzster

Hmm, interesting, I was doing something similar not so long ago. I found vanilla writing coords to pdf just a royal pain in the ass. It is impossible to make changes without changing every other previous coordinate.

Text doesn't wrap properly either and overflows the boxes without specific instruction.

Best bet is to either use phpexcel and save to an excel sheet then convert to pdf. This at least makes it super easy to add new fields without worrying about affecting the others.

Another realistic option would be to use dompdf, so render out your file as a html document then simply use dompdf to convert to a pdf.

I personally think these are your only options, if pushed I would go for dompdf, trouble is you would need to recreate your template documents in html and css - this might be an issue if you have hundreds of documents.

In that way dumping it to excel then running an excel to pdf conversion might be better.

Edited by iamthwee


I was using MPDF for printing php to pdf. It is easy to setting and to use , just simply copy the MPDF to your project directory.

The liblary size ( About 30MB ) is causing this one also load quite slow. By the way this liblary is a good one because easy to use.

Example code :

$mpdf = new mPDF('utf-8',    // mode - default ''
     '',    // format - A4, for example, default ''
     0,     // font size - default 0
     '',    // default font family
     15,    // margin_left
     15,    // margin right
     55,     // margin top
     15,    // margin bottom
     5,     // margin header
     5,     // margin footer
     'L');  // L - landscape, P - portrait
$mpdf->useOddEven = 1;
$html = '
@page {
  size: auto;
  odd-header-name: html_myHeader2;
  even-header-name: html_myHeader2;
  odd-footer-name: html_myFooter1;
  even-footer-name: html_myFooter1;
<htmlpageheader name="myHeader2" style="display:none">
<div style="border-bottom: 1px solid #000000; font-weight: bold;  font-size: 10pt;">
<htmlpagefooter name="myFooter1" style="display:none">
<div style="border-top: 1px solid #000000; font-weight: bold; font-size:10pt;">
this is  <b>footer</b>
example php looping : <br/>';

for($i = 1; $i <= 30 ; $i ++){
$html .=  $i . '<br/>';

$html .=  '</body></html>';




Many thanks for the replies, Currently the values sit in variables that are printed in HTML inputs. If I use something to convert the html to PDF then wouldnt I lose the data in the inputs


yes but there is php code in them printing variables

<input name="Title" type="text" id="Title" tabindex=2 style="color: <?php print $TextColour;?>;" value="<?php print $_SESSION['Title'] ;?>" size="30" readonly/>

i havnt used dompdf as yet but I have setup ezpdf to print out what I need.

I was just wondering if i took an html page that had inputs that were populated by php code if they would display in the converted pdf

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.