如何获得 wkhtmltopdf 的 phalcon 视图渲染?

how to get phalcon view render for wkhtmltopdf?

我正在使用 Phalcon Framework in my project. I have generated reports page and I want to convert it to pdf and download. After lots of googling I get wkhtmltopdf,但它仅将 html 文件转换为 pdf。我有 phtml 文件。 Phalcon 能否将视图呈现为 html-strings 或任何其他我可以转换的方式?

这是我的代码:

$pdf = new mikehaertl\wkhtmlto\Pdf(APP_PATH . '\views\views\sales\salesreports.phtml');
if (!$pdf->send()) {
   throw new Exception('Could not create PDF: ' . $pdf->getError());
}

Phalcon 将模板渲染为变量 html 字符串:

$view = new \Phalcon\Mvc\View\Simple();
$view->setViewsDir(APP_PATH . '\app\views\views\sales\');
$optionalParams = [
    'var1' => 123,
    'var2' => 345,
];
// DO NOT put .phtml extension in the template name
$html = $view->render('salesreports', $optionalParams); 

我不熟悉 wkhtmlto\Pdf 库,但从文档来看应该是这样的:

use mikehaertl\wkhtmlto\Pdf;

$pdf = new Pdf;
$pdf->addPage($html);
$pdf->send();

我认为 Mpdf 会很棒 option.Its 更容易生成 invoice.I 在我的其中一个中使用它 project.It 非常容易安装在您的 phalcon 项目中。

第一步:通过cd..命令进入项目文件夹。

我希望你已经在你的项目中安装了 composer。

执行 2:Run 此命令 "composer require mpdf/mpdf" 并等待安装完成。

步骤 3:Go 到供应商 fodder.You 将看到 "mpdf" 已经 added.Ignore 其他文件夹。

步骤 4:Go 到您的控制器。复制以下 line.Coz 您需要在 运行 时间在自动加载器中加载外部模块。我希望你的供应商文件夹在根目录中。

步骤 5:Copy require_once DIR 。 '../../../vendor/autoload.php'; 一切就绪。

第 6 步:转到您的函数,例如 public function pdfgenerateAction(){}

第七步:

public function pdfgenerateAction()
{
    $email = $this->request->getPost("email");

    $mpdf = new \Mpdf\Mpdf();



    $mpdf->WriteHTML("<hr>");
     //Dont bother about this query.You can use Model as well.Just pass the array value to you foreach loop.
     $sheet = $this->modelsManager->createBuilder()
          ->columns("comment.comment, comment.username, comment.email, comment.postedat,item.name,item.photo,item.view,item.categoryid,item.id")
          ->From('comment')
          ->innerjoin('item', 'comment.productid = item.id')
          ->where("comment.email = '$email' ")
          ->getQuery()
          ->execute();        

    $table = "<table>
    <tr>
    <td >Given Name</td>
    <td >Email</td>
    <td >Bird Name</td>
    <td >Posted Time</td>
    <td >Your comment</td>
    <td >Total view by all </td>
    </tr>";
    foreach ($sheet as $row) {
        $table.= "<tr>
        <td >$row->username</td>
        <td >$row->email</td>
        <td >$row->name</td>
        <td >$row->postedat</td>
        <td >$row->comment</td>
        <td >$row->view</td>
        </tr>";
    }
    $table.= '</table>';

    $mpdf->WriteHTML($table);
    $mpdf->WriteHTML("<hr>");
    $mpdf->WriteHTML("<div style='display:block;position:fixed;bottom:0px;height:30px;width:100%' align='center'><strong>Your Footer of PDF</strong></div>");

    //$filename = $info->id;
    $filename = "Put your file name";
    //Crete folder inside your public folder
    $mpdf->Output("Foldername/$filename.pdf", 'F');




   $this->view->pick('pdf/response');
   // $this->flashSession->success("success :: PDF generated");
   // $this->response->redirect('user-home');



}