Generate Dynamic Pdf File Using Tcpdf In PHP

In this tutorial, you’ll learn how to generate Dynamic PDF files and save them into the server using PHP code

You need to download tcpdf and add the library in your project folder and then include it in libraries Folder:

public function ganaratepdf(){

$this->load->library(‘tcpdf’);

$obj_pdf = new TCPDF(‘P’, PDF_UNIT, PDF_PAGE_FORMAT, true, ‘UTF-8’, false);
$obj_pdf->SetCreator(PDF_CREATOR);
$obj_pdf->SetTitle(“Booking PDF”);
$obj_pdf->SetHeaderData(”, ”, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, ”, PDF_FONT_SIZE_MAIN));
$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, ”, PDF_FONT_SIZE_DATA));
$obj_pdf->SetDefaultMonospacedFont(‘helvetica’);
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(PDF_MARGIN_LEFT, ‘4’, PDF_MARGIN_RIGHT);
$obj_pdf->setPrintHeader(false);
$obj_pdf->setPrintFooter(false);
$obj_pdf->SetAutoPageBreak(TRUE, 10);
$obj_pdf->SetFont(‘helvetica’, ”, 12);
$obj_pdf->AddPage();

$date=date(‘Y-m-d’);

$content = ‘

<table>
    <tbody>
        <tr>
            <td><img src="https://qrpe.in/businessservices/assets/invoice/paytm.png" width="150"></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>Payment Receipt Receipt Number: 12495354273 &nbsp; Service Provider National Payment corporation of India (BBPS Dept.) National Payment corporation of India (BBPS Dept.) 1001A, The Capital B Wing, 10th Floor, Bandra Kurla Complex, Bandra (E), Mumbai &nbsp; BBPS Biller Id: UGVCL0000GUJ01 BBPS Transaction Id: PT01GYWT4625 Payment Channel: androidapp 8.14.55 &nbsp;</td>
            <td><img src="https://qrpe.in/businessservices/assets/invoice/paytm.png" alt="" width="150"> &nbsp; Receipt Date : 01-12-2020 Payer Rahul Sah 7405379159 [email protected]</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>Description Bill Amount Convenience Fee (Inclusive of 18% GST) Total Amount Bill Payment for Uttar Gujarat Vij Company Limited 75203207160 3770 0 3770</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>Total Amount in Words: Three Thousand Seven Hundred Seventy Rupees Only</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>Please Note:</td>
        </tr>
        <tr>
            <td>Dear Consumer, the bill payment will reflect in next 48 hours or in the next billing cycle, at your service provider’s end. Please contact paytm customer support for any queries regarding this order.</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>DECLARATION:</td>
        </tr>
        <tr>
            <td>This is not an invoice but only a confirmation of the receipt of the amount paid against for the service as described above. Subject to terms and conditions mentioned at paytm.com</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>(This is computer generated receipt and does not require physical signature.)<br>B-121 Sector 5, Noida, Uttar Pradesh 201301,<br>Service tax registration number: AAACO4007ASD002<br>Paytm Order ID :12252016430</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
    </tbody>
</table>

‘;

$content .= ”;

$file_path = $_SERVER[‘DOCUMENT_ROOT’].’/assets/invoice/amd.pdf’; // idar change krlo

$obj_pdf->writeHTML($content);
$obj_pdf->Output($file_path, ‘F’); //for save
// $obj_pdf->Output($file_path, ‘I’); //for view

// $obj_pdf->SetDisplayMode(‘real’, ‘default’);

echo $file_path;
}

Note:-

TCPDF has very limited CSS support. It doesn’t support all attributes.

Currently, only the following CSS attributes are supported:

  • font-family
  • font-size
  • font-weight
  • font-style
  • color
  • background-color
  • text-decoration
  • width
  • height
  • text-align

Leave a Reply

Your email address will not be published.