print to pdf with php?

13,755

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
13,755

Related videos on Youtube

user485783
Author by

user485783

Updated on June 04, 2022

Comments

  • user485783
    user485783 almost 2 years

    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

  • moinudin
    moinudin over 13 years
    "there is no library which can handle html code that well" - so leave it to a tried and tested rendering engine like I suggest.
  • user485783
    user485783 over 13 years
    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 (html2pdf.fr/en/default) or wkhtmltopdf does someone have experience with? thanks for you suggestions
  • user485783
    user485783 over 13 years
    yes it seem great, but i still searching the sample regarding of my invoice code above, could you have idea? thanks
  • user485783
    user485783 over 13 years
    i tried this for several time, but i not get any luck yet, it seem hard code to me.
  • helle
    helle over 13 years
    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.
  • moinudin
    moinudin over 13 years
    @user What file format is the invoice in? We are all assuming HTML, but perhaps it isn't.
  • user485783
    user485783 over 13 years
    the file format of my invoice is .tpl use as template site, inside the .tpl file have a lot php code as shown above.
  • BrianS
    BrianS over 13 years
    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.

Related