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.

[cc lang=”java”] $invoice_date = date ( “d.m.Y” ) ;
$delivery_date = date ( “d.m.Y” ) ;
$pdfAuthor = “you company” ;

$invoice_header = ‘

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::

name: name
IBAN: 132 165 4651 6464
BIC: 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 = ‘

‘ . nl2br ( trim ( $invoice_header ) ) . ‘ Bill number ‘ . $bill_number . ‘
Invoice date: ‘ . $invoice_date . ‘
Delivery date: ‘ . $delivery_date . ‘
invoice
‘ . nl2br ( trim ( $recipient ) ) . ‘

 

‘ ;$total_price = 0 ;foreach ( $invoice as $entry ) {
$amount = $entry [ 1 ] ;
$single_price = $entry [ 2 ] ;
$price = $amount * $single_price ;
$total_price += $price ;
$html . = ” ;
}
$html . = ”

description amount Price price
‘ . $entry [ 0 ] . ‘ ‘ . $entry [ 1 ] . ‘ ‘ . number_format ( $entry [ 2 ] , 2 , ‘,’ , ” ) . ‘ Euro ‘ . number_format ( $price , 2 , ‘,’ , ” ) . ‘ Euro

” ;

$html . = ‘


‘ ;if ( $value_added_tax > 0 ) {$erm = $total_price / ( 1 + $value_added_tax ) ;$value_added_tax_amount = $total_price – $erm ;$html . = ” ;
}$html . = ‘

Subtotal ‘ . number_format ( $erm , 2 , ‘,’ , ” ) . ‘ Euro
value_added_tax (‘ . intval ( $value_added_tax * 100 ) . ‘%) ‘ . number_format ( $value_added_tax_amount , 2 , ‘,’ , ” ) . ‘ Euro
Total: ‘ . number_format ( $total_price , 2 , ‘,’ , ” ) . ‘ Euro

‘ ;

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 -> SetCreator ( PDF_CREATOR ) ;
$pdf -> SetAuthor ( $pdfAuthor ) ;
$pdf -> SetTitle ( ‘Invoice ‘ . $bill_number ) ;
$pdf -> SetSubject ( ‘Invoice ‘ . $bill_number ) ;

$pdf -> setHeaderFont ( Array ( PDF_FONT_NAME_MAIN , ” , PDF_FONT_SIZE_MAIN ) ) ;
$pdf -> setFooterFont ( Array ( PDF_FONT_NAME_DATA , ” , PDF_FONT_SIZE_DATA ) ) ;

$pdf -> SetDefaultMonospacedFont ( PDF_FONT_MONOSPACED ) ;

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

$pdf -> SetAutoPageBreak ( TRUE , PDF_MARGIN_BOTTOM ) ;

$pdf -> setImageScale ( PDF_IMAGE_SCALE_RATIO ) ;

$pdf -> SetFont ( ‘dejavusans’ , ” , 10 ) ;

$pdf -> AddPage ( ) ;

$pdf -> writeHTML ( $html , true , false , true , false , ” ) ;

$pdf -> Output ( $pdfName , ‘I’ ) ;

//Option 2: Save PDF in the directory:
//$pdf->Output(dirname(__FILE__).’/’.$pdfName, ‘F’);
//echo ‘Download PDF: ‘.$pdfName.’‘;

?> [/cc]

 

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.

 

Recent Articles

spot_img

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here