【问题标题】:how to get phalcon view render for wkhtmltopdf?如何获得 wkhtmltopdf 的 phalcon 视图渲染?
【发布时间】:2018-09-12 13:23:14
【问题描述】:

我在我的项目中使用Phalcon Framework。我已经生成了报告页面,我想将其转换为 pdf 并下载。经过大量谷歌搜索后,我得到了wkhtmltopdf,但它仅将 html 文件转换为 pdf。我有 phtml 文件。 Phalcon 可以将视图呈现为 html 字符串或我可以转换的任何其他方式吗?

这是我的代码:

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

【问题讨论】:

    标签: php pdf view wkhtmltopdf phalcon


    【解决方案1】:

    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();
    

    【讨论】:

    • 如果需要身份验证,渲染页面是否有问题?
    • 如果用户通过身份验证,请确保您正在呈现页面?在生成$view 之前检查用户是否已登录。
    • $html 变量为空
    • render() 中第二个参数的作用是什么?
    • 确保您设置了正确的ViewsDir 路径。对于可以在 .phtml 文件中使用的参数(变量),第二个参数是可选的。例如,您想个性化您的消息Hello $userNames 等...
    【解决方案2】:

    我认为 Mpdf 将是一个不错的选择。它更容易生成发票。我在我的一个项目中使用它。它很容易安装在您的 phalcon 项目中。

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

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

    第 2 步:运行此命令“composer require mpdf/mpdf”并等待安装完成。

    第3步:去vendor fodder。你会看到“mpdf”已经被添加了。忽略其他文件夹。

    第 4 步:转到您的控制器。复制以下行。因为您需要在运行时在自动加载器中加载外部模块。我希望您的供应商文件夹在根目录中。

    第 5 步:复制 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');
    
    
    
    }
    

    【讨论】:

    • 谢谢,但我需要一些可以从给定链接自动生成 HTML 然后将其转换为 pdf 的东西。所以,我找到了一个替代方案 - chrome headless
    猜你喜欢
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 2021-09-13
    • 2016-02-20
    • 1970-01-01
    相关资源
    最近更新 更多