使用html2pdf使用php和mvc模式生成报告

使用html2pdf使用php和mvc模式生成报告,php,model-view-controller,pdf-generation,fpdf,Php,Model View Controller,Pdf Generation,Fpdf,我已经设置了一个应用程序,它以某种方式使用了前端控制器。我发现了一个名为html2pdf的库。此库将html转换为pdf 像这样: <?php $content = " <page> <h1>Exemple d'utilisation</h1> <br> Ceci est un <b>exemple d'utilisation</b> de <a

我已经设置了一个应用程序,它以某种方式使用了前端控制器。我发现了一个名为html2pdf的库。此库将html转换为pdf

像这样:

<?php
    $content = "
     <page>
      <h1>Exemple d'utilisation</h1>
      <br>
      Ceci est un <b>exemple d'utilisation</b>
      de <a href='http://html2pdf.fr/'>HTML2PDF</a>.<br>
     </page>";

   require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
   $html2pdf = new HTML2PDF('P','A4','fr');
   $html2pdf->WriteHTML($content);
   $html2pdf->Output('exemple.pdf');
?>
我的想法是,不要渲染模板,而是用关于我的
信息替换
$someinfo
变量,因为我使用了php的
提取
函数

我可以替换变量,然后将输出保存为html,这样我就可以使用html2pdf将其转换为pdf

是否已经实施了该计划?还是有比创建和转换html文件更有效的解决方案


谢谢。

在ZendFramework MVC中,您可以在控制器中执行以下操作:

function toPdfAction()  { 
   // I need a different layout 
   $this->getHelper('layout')->setLayout('pdf-layout'); 
   $this->_helper->layout->disableLayout(); 

   // we need to do renderering ourselves.   
   $this->getHelper('viewRenderer')->setNoRender(); 

   /* @var $layout Zend_Layout */ 
   $layout = $this->_helper->layout->getLayoutInstance(); 
   $layout->assign('content', $this->view->render('mycontroller/myaction.tpl')); 

   $output = $layout->render(); 

   // now we still need to ensure that the rendering is not sent to the browser 
   $this->getResponse()->clearBody(); 

   // now do something with $ouput like convert it to PDF and stream it back
} 

然而,答案取决于MVC实现。如果您不使用Zend,那么这可能对您不起作用。你使用的是什么MVC框架?

我创建了一个迷你MVC框架,是的,我认为这不适用于我的框架,但我可以看看zend是如何做到的。嗯。。将这样的东西放在控制器中会违反MVC的基本原则和受MVC启发的设计模式。Zend这样做并不意味着这是一个好的实践。@tereško,是的,它肯定会打破“好的实践”。我不知道我该怎么做。
function toPdfAction()  { 
   // I need a different layout 
   $this->getHelper('layout')->setLayout('pdf-layout'); 
   $this->_helper->layout->disableLayout(); 

   // we need to do renderering ourselves.   
   $this->getHelper('viewRenderer')->setNoRender(); 

   /* @var $layout Zend_Layout */ 
   $layout = $this->_helper->layout->getLayoutInstance(); 
   $layout->assign('content', $this->view->render('mycontroller/myaction.tpl')); 

   $output = $layout->render(); 

   // now we still need to ensure that the rendering is not sent to the browser 
   $this->getResponse()->clearBody(); 

   // now do something with $ouput like convert it to PDF and stream it back
}