[Drupal] How to generate a PDF file by passing custom data using TCPDF library

| | 3 min read

Creating an order invoice detail as pdf can be a most frequent requirement when dealing with web applications. In this article I like to explain how to create a more advanced pdf document in Drupal. We will be creating a order detail report using tcpdf. TCPDF in based on FPDF which contains more advanced features compared to FPDF.

For generating pdf document tcpdf library should be added, as explained in the previous post.

We will be working with tcpdf inside module files. Include files 'tcpdf_include.php, tcpdf.php' to the module which contains implementation functions. Lets have a look at simple example rendering pdf using tcpdf.


setLanguageArray($l);
  }
?>

The object '$pdf' is created as an instance of custom class created by inheriting TCPDF class. Custom classes can be created by extending TCPDF class as per your requirement. Since Drupal 7 is not under OOP's concept you could define the custom class inside a method of the module. Language files for the pdf are included inside the 'lang' directory. Call the setLanguageArray method to get language features.

Call SetFont method to add font properties in the below example the method accepts three parameters font name, font weight and size. AddPage() is the method used to add new page.


  // set font
  $pdf->SetFont('sans-serif', 'B', 21);

  // add a page
  $pdf->AddPage();

Following snippet contains adding logo and other contents to our pdf document. We add contents using corrdinates in a cell. In the below code we are setting the contents to be rendered into our pdf on creation. The invoice table headers and details can be stored in the array variables. The writeHTMLCell method specifies an x and y coordinate to be passed in a long with the text content. On using Cell element you need to define first were the cursor to be moved to.


  $html = 'Logo';

  $header = array('DESCRIPTION', 'Qty', 'Unit Value' , 'PRICE');
  $product_details = array('Your description here', '5.0', '56' , '88.99');

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

  $pdf->SetFont('helvetica', '', 8);

  $html = "

INVOICE REPORT

"; $pdf->writeHTMLCell(0, 0, '', '', $html, '', 1, 0, true, 'L', true);

Text() method to add text with coordinates, using the coordinates will move the text to the desired position in the pdf. Here call to ColoredTable() creates the header and product details cell from the array passed. Cell() function is used to print texts in a table. Method accepts text content with left and right margins.


  $pdf->Text(10, 40, 'INVOICE TO :');
  $pdf->Text(30, 40, 'Mr. Doe');
  $pdf->Text(10, 45, 'ADDRESS :');
  $pdf->Text(30, 45, 'Xyx co. ltd.');
  $pdf->Text(30, 50, 'Place');
  $pdf->Text(30, 55, 'Locality');
  $pdf->Text(10, 70, 'POSTCODE :');
  $pdf->Text(30, 70, 'XXX-XX');
  $pdf->Text(10, 75, 'TEL NO. :');
  $pdf->Text(10, 80, '999-99-8888');
  $pdf->Text(110, 40, 'DELIVER TO :');
  $pdf->Text(130, 40, 'Doe');
  $pdf->Text(110, 45, 'ADDRESS :');
  $pdf->Text(130, 45, 'Xyz Co. ltd.');
  $pdf->Text(130, 50, 'Place');
  $pdf->Text(130, 55, 'City');
  $pdf->Text(130, 60, 'xxx-xxx');
  $pdf->Text(110, 70, 'POSTCODE :');
  $pdf->Text(130, 70, 'XXX-XX');
  $pdf->Text(110, 75, 'TEL NO. :   ');
  $pdf->Text(110, 80, '');
  $pdf->Text(10, 85, 'DATE OF INVOICE :');
  $pdf->Text(50, 85, '2015-01-08 08:31:09');
  $pdf->Text(10, 91, 'INV. NUM. :');
  $pdf->Text(50, 91, '202403');
  $pdf->Text(10, 95, 'ORDER NO. :');
  $pdf->Text(50, 95, '202403');
  $pdf->Text(10, 100, '');
  $pdf->Cell(45, 0, '', 0, 1, 'C', 0, '', 1);
  $pdf->ColoredTable($header, $product_details);

  $pdf->Cell(45, 0, '', 0, 1, 'C', 0, '', 1);

