创建新页面
创建新页面时要考虑三件事:创建页面、将页面添加到文档中以及确保一切显示正常。
清单 19. 创建新页面
复制内容到剪贴板
代码:
...
foreach($entries as $row){
$title = $row['feedname'];
$entrydata = $row['entrydata'];
if($row['channelname'] != '')
{
$title = "$title > " . $row['channelname'];
}
if ($startPos < 72){
array_push($pdf->pages, $page);
$page = new Zend_Pdf_Page(
Zend_Pdf_Const::PAGESIZE_LETTER);
$startPos = $pageHeight - 48;
}
$headlineStyle = new Zend_Pdf_Style();
$headlineStyle->setFillColor(
new Zend_Pdf_Color_RGB(0.9, 0, 0));
$headlineStyle->setFont(
new Zend_Pdf_Font_Standard(
Zend_Pdf_Const::FONT_HELVETICA_BOLD), 18);
$page->setStyle($headlineStyle);
$title = strip_tags($title );
$title = wordwrap($title , 55, '\n');
$headlineArray = explode('\n', $title );
foreach ($headlineArray as $line) {
$line = ltrim($line);
$page->drawText($line, 48, $startPos);
$startPos = $startPos - 24;
}
$articleStyle = new Zend_Pdf_Style();
$articleStyle->setFillColor(
new Zend_Pdf_Color_RGB(0, 0, 0));
$articleStyle->setFont(
new Zend_Pdf_Font_Standard(
Zend_Pdf_Const::FONT_HELVETICA_BOLD), 12);
$page->setStyle($articleStyle);
$entrydata = strip_tags($entrydata);
$entrydata = wordwrap($entrydata, 90, '\n');
$articleArray = explode('\n', $entrydata);
foreach ($articleArray as $line) {
if ($startPos < 48){
array_push($pdf->pages, $page);
$page = new Zend_Pdf_Page(
Zend_Pdf_Const::PAGESIZE_LETTER);
$articleStyle = new Zend_Pdf_Style();
$articleStyle->setFillColor(
new Zend_Pdf_Color_RGB(0, 0, 0));
$articleStyle->setFont(
new Zend_Pdf_Font_Standard(
Zend_Pdf_Const::FONT_HELVETICA_BOLD), 12);
$page->setStyle($articleStyle);
$startPos = $pageHeight - 48;
}
$page->drawText($line, 48, $startPos);
$startPos = $startPos - 16;
}
$startPos = $startPos - 16;
}
array_push($pdf->pages, $page);
header('Content-type: application/pdf');
echo $pdf->render();
}
...由于 PDF 文档的页面只是一个数组,所以我们能够使用 array_push() 函数将当前页面添加到文档末端。然后就可以创建一个新页面(为方便起见,使用相同的变量名),并将起始位置重置于页面顶部向下 2/3 英寸的位置。
现在,检查摘要的条件已经充分,因为我们能够在页面创建后轻易地设置其样式。而对正文的检查,会稍复杂一些,因为我们也许会遇到某些情况而停止操作,比如说在数组中部创建了页面而新页面的 $articleStyle 样式并未设置好。因此,除了将当前页面添加到数组并创建一个新页面以外,还需要重置样式。我们再一次将起始位置设为页面顶部向下 2/3 英寸的位置。
最后,我们改变了将初始页面添加到文档的方式。在此这前,我们完成了所有的处理,然后将当前页面添加为首页。在这种情况下,我们并不知道这是否是第一页,因此我们只是把它放入数组。
结果产生了一个适当地增加了页码的文档
尽管页面被适当地添加了页码,但它仍缺少一个边框。