我正在使用tcpdf创建相册 - 效果很好。我遇到的挑战:页面的最终大小只有在我创建了所有页面后才会知道。我需要做的是创建所有页面后:向左或向右放大页面(取决于它是书的左页还是右页)。或者,我可以创建一个大于需要的页面,并在左侧或右侧切断它们。我该怎么做 - 我想保留背景颜色,我在原始页面上“使用$ pdf-> Rect”(0,0,$ pdf-> getPageWidth(),$ pdf-&gt ; getPageHeight(),'F',$ coverBackground)。
答案 0 :(得分:0)
我不熟悉一种允许您在创建页面后调整页面大小的方法。根据我对您的应用程序的理解,页边距和大小仅取决于其页面编号或文档中的位置。因此,可以通过简单地知道将在文档中的总页数来计算页面大小和页边距。这允许在创建每个页面之前设置它们。
以下示例包含addCustomPage()
函数,可以代替AddPage()
调用该函数。它根据当前页码和文档中的总页数计算每页的页边距和大小。计算保证金的代码需要修改或替换以满足您的要求。它目前假设书外的页面需要更宽的页面大小和内部边距。该示例应在TCPDF示例目录中运行。
<?php
/**
* Add page and margins based on current page number of a bound book.
*
* Additional margins alternate left and right for two-sided printing. Inside
* margins and page sizes are larger towards the outside of the book.
*
* @param $pdf (object) an instance of TCPDF class for document
* @param $totalPages (integer) total number of pages in document
*/
function addCustomPage($pdf, $totalPages) {
$basePageSize = array(210, 294); // Size of page in mm, including margins.
$pageIndex = $pdf->getPage();
// Example for calculating margins based on current page number. This is a
// very simple example that will need to be adjusted for your layout.
$addToMargin = 0.3;
if ($pageIndex < $totalPages / 2) {
$addToMargin*= ($totalPages / 2 - $pageIndex);
}
else {
$addToMargin*= $pageIndex - $totalPages / 2;
}
// Add additional margin to left for even pages starting at 0. Right for odd.
if ($pageIndex % 2 == 0) {
$pdf->SetLeftMargin(10 + $addToMargin);
$pdf->SetRightMargin(10);
}
else {
$pdf->SetLeftMargin(10);
$pdf->SetRightMargin(10 + $addToMargin);
}
// Create page with margin added to width
$pdf->AddPage('P', array($basePageSize[0] + $addToMargin, $basePageSize[1]) , true);
}
require_once ('tcpdf_include.php');
$pdf = new TCPDF();
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(10, 10, 10);
// Generate 50 example pages with background
for ($p = 0; $p < 50; $p++) {
addCustomPage($pdf, 50);
$pdf->Rect($pdf->getX() , $pdf->getY() , 190, 274, 'DF', '', array(241, 248, 255));
}
$pdf->Output('example.pdf', 'I');