【问题标题】:Help needed creating PDF in Zend Framework在 Zend Framework 中创建 PDF 需要帮助
【发布时间】:2011-05-30 19:39:26
【问题描述】:

我在我的应用程序中使用 Doctrine 1.2.3,我正在尝试使用 zend_pdf_table 组件根据以下教程 http://devzone.zend.com/article/12492-Creating-PDF-Documents-with-Zend-Framework 创建 PDF 报告。

编辑文档已创建,但我只能让第一条记录出现在报告中,尽管尝试添加我自己的样式,但仍然出现样式错误。

我可能会离开这个,因为我的项目时间不多了,我会考虑从头开始创建自己的 PDF。

[error] [client 127.0.0.1] PHP Notice:  Trying to get property of non-object in C:\\WebDocs\\xx\\library\\Zend\\Pdf\\Style.php on line 211

我修改后的代码如下

// report to print todays appointments
   public function todaysgroomingappointmentsAction()
   {
    try{

    $t=date('y-m-d');

  $q = Doctrine_Query::create()
    ->select('CONCAT(g.gapmtSTime, g.gapmtETime) AS Time, CONCAT(c.firstname, c.lastname) AS Client, p.name AS Pet, r.groomprocedure AS Procedure, u.name AS Groomer')
    ->from('PetManager_Model_Groomappointments g')
    ->leftJoin('g.PetManager_Model_Clients c')
    ->leftJoin('g.PetManager_Model_Pets p')
    ->leftJoin('g.PetManager_Model_Users u')
    ->leftJoin('g.PetManager_Model_Groomservices s')
    ->leftJoin('s.PetManager_Model_Groomprocedures r')
    ->where('g.gapmtStatus = 1 AND g.gapmtDate = ?',$t);
    $result = $q->fetchArray();

  require_once 'Zend/Pdf.php';;         
  require_once 'My/Pdf.php';    
  require_once 'My/Pdf/Document.php';
  require_once 'My/Pdf/Page.php';
  require_once 'My/Pdf/Table.php';
  require_once 'My/Pdf/Table/Row.php';
  require_once 'My/Pdf/Table/Cell.php';
  require_once 'My/Pdf/Table/Column.php';
  require_once 'My/Pdf/Table/HeaderRow.php';  

    // create PDF
    $pdf = new My_Pdf_Document('todaysappointments.pdf', 'D:/');

    // create page
    $page = $pdf->createPage();

    // define font resource
   $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);

   // set font
  $page->setFont($font, 24);

  // create table
 $table = new My_Pdf_Table(4);

 // iterate over record set
 // set up table content
  foreach ($result as $record) {
   $row = new My_Pdf_Table_Row();
  $cols = array();
  foreach ($record as $k => $v) {
   $col = new My_Pdf_Table_Column();
    $col->setText($v);
    $cols[] = $col;      
  }
  $row->setColumns($cols);
  $row->setFont($font, 14);
  $row->setBorder(My_Pdf::TOP, new Zend_Pdf_Style());
  $row->setBorder(My_Pdf::BOTTOM, new Zend_Pdf_Style());
  $row->setBorder(My_Pdf::LEFT, new Zend_Pdf_Style());
  $row->setCellPaddings(array(10,10,10,10));
  $table->addRow($row);
 }

  // add table to page
 $page->addTable($table, 0, 0);

 // add page to document
 $pdf->addPage($page);

 // save as file
 $pdf->save('todaysappointments.pdf');
  echo 'SUCCESS: Document saved!';  
  } catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
 } catch (Exception $e) {
 die ('Application error: ' . $e->getMessage());    
}
} 

谁能告诉我为什么会出现这个错误以及如何解决它。

非常感谢。

【问题讨论】:

  • Zend_Pdf_Style line #211 是 return $this->_lineWidth->value; 所以我猜这是 $_lineWidth 属性的问题
  • 只是一个旁注:这里有点autloading 将有助于收紧一点。
  • @Phil lineWidth 实际上是由包本身提供的 我试图在我的代码中输入它,但我仍然收到错误。

标签: zend-framework doctrine


【解决方案1】:

如果需要保存,则必须将文件名作为参数添加到 $pdf->save(),如果只需要打印内容,可以使用 echo $pdf->render(),当然需要发送权限pdf 的标题。

关于样式,您必须使用 Zend_Pdf_Style 定义任何样式,而不仅仅是传递空的 new Zend_Pdf_Style(),例如:

$style = new Zend_Pdf_Style(); $style->setLineColor(new Zend_Pdf_Color_Rgb(0, 0, 0)); $style->setLineWidth(1);

【讨论】:

  • 这个包实际上允许你指定文件和目录来保存文件,我现在可以打开它,因为我没有正确放置 try 块。愚蠢的我知道,但是我不能在生成的文档中出现超过一条记录,而且我仍然遇到样式错误。我确实将文档名称添加到了 save 参数中。
  • 您必须在Zend_Pdf_Style 中至少定义$_lineWidth。我在该类中检查了它,默认变量设置为null,但是您正在创建的行正在尝试在样式类上调用方法getLineWidth(),在此方法内部存在行return $this->_lineWidth->value;,最后它尝试读取@987654326 @来自null。尝试实例化 Zend_Pdf_Style 并调用它 setLineWidth() 即使是 0,它也会有所帮助。
  • @ Arek Jablonski 我在变量名 myStyle 下实例化了 Zend_Pdf_Style 并添加了您在回答中建议的行,但是我仍然收到与基础 zend 类的第 211 行相关的错误,并且只有一个的记录正在打印在文档中。感谢您的帮助,由于截止日期,我正在考虑继续前进,并且我在导出到 MS-Excel 时遇到问题,我目前正在查看/寻求帮助。
猜你喜欢
  • 2013-02-19
  • 1970-01-01
  • 2020-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 2013-12-08
相关资源
最近更新 更多