我有一个HTML文档,我正在使用TCPDF将其转换为PDF。HTML 文档包含多个 html 表。这工作正常。但是,我还需要将 PDF 文档中的页数添加到第一页。是否有任何解决方案可以构建PDF文件,然后在当前第一页之前添加页面?
我不知道
它是否适用于 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/
您应该能够根据表格的大小计算出有多少页。 我会阅读所有的html数据,测量它,然后开始制作PDF。
我发现的想法是克隆 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);