【问题标题】:Add a page before first page in TCPDF在 TCPDF 的第一页之前添加一页
【发布时间】:2013-10-30 04:52:22
【问题描述】:

我有一个 HTML 文档,我正在使用 TCPDF 将其转换为 PDF。 HTML 文档包含几个 html 表格。这工作正常。但是,我还需要将 PDF 文档中的页数添加到第一页。是否有任何解决方案来构建 PDF 文件,然后在当前第一页之前添加一页?

【问题讨论】:

    标签: php html pdf pdf-generation tcpdf


    【解决方案1】:

    我不知道它是否适用于 HTML,但我找到了一个适用于 movePage 的解决方案。
    您创建页面,获取其位置并将其移动到第一个位置:

    $pdf->AddPage();
    $pdf->Cell(0, 10, 'My content');
    // Get the current page number
    $pageNo = $pdf->PageNo(); 
    // First page of the document
    $toPage = 1;
    $pdf->movePage($pageNo, $toPage);
    

    此处为官方示例:https://tcpdf.org/examples/example_044/

    【讨论】:

      【解决方案2】:

      我找到的思路是克隆TCPDF实例,获取返回的页数:

      $tmp_pdf = clone $pdf;
      $tmp_pdf->AddPage();
      $tmp_pdf->writeHTML($report, true, false, true, false, '');
      $num_pages = $tmp_pdf->getNumPages();
      unset($tmp_pdf);
      $report = preg_replace('#\{num_pages\}#is', $num_pages, $report);
      

      【讨论】:

        【解决方案3】:

        您应该能够根据表格的大小计算出有多少页。我会读入所有的 html 数据,测量它,然后开始制作 PDF。

        【讨论】:

        • 感谢您的回复,但它不起作用,因为我里面有一些分页符标签......
        猜你喜欢
        • 2015-04-21
        • 2011-11-04
        • 2020-07-10
        • 2011-08-16
        • 2023-03-19
        • 1970-01-01
        • 2017-01-21
        • 2019-03-10
        • 2021-06-06
        相关资源
        最近更新 更多