2

I have a form like the following invoice

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
 dir="<?php echo $direction; ?>"
 lang="<?php echo $lang; ?>"
 xml:lang="<?php echo $lang; ?>">
 <head> <title><?php echo $title;
 ?></title> <link rel="stylesheet"
 type="text/css" href="theme/<?php echo
 $template;
 ?>/stylesheet/stylesheet.css" />
 <style type="text/css"> body {
 background: #FFFFFF; } </style>
 </head> <body>

 <div style="page-break-after: always;
 "> <table width="100%">   <tr>
     <td width="auto" align="left"><a href="<?php echo $url_logo; ?>"
 title="<?php echo $_name; ?>"><?php
 echo "<img src=".$logo." alt=\"Logo
 Store\" style=\"border: none;\" />";
 ?></a></td>
     <td width="*" valign="middle"><h1><?php echo
 $heading_title; ?></h1></td>   </tr>
 </table><div class="line"></div>  
 <div class="div_">
     <table width="100%">
       <tr>
         <td>  
           <?php echo $_address; ?><br />
          <?php if ($telephone) { ?>
           <?php echo $store_telephone; ?><br />
           <?php } ?>
           <?php echo $_email; ?><br />         </td>
         <td align="right" valign="top">        <table>
             <?php if ($invoice_id) { ?>
             <tr>
               <td><b><?php echo $txt_invoice_id; ?></b></td>
               <td>:&nbsp;<?php echo $invoice_id; ?></td>
             </tr>
             <?php } ?> <?php if ($historys) { ?> <?php foreach ($historys as $history) { ?
             <tr>
               <td><b><?php echo $column_date_added; ?></b></td>
               <td>:&nbsp;<?php echo $history['date_added']; ?></td>
             </tr>
             <tr>
               <td><b><?php echo $column_status; ?></b></td>
               <td>:&nbsp;<?php echo $history['status']; ?></td>
             </tr>      <?php } ?>      <?php } ?>
             <tr>
               <td><b><?php echo $txt_order_id; ?></b></td>
               <td>:&nbsp;<?php echo $order_id; ?></td>
             </tr>      <?php if ($shipping_method) { ?>
             <tr>
               <td><b><?php echo $txt_shipping_method; ?></b></td>
               <td>:&nbsp;<?php echo $shipping_method; ?></td>
             </tr>      <?php } ?>
             <tr>
               <td><b><?php echo $txt_payment_method; ?></b></td>
               <td>:&nbsp;<?php echo $payment_method; ?></td>
             </tr>
           </table>
          </td>
       </tr>
     </table>   </div> </div> </body> </html>

invoice form in access through http://www.mysite.com/index.php?print=page/invoice&id=952 and invoice extension is .tpl

I've tried using FPDF (http://www.fpdf.org/), but failed, and it seems FPDF not match the css, then I tried to use tcpdf (http://www.tcpdf.org) regarding from http://www.tcpdf.org/examples/example_054.phps but are visible only error with php code: e.g. <?php echo $history['status']; ?>

Does anybody already have experience or have a some problem? please share here, or have the same sample? thanks in advance

user485783
  • 1,685
  • 3
  • 11
  • 12
  • You want to generate a PDF from HTML code, correct? – Pekka Dec 29 '10 at 08:54
  • 2
    try using dompdf. http://code.google.com/p/dompdf/ – Kinjal Dixit Dec 29 '10 at 09:13
  • @ Pekka, I want to generate pdf from invoice page as shown above, inside contain some php code, thanks – user485783 Dec 29 '10 at 09:50
  • @kinjal, i heard that dompdf have many issues security bug, e.g. exploit. and many software developer removed dompdf from their software regarding that security issue – user485783 Dec 29 '10 at 09:57
  • possible duplicate of [Converting html to pdf in php ?](http://stackoverflow.com/questions/4542230/converting-html-to-pdf-in-php) – moinudin Dec 29 '10 at 10:41
  • There are some PHP libraries that generate PDF from HTML code pretty well (including inline styles). I know we used something in our past project and it worked well. I don't remember the name though. – Richard Knop Dec 29 '10 at 10:46
  • ah, Richard Knop, perhaps You still remember how to convert it? thanks – user485783 Dec 29 '10 at 11:15
  • If the primary purpose is to create a PDF file which does not have to appear exactly as it would in the web preview, maybe you should seriously consider formatting it using TeX. Converting TeX to PDF is fairly simple task, so the focus will shift from finding a HTML to PDF converter to formatting the invoice in TeX. – Kinjal Dixit Dec 29 '10 at 18:35

3 Answers3

3

The big problem is, that there is no library which can handle html code that well.

dom-pdf is a very good one, but have still huge problems with nested tables an page breaks. for your reason it should do the trick, I guess. It can also handle css a bit.

to the security issue: To resolve the security issue of DOMPDF, all that is needed is to modify DOMPDF slightly to only function in cgi mode, preventing any access from external sources and only able to be called from your application.

if you can create the table yourself, I strongly recommend using fpdf. you will not be able to handle any css, but there are many formatting possibilities with fpdf.

for dom-pdf you have a link from kinjal above. and fpdf you have already found.

helle
  • 11,183
  • 9
  • 56
  • 83
  • "there is no library which can handle html code that well" - so leave it to a tried and tested rendering engine like I suggest. – moinudin Dec 29 '10 at 10:48
  • Hi, i am not familiar with anything of pdf library, but use fpdf seem hard code to me, because everything need to create first, dompdf seem easy than fpdf but lot of security risk, and now i want to see another alternative e.g. tcpdf, html2pdf (http://html2pdf.fr/en/default) or wkhtmltopdf does someone have experience with? thanks for you suggestions – user485783 Dec 29 '10 at 11:08
  • if it is a matter of time, i gues you'll need same time for setting up one of the later suggestions, and finding into the fpdf-class. hint for that: concentrate on multi-cell rather than cell. – helle Dec 29 '10 at 11:11
  • 2
    dompdf is fairly straight-forward to use. There is one security issue, but it is dependent on your configuration and fairly easy to work around (as helle mentioned). Plus, it would be easy to convert your invoice by just passing in the URL for the invoice. – BrianS Dec 29 '10 at 18:14
3

As Helle said, FPDF is a really neat tool for generating PDFs from PHP. Although using FPDF you'll have to dig into its manual, as it doesn't convert HTML into PDFs. From my experience though, FPDF is rather straightforward to use and you should not have too much trouble with it.

Peter Perháč
  • 20,434
  • 21
  • 120
  • 152
1

wkhtml uses the WebKit rendering engine and as such produces fantastic results. See my answer on Converting html to pdf in php? for further details.

Community
  • 1
  • 1
moinudin
  • 134,091
  • 45
  • 190
  • 216
  • yes it seem great, but i still searching the sample regarding of my invoice code above, could you have idea? thanks – user485783 Dec 29 '10 at 11:10
  • @user What file format is the invoice in? We are all assuming HTML, but perhaps it isn't. – moinudin Dec 29 '10 at 11:14
  • the file format of my invoice is .tpl use as template site, inside the .tpl file have a lot php code as shown above. – user485783 Dec 29 '10 at 11:17