Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1261687
  • 博文数量: 315
  • 博客积分: 10397
  • 博客等级: 上将
  • 技术积分: 3731
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-07 21:21
文章分类

全部博文(315)

文章存档

2015年(10)

2014年(3)

2013年(2)

2012年(8)

2011年(8)

2010年(29)

2009年(59)

2008年(77)

2007年(119)

分类:

2008-01-07 13:54:42

众所周之,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了.
 
阅读(3183) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-03-16 15:55:46

我只想实现第一步,那么代码应该写到控制(controllers)里还是视图(view)里呢??请教!!

chinaunix网友2009-07-01 16:16:45

收获不少,谢谢