We could set fill color as RGB color model to this method using the SetFillColor method, we can pass rgb color codes as parameters. We have again used the writeHTMLCell to position the text content.


  $pdf->SetFillColor(255,255,0);
  $pdf->writeHTMLCell(141, 0, '', '', '', '', 0, 0, true, 'L', true);
  $html = 'Subtotal: 999

Tax title: Test' . '

Shipping: 5.0


GRAND TOTAL: 1000'; $pdf->SetFillColor(255,255,0); $pdf->writeHTMLCell(40, 0, '', '', $html, '', 0, 0, true, 'R', true); //Close and output PDF document $invoice_report_name = 'invoice.pdf'; $pdf->Output('invoice.pdf', 'F');

Please see the codes combined below. This is a working code which was tested to be working. Your can take this as a reference for creating pdf documents using tcpdf. Hope the functions used in the above snippet are familiar to you if not please refer part-1 of this article.


setLanguageArray($l);
  }

  // set font
  $pdf->SetFont('sans-serif', 'B', 20);

  // add a page
  $pdf->AddPage();

  $html = 'Logo';
  
  $header = array('DESCRIPTION', 'Qty', 'Unit Value' , 'PRICE');
  $product_details = array('Your description here', '5.0', '56' , '88.99');
  
  $pdf->writeHTML($html, true, false, true, false, '');

  $pdf->SetFont('helvetica', '', 8);

  $html = '';

  $pdf->writeHTMLCell(0, 0, '', '', $html, '', 1, 0, true, 'L', true);

  $pdf->Text(10, 40, 'INVOICE TO :');
  $pdf->Text(30, 40, 'Mr. Doe');
  $pdf->Text(10, 45, 'ADDRESS :');
  $pdf->Text(30, 45, 'Xyx co. ltd.');
  $pdf->Text(30, 50, 'Place');
  $pdf->Text(30, 55, 'Locality');
  $pdf->Text(10, 70, 'POSTCODE :');
  $pdf->Text(30, 70, 'XXX-XX');
  $pdf->Text(10, 75, 'TEL NO. :');
  $pdf->Text(10, 80, '999-99-8888');
  $pdf->Text(110, 40, 'DELIVER TO :');
  $pdf->Text(130, 40, 'Doe');
  $pdf->Text(110, 45, 'ADDRESS :');
  $pdf->Text(130, 45, 'Xyz Co. ltd.');
  $pdf->Text(130, 50, 'Place');
  $pdf->Text(130, 55, 'City');
  $pdf->Text(130, 60, 'xxx-xxx');
  $pdf->Text(110, 70, 'POSTCODE :');
  $pdf->Text(130, 70, 'XXX-XX');
  $pdf->Text(110, 75, 'TEL NO. :   ');
  $pdf->Text(110, 80, '');
  $pdf->Text(10, 85, 'DATE OF INVOICE :');
  $pdf->Text(50, 85, '2015-01-08 08:31:09');
  $pdf->Text(10, 90, 'INVOICE NO. :');
  $pdf->Text(50, 90, '202403');
  $pdf->Text(10, 95, 'ORDER NO. :');
  $pdf->Text(50, 95, '202403');
  $pdf->Text(10, 100, '');
  $pdf->Cell(45, 0, '', 0, 1, 'C', 0, '', 1);
  $pdf->ColoredTable($header, $product_details);

  $pdf->Cell(45, 0, '', 0, 1, 'C', 0, '', 1);
  $pdf->SetFillColor(255,255,0);
  $pdf->writeHTMLCell(140, 0, '', '', '', '', 0, 0, true, 'L', true);
  $html = 'Subtotal: 999

Tax title: Test' . '

Shipping: 5.0


GRAND TOTAL: 1000'; $pdf->SetFillColor(255,255,0); $pdf->writeHTMLCell(40, 0, '', '', $html, '', 0, 0, true, 'R', true); //Close and output PDF document $invoice_report_name = 'invoice-report.pdf'; $pdf->Output('invoice-report.pdf', 'F'); ?>

In the above code the object '$pdf' is created as an instance of custom class created by extending TCPDF class. Any custom class can be created by extending TCPDF class as per your requirement. Since drupal 7 is not under OOP's concept you could define the custom class inside a method of the module.

There are several liberaries similar to tcpdf by which could render pdf. Hope you have found this note useful. Thank you.

Please feel free to share your thoughts and suggestions regarding this here.