众所周之,Zend Framework集成了许多优秀且实用的功能,今天看看它的操作PDF的强大能力。
PDF是一种公认的文档格式,如果能将它应用到公文、OA等系统中去的话,数字签名也就不需要那么麻烦了
OK,闲话少叙,让我们看看在Zend Framework中它的神奇表现吧:
1、建立PDF文件(空的,只是形式没有页面)
require_once 'Zend/Pdf.php';
$pdf = new Zend_Pdf();
$pdf->save("chomp.pdf");
?>
是不是很简单?
2、建立空的PDF文件(已经有页面,只是页面没有元素)
require_once 'Zend/Pdf.php';
$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER);
$pageHeight = $page->getHeight();
$pageWidth = $page->getWidth();
echo 'Height = '.$pageHeight.'\n';
echo 'Width = '.$pageWidth.'\n';
$pdf->pages[0] = ($page);
$pdf->save("chomp.pdf");
?>
3、插入图片至页面中(Image objects should be created with Zend_Pdf_Image::imageWithPath($filePath) method (JPG, PNG and TIFF images are supported now))
$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER);
/*$pageHeight = $page->getHeight();
$pageWidth = $page->getWidth();
echo 'Height = '.$pageHeight;
echo '
';
echo 'Width = '.$pageWidth;*/
//echo dirname(__FILE__);
$chompImage = Zend_Pdf_Image::imageWithPath('public/images/blume.jpg');
$pageHeight = $page->getHeight();
$pageWidth = $page->getWidth();
$imageHeight = 72;
$imageWidth = 72;
$topPos = $pageHeight - 36;
$leftPos = 36;
$bottomPos = $topPos - $imageHeight;
$rightPos = $leftPos + $imageWidth;
//$page->drawImage($chompImage, $leftPos, $bottomPos, $rightPos, $topPos);
$page->drawImage($chompImage, 100, 100, 400, 300);
$pdf->pages[0] = ($page);
$pdf->save("public/pdf/chomp.pdf");
error_message("Succeed In Creating PDF!");
注:需php的GD支持
4:加入文字:
$page->drawImage($chompImage, 100, 100, 400, 300);
$style = new Zend_Pdf_Style();
$style->setLineColor(new Zend_Pdf_Color_RGB(0.9, 0, 0));
$style->setFillColor(new Zend_Pdf_Color_GrayScale(0.2));
$style->setLineWidth(3);
$style->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD), 32);
$string="That's OK!";
$page->setStyle($style);
$page->drawText($string, $rightPos + 32, $topPos - 48);
$pdf->pages[0] = ($page);
$pdf->save("public/pdf/chomp.pdf");
error_message("Succeed In Creating PDF!");
5、创建图片
$string="That's OK!";
$style = new Zend_Pdf_Style();
$style->setLineColor(new Zend_Pdf_Color_RGB(0.9, 0, 0));
$style->setFillColor(new Zend_Pdf_Color_GrayScale(0.2));
$style->setLineWidth(3);
$style->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD), 32);
$page->setStyle($style);
$page->drawText($string, $rightPos + 32, $topPos - 48);
$page->drawRectangle(18, $pageHeight - 18, $pageWidth - 18,
18, Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE);
$pdf->pages[0] = ($page);
$pdf->save("public/pdf/chomp.pdf");
error_message("Succeed In Creating PDF!");
如果多行和多个图形及多个图象,只需要将其间隔排版好,在drawText,drawImage,drawRectangle多次就OK了.
阅读(3221) | 评论(2) | 转发(0) |