首页 > 解决方案 > TCPdf 在多个页面中将自定义作者或页面添加到页脚(每个页面上的作者/页面可以不同)

问题描述

我在 TCpdf 的页脚中找到了很多关于页码的信息,但我还没有找到针对我的具体问题的解决方案。

我的问题:用户可以添加内容类型:图像、页码、标题和方向。需要页码,因为内容是在接收到图像时创建的。所以这不是按时间顺序排列的。可以为第 20 页添加图像,然后发送应该在第 2 页的图像。因此,作为解决方案,我添加了在内容类型中添加页码作为字段的可能性。

创建内容类型后,将再次生成完整的 pdf。但是如何将页面设置为插入的页面?

我的解决方案是禁用页脚,并在添加页面时添加我自己的行。但是在尝试创建目录时,它会使用最后一个页码并不断重复最后一个数字。页面标题 1.............40 页面标题 2............40

我创建每个页面时的代码(我认为我可以添加一个 setPage($number)),但这给出了错误:setPage() 函数上的错误页码

  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');

}

另一个问题:每个内容都可以由自定义作者创建。如何将作者姓名添加到页脚?(现在的解决方案:是将作者也添加为html中的自定义行,而不是在页脚中)

标签: pdfdrupal-7tcpdfpage-numbering

解决方案


我找到了将自定义数字传递给我的页脚的解决方案。这也将是我想添加作者的标题问题的解决方案。

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');
     }
  }

在我的 foreach( $nodes ... ) 中,我添加了 $pdf->setPageNumber($page) 这需要在 addPage 之后。因为否则,页脚将收到前一个节点的编号。

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

这可以针对您希望对页眉或页脚进行的所有自定义更改完成


推荐阅读