Export to PDF using PHP with TCPDF

In the following script you will learn how to easily create dynamic PDF using PHP without additional software. You can either save them on the server or send them directly to the user in the browser. Here I would like to demonstrate it using the example of an invoice, which is created by PHP and output as PDF.

Create PDF document via TCPDF

tcpdf is a free library for creating PDF documents in PDF. The library is quite extensive, but with the right code base you can easily create your own PDF documents. It is particularly convenient that HTML code can be translated into PDF. So you do not have to learn complicated API commands, but you can write your dynamic page as HTML code and create from it via tcpdf into a PDF document.

Download the tcpdf library as the first script and unzip it accordingly, eg in the folder ‘tcpdf’.

Create PDF using PHP.

Subsequent code first generates HTML code for the bill. This HTML code is then translated into a PDF and sent to the user in the browser.

<!--?php &lt;/p&gt; &lt;p&gt; $bill_number = "743" ;&lt;br ?--> $invoice_date = date ( "d.m.Y" ) ;
$delivery_date = date ( "d.m.Y" ) ;
$pdfAuthor = "you company" ;

$invoice_header = '
<img src="logo.png" />
Your company
Your address
Your Site'
;

$recipient = 'Name
Address
Zip'
;

$Invoices_footer = "We ask for a settlement of the bill within 4 days after receipt. Please transfer the full amount to::

<b>name:</b> name
<b>IBAN</b>: 132 165 4651 6464
<b>BIC</b>: CA56464"
;

$invoice = array (
array ( "Pruduct 1" , 1 , 40.50 ) ,
array ( "Pruduct 2" , 5 , 5.20 ) ,
array ( "Pruduct 3" , 3 , 10.00 ) ) ;

$value_added_tax = 0.0 ;

$pdfName = "Invoice_" . $bill_number . ".pdf" ;

$html = '
<table style="width: 100%;" cellspacing="0" cellpadding="5">
<tbody>
<tr>
<td>'
. nl2br ( trim ( $invoice_header ) ) . '</td>
<td style="text-align: right;">Bill number '
. $bill_number . '
Invoice date: '
. $invoice_date . '
Delivery date: '
. $delivery_date . '</td>
</tr>
<tr>
<td style="font-size: 1.3em; font-weight: bold;">invoice</td>
</tr>
<tr>
<td colspan="2">'
. nl2br ( trim ( $recipient ) ) . '</td>
</tr>
</tbody>
</table>
&nbsp;

'
;$total_price = 0 ;foreach ( $invoice as $entry ) {
$amount = $entry [ 1 ] ;
$single_price = $entry [ 2 ] ;
$price = $amount * $single_price ;
$total_price += $price ;
$html . = '' ;
}
$html . = "
<table style="
width: 100%;" border="0" cellspacing="0" cellpadding="5">
<tbody>
<tr style="
background-color: #cccccc; padding: 5px;">
<td style="
padding: 5px;"><b>description</b></td>
<td style="
text-align: center;"><b>amount</b></td>
<td style="
text-align: center;"><b>Price</b></td>
<td style="
text-align: center;"><b>price</b></td>
</tr>
<tr>
<td>' . $entry [ 0 ] . '</td>
<td style="
text-align: center;">' . $entry [ 1 ] . '</td>
<td style="
text-align: center;">' . number_format ( $entry [ 2 ] , 2 , ',' , '' ) . ' Euro</td>
<td style="
text-align: center;">' . number_format ( $price , 2 , ',' , '' ) . ' Euro</td>
</tr>
</tbody>
</table>
"
;

$html . = '

<hr />

