Search code examples
pdfdrupal-7tcpdfpage-numbering

TCPdf add custom author or page to footer in multiple pages ( author/page can be different on each page )


I have found a lot of information about page numbers in the footer for TCpdf, but I haven't found the solution to my specific problem.

My problem: A user can add a content type with : image, page number, title and orientation. The page number is needed, because the content is created when an image is received. So this is not in chronological order. An image can be added for page 20, while later on, an image that should be on page 2 is send. So as solution I added the possibility to add page number as field in the content type.

when the content type is created, the full pdf is again generated. But how can I set the page to the inserted page?

The solution I had was to disable the footer, and add my own line when adding the page. But when trying to create the table of contents, it takes the last page number and keeps repeating the last number. pagetitle1.....................40 pagetitle2.....................40 ...

my code for when each page is created ( where I thought I could add a setPage($number)), but this gave the error: Wrong page number on setPage() function

  foreach ($nodes as $node) {
 $title = $node->title;
 $image = $node->field_tune['und'][0];
 $image_orientation = $node->field_orientatie['und'][0]['value'];
 $author = $node->field_composer['und'][0]['value'];
 $page = $node->field_page['und'][0]['value'];
   $orientation = ($image_orientation === 'Landscape' ) ? 'L' : 'P';

 // add a page
 $pdf->AddPage($orientation, 'A4', FALSE, TRUE);

 //Here I added the code to set page number
 //$pdf->setPage($page); --> this gave the error. 
  //But after further checking, this wasn't something to set 
  //pagenumber, but to go back to the defined page. 

 $pdf->Bookmark($title, 0, 0, '', 'B', array(0,64,128));


 $html = theme('html_content_type_theme', [
   'image' => theme('image_style', [
     'style_name' => 'styled_image',
     'path' => $image_uri,
   ]),
   'orientation' => $orientation,
   'title' => $title,
   'author' => $author,
 ]);

 // output the HTML content
 $pdf->writeHTML($html, TRUE, TRUE, TRUE, FALSE, '');
 $pdf->SetY(-15);
 $pdf->SetFont('helvetica', '', 10);
 //Here I create my custom footer to see the correct page
 $pdf->Cell(0, 10, 'Ypres surrey Pipes & drums', 0, FALSE, 'C', 0, '', 0, FALSE, 'T', 'C');
 $pdf->Cell(0, 10, $page, 0, FALSE, 'T', 0, '', 0, FALSE, 'T', 'C');

}

Another question: every content can be created by a custom author. How can I add the author name to the footer? ( Solution now: Is to add the author also as custom line in the html, not in the footer )


Solution

  • I found the solution to pass a custom number to my footer. This also will be the solution for my header problem where I want to add the author.

    class MYPDF extends TCPDF {
    
         public $pageNumber = '1';
    
         /**
          * @return string
          */
         public function getPageNumber(): string {
           return $this->pageNumber;
         }
    
         /**
          * @param string $pageNumber
          *
          * @return MYPDF
          */
         public function setPageNumber(string $pageNumber): MYPDF {
           $this->pageNumber = $pageNumber;
           return $this;
         }
    
        public function Footer() {
            $this->SetY(-15);
            $this->SetFont('helvetica', 'I', 8);
            $this->Cell(0, 10, $this->pageNumber, 0, false, 'R', 0, '', 0, false, 'T', 'M');
         }
      }
    

    In my foreach( $nodes ... ) I added the $pdf->setPageNumber($page) This needs to be after the addPage. Because else, the footer will receive the number of the previous node.

    $pdf->AddPage($orientation, 'A4', FALSE, TRUE);
    $pdf->setPageNumber($page);
    

    this can dan be done for all the custom changes you want to have on header or footer