Example #1
0
 /**
  * Output a Table Of Content Index (TOC) using HTML templates.
  * This method must be called after all Bookmarks were set.
  * Before calling this method you have to open the page using the addTOCPage() method.
  * After calling this method you have to call endTOCPage() to close the TOC page.
  * @param $page (int) page number where this TOC should be inserted (leave empty for current page).
  * @param $toc_name (string) name to use for TOC bookmark.
  * @param $templates (array) array of html templates. Use: "#TOC_DESCRIPTION#" for bookmark title, "#TOC_PAGE_NUMBER#" for page number.
  * @param $correct_align (boolean) if true correct the number alignment (numbers must be in monospaced font like courier and right aligned on LTR, or left aligned on RTL)
  * @param $style (string) Font style for title: B = Bold, I = Italic, BI = Bold + Italic.
  * @param $color (array) RGB color array for title (values from 0 to 255).
  * @public
  * @author Nicola Asuni
  * @since 5.0.001 (2010-05-06)
  * @see addTOCPage(), endTOCPage(), addTOC()
  */
 public function addHTMLTOC($page = '', $toc_name = 'TOC', $templates = array(), $correct_align = true, $style = '', $color = array(0, 0, 0))
 {
     $filler = ' ';
     $prev_htmlLinkColorArray = $this->htmlLinkColorArray;
     $prev_htmlLinkFontStyle = $this->htmlLinkFontStyle;
     // set new style for link
     $this->htmlLinkColorArray = array();
     $this->htmlLinkFontStyle = '';
     $page_first = $this->getPage();
     $page_fill_start = false;
     $page_fill_end = false;
     // get the font type used for numbers in each template
     $current_font = $this->FontFamily;
     foreach ($templates as $level => $html) {
         $dom = $this->getHtmlDomArray($html);
         foreach ($dom as $key => $value) {
             if ($value['value'] == '#TOC_PAGE_NUMBER#') {
                 $this->SetFont($dom[$key - 1]['fontname']);
                 $templates['F' . $level] = $this->isUnicodeFont();
             }
         }
     }
     $this->SetFont($current_font);
     $maxpage = 0;
     //used for pages on attached documents
     foreach ($this->outlines as $key => $outline) {
         // get HTML template
         $row = $templates[$outline['l']];
         if (TCPDF_STATIC::empty_string($page)) {
             $pagenum = $outline['p'];
         } else {
             // placemark to be replaced with the correct number
             $pagenum = '{#' . $outline['p'] . '}';
             if ($templates['F' . $outline['l']]) {
                 $pagenum = '{' . $pagenum . '}';
             }
             $maxpage = max($maxpage, $outline['p']);
         }
         // replace templates with current values
         $row = str_replace('#TOC_DESCRIPTION#', $outline['t'], $row);
         $row = str_replace('#TOC_PAGE_NUMBER#', $pagenum, $row);
         // add link to page
         $row = '<a href="#' . $outline['p'] . ',' . $outline['y'] . '">' . $row . '</a>';
         // write bookmark entry
         $this->writeHTML($row, false, false, true, false, '');
     }
     // restore link styles
     $this->htmlLinkColorArray = $prev_htmlLinkColorArray;
     $this->htmlLinkFontStyle = $prev_htmlLinkFontStyle;
     // move TOC page and replace numbers
     $page_last = $this->getPage();
     $numpages = $page_last - $page_first + 1;
     // account for booklet mode
     if ($this->booklet) {
         // check if a blank page is required before TOC
         $page_fill_start = ($page_first % 2 == 0 xor $page % 2 == 0);
         $page_fill_end = !($numpages % 2 == 0 xor $page_fill_start);
         if ($page_fill_start) {
             // add a page at the end (to be moved before TOC)
             $this->addPage();
             ++$page_last;
             ++$numpages;
         }
         if ($page_fill_end) {
             // add a page at the end
             $this->addPage();
             ++$page_last;
             ++$numpages;
         }
     }
     $maxpage = max($maxpage, $page_last);
     if (!TCPDF_STATIC::empty_string($page)) {
         for ($p = $page_first; $p <= $page_last; ++$p) {
             // get page data
             $temppage = $this->getPageBuffer($p);
             for ($n = 1; $n <= $maxpage; ++$n) {
                 // update page numbers
                 $a = '{#' . $n . '}';
                 // get page number aliases
                 $pnalias = $this->getInternalPageNumberAliases($a);
                 // calculate replacement number
                 if ($n >= $page) {
                     $np = $n + $numpages;
                 } else {
                     $np = $n;
                 }
                 $na = TCPDF_STATIC::formatTOCPageNumber($this->starting_page_number + $np - 1);
                 $nu = TCPDF_FONTS::UTF8ToUTF16BE($na, false, $this->isunicode, $this->CurrentFont);
                 // replace aliases with numbers
                 foreach ($pnalias['u'] as $u) {
                     if ($correct_align) {
                         $sfill = str_repeat($filler, strlen($u) - strlen($nu . ' '));
                         if ($this->rtl) {
                             $nr = $nu . TCPDF_FONTS::UTF8ToUTF16BE(' ' . $sfill, false, $this->isunicode, $this->CurrentFont);
                         } else {
                             $nr = TCPDF_FONTS::UTF8ToUTF16BE($sfill . ' ', false, $this->isunicode, $this->CurrentFont) . $nu;
                         }
                     } else {
                         $nr = $nu;
                     }
                     $temppage = str_replace($u, $nr, $temppage);
                 }
                 foreach ($pnalias['a'] as $a) {
                     if ($correct_align) {
                         $sfill = str_repeat($filler, strlen($a) - strlen($na . ' '));
                         if ($this->rtl) {
                             $nr = $na . ' ' . $sfill;
                         } else {
                             $nr = $sfill . ' ' . $na;
                         }
                     } else {
                         $nr = $na;
                     }
                     $temppage = str_replace($a, $nr, $temppage);
                 }
             }
             // save changes
             $this->setPageBuffer($p, $temppage);
         }
         // move pages
         $this->Bookmark($toc_name, 0, 0, $page_first, $style, $color);
         if ($page_fill_start) {
             $this->movePage($page_last, $page_first);
         }
         for ($i = 0; $i < $numpages; ++$i) {
             $this->movePage($page_last, $page);
         }
     }
 }