'
;if ( $value_added_tax &gt; 0 ) {$erm = $total_price / ( 1 + $value_added_tax ) ;$value_added_tax_amount = $total_price - $erm ;$html . = '' ;
}$html . = '
<table style="width: 100%;" border="0" cellspacing="0" cellpadding="5">
<tbody>
<tr>
<td colspan="3">Subtotal</td>
<td style="text-align: center;">'
. number_format ( $erm , 2 , ',' , '' ) . ' Euro</td>
</tr>
<tr>
<td colspan="3">value_added_tax ('
. intval ( $value_added_tax * 100 ) . '%)</td>
<td style="text-align: center;">'
. number_format ( $value_added_tax_amount , 2 , ',' , '' ) . ' Euro</td>
</tr>
<tr>
<td colspan="3"><b>Total: </b></td>
<td style="text-align: center;"><b>'
. number_format ( $total_price , 2 , ',' , '' ) . ' Euro</b></td>
</tr>
</tbody>
</table>
'
;

if ( $value_added_tax == 0 ) {
$html . = 'according to ......

'
;
}

$html . = nl2br ( $invoice_footer ) ;

// TCPDF Library load
require_once ( 'tcpdf/tcpdf.php' ) ;

$pdf = new TCPDF ( PDF_PAGE_ORIENTATION , PDF_UNIT , PDF_PAGE_FORMAT , true , 'UTF-8' , false ) ;

$pdf -&gt; SetCreator ( PDF_CREATOR ) ;
$pdf -&gt; SetAuthor ( $pdfAuthor ) ;
$pdf -&gt; SetTitle ( 'Invoice ' . $bill_number ) ;
$pdf -&gt; SetSubject ( 'Invoice ' . $bill_number ) ;

$pdf -&gt; setHeaderFont ( Array ( PDF_FONT_NAME_MAIN , '' , PDF_FONT_SIZE_MAIN ) ) ;
$pdf -&gt; setFooterFont ( Array ( PDF_FONT_NAME_DATA , '' , PDF_FONT_SIZE_DATA ) ) ;

$pdf -&gt; SetDefaultMonospacedFont ( PDF_FONT_MONOSPACED ) ;

$pdf -&gt; SetMargins ( PDF_MARGIN_LEFT , PDF_MARGIN_TOP , PDF_MARGIN_RIGHT ) ;
$pdf -&gt; SetHeaderMargin ( PDF_MARGIN_HEADER ) ;
$pdf -&gt; SetFooterMargin ( PDF_MARGIN_FOOTER ) ;

$pdf -&gt; SetAutoPageBreak ( TRUE , PDF_MARGIN_BOTTOM ) ;

$pdf -&gt; setImageScale ( PDF_IMAGE_SCALE_RATIO ) ;

$pdf -&gt; SetFont ( 'dejavusans' , '' , 10 ) ;

$pdf -&gt; AddPage ( ) ;

$pdf -&gt; writeHTML ( $html , true , false , true , false , '' ) ;

$pdf -&gt; Output ( $pdfName , 'I' ) ;

//Option 2: Save PDF in the directory:
//$pdf-&gt;Output(dirname(__FILE__).'/'.$pdfName, 'F');
//echo 'Download PDF: <a href="'.$pdfName.'">'.$pdfName.'</a>';

?&gt;

 

In case you don’t see the code clear you can download it here and use it invoice_template

If you do not download the script from Github , make sure the require_once ( ‘tcpdf / tcpdf.php’ ) ; works, ie that the file tcpdf.php is located in the subfolder tcpdf .

At the beginning of the script, you can specify some data of your bill. For example, the invoice number, the invoice date or the address of the recipient. In these lines you can use normal HTML, so for example an image can be integrated into the PDF via the img tag.

The variable $ billing_posts lists the purchased products, each with designation, quantity and gross unit price. The script calculates the total costs accordingly.

You can enter a VAT rate in the variable $ VAT . For example, if 0.19 is stored in this variable, 19% VAT will be shown on the invoice. The calculation is automatic.

The content of the PDF document is defined in the variable $ html . Here, ordinary HTML code specifying the content and appearance of the PDF is specified. The HTML code can be easily and conveniently adapted to your application. TCPDF supports many HTML commands, as well as some inline CSS commands. However, in comparison to the browser, the scope is quite rudimentary. This is absolutely sufficient for the generation of simple PDF documents. But you should not hope that a decent PDF file can be generated from the HTML code of your website with all the CSS commands.

To save the PDF you have two options: The output of the PDF in the browser, so that the user can view them directly in the browser / download. Or, as a second variant, saving the PDF on the server.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here