How to create a table of contents using dompdf?

I am using the dompdf php library to create a PDF report from an HTML template. This html template has a table of contents section. When generating the PDF, I need to update the page number of the table of contents. Does anyone know how I can achieve this in the dompdf php library?

Thanks in advance.

+4
source share
4 answers

I have achieved this in Drupal and assume that it works with other php opensource and frameworks. I saved this code inside the script tag

$GLOBALS['entity_page'][] = $pdf->get_page_number(); 

in the template in which the page number is stored. Template with extension tpl.php Now in the module after other codes for export I added ......

 $canvas = $dompdf->get_canvas(); $font = Font_Metrics::get_font("helvetica", "normal"); $canvas->page_text(520, 805, "Page {PAGE_NUM}", $font, 9, array(0.4, 0.4, 0.4)); foreach ($GLOBALS['entity_page'] as $key => $val) { $GLOBALS["entity_val"] = 0; $GLOBALS["entity_y"] = 110; $canvas->page_script('if($PAGE_NUM == 3 && $PAGE_NUM < 4){ $font = Font_Metrics::get_font("helvetica", "normal"); $x = 380; $y = $GLOBALS["entity_y"]; $pdf->text($x, $y, "-------------------------".$GLOBALS["entity_page"][$GLOBALS["entity_val"]]."", $font, 12, array(0, 0, 0, 0.8)); $GLOBALS["entity_y"] = $GLOBALS["entity_y"] + 33; $GLOBALS["entity_val"] = $GLOBALS["entity_val"] + 1; }'); } 

$ pdf-> text This part adds page numbers in constant increment at the y axis position. Other global variables entity_y and entity_val are used to store values.

+2
source

Creating content from HTML (with h1, h2, h3), I did the following:

  • Give each title a unique identifier first (because we use PrinceXML) and is a good estimate.
  • Then create an OL / LI structure, although this piece of code may contain errors.

See: http://pastie.org/655728

+1
source

Perhaps you have already decided this? I did not use dompdf, but I did a similar thing in Zend_Pdf: I made a blank page for the table of contents, and then continued to create all the other later pages, preserving the array page_number => title. In the end, I came back and updated the content page using the previously saved link ...

0
source

As a complement to vicky shrestha, the answer here is what I have for a table of contents that extends over more than one page.

36 is just an arbitrary number of elements that fit into the design.

 foreach ($GLOBALS['entity_page'] as $key => $val) { $GLOBALS["entity_y"] = 88; $GLOBALS["entity_val"] = 0; $GLOBALS["entity_per_page"] = 36; if($val) { $canvas->page_script(' if(isset($GLOBALS["entity_page"][$GLOBALS["entity_val"]])) { if($PAGE_NUM == $GLOBALS["entity_page_number"]){ $x = 505; $y = $GLOBALS["entity_y"]; $font = $fontMetrics->get_font("Open Sans", "Helvetica Neue", "Helvetica, Arial, sans-serif"); $pdf->text($x, $y, $GLOBALS["entity_page"][$GLOBALS["entity_val"]]."", $font, 7, array(0, 0, 0, 1)); $GLOBALS["entity_y"] = $GLOBALS["entity_y"] + 19; $GLOBALS["entity_val"] = $GLOBALS["entity_val"] + 1; if (($GLOBALS["entity_val"] + 1) % $GLOBALS["entity_per_page"] == 0 ) { $GLOBALS["entity_page_number"] = $GLOBALS["entity_page_number"] + 1; $GLOBALS["entity_y"] = 31; } } }'); } } 

isset is important because for some reason the foreach will loop extra time, and in the end you will get an exception outside the bounds.

0
source

All Articles