Hi

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

<table>
        <tr>
            <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>
        </tr>
        <tr>
            <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

//DETAILS OF MAIN MEMBER
$pdf->filledRectangle(50,740,500,15);

//Set Color to White 
$pdf->setColor(255,255,255);

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

//Set Color back to black 
$pdf->setColor(0,0,0);

$pdf->Rectangle(50,720,100,15);
$pdf->addText(93,724,8,"Title");
$pdf->addText(160,724,8,"{$_SESSION['Title']}");

$pdf->Rectangle(300,720,100,15);
$pdf->addText(335,724,8,"Gender");
$pdf->addText(410,724,8,"{$_SESSION['Gender']}");

$pdf->Rectangle(50,700,100,15);
$pdf->addText(80,704,8,"First Name");
$pdf->addText(160,704,8,"{$_SESSION['FirstName']}");

$pdf->Rectangle(300,700,100,15);
$pdf->addText(334,704,8,"Surname");
$pdf->addText(410,704,8,"{$_SESSION['Surname']}");

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.

Recommended Answers

All 7 Replies

Member Avatar for iamthwee

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.

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 :

<?php
include("mpdf.php");
$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 = '
<html>
<head>
<style>
@page {
  size: auto;
  odd-header-name: html_myHeader2;
  even-header-name: html_myHeader2;
  odd-footer-name: html_myFooter1;
  even-footer-name: html_myFooter1;
}
</style>
</head>
<body>
<htmlpageheader name="myHeader2" style="display:none">
<div style="border-bottom: 1px solid #000000; font-weight: bold;  font-size: 10pt;">
header      
</div>
</htmlpageheader>
<htmlpagefooter name="myFooter1" style="display:none">
<div style="border-top: 1px solid #000000; font-weight: bold; font-size:10pt;">
this is  <b>footer</b>
</div>
</htmlpagefooter>
example php looping : <br/>';

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

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

$mpdf->WriteHTML($html);

$mpdf->Output();
?>

Hi

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

Member Avatar for iamthwee

Sorry I don't understand, aren't your input just text boxes?

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/>
Member Avatar for iamthwee

Does your pdf look exactly like your html form?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.