Php 如何获取wkhtmltopdf的phalcon视图渲染?

Php 如何获取wkhtmltopdf的phalcon视图渲染?,php,pdf,view,wkhtmltopdf,phalcon,Php,Pdf,View,Wkhtmltopdf,Phalcon,我正在我的项目中使用。我已经生成了报告页面,我想将其转换为pdf并下载。经过大量的谷歌搜索,我得到了,但它只是将html文件转换成pdf。我有phtml文件。 Phalcon可以将视图呈现为html字符串或任何其他我可以转换的方式吗 这是我的密码: $pdf = new mikehaertl\wkhtmlto\Pdf(APP_PATH . '\\views\\views\\sales\\salesreports.phtml'); if (!$pdf->send()) { throw

我正在我的项目中使用。我已经生成了报告页面,我想将其转换为pdf并下载。经过大量的谷歌搜索,我得到了,但它只是将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());
}

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

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将是一个很好的选择。它更容易生成发票。我在我的一个项目中使用了它。它很容易安装在您的phalcon项目中

步骤1:通过cd转到项目文件夹。。指挥部

我希望您已经在项目中安装了composer

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

第3步:转到供应商饲料。您将看到“mpdf”已添加。忽略其他文件夹

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

第5步:复制需要一次DIR”../..//供应商/autoload.php'; 一切就绪

步骤6:转到公共函数pdfGeneration(){}之类的函数

步骤7:

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



}
公共函数pdfgenerateAction()
{
$email=$this->request->getPost(“电子邮件”);
$mpdf=new\mpdf\mpdf();
$mpdf->WriteHTML(
); //不用担心这个查询。您也可以使用模型。只需将数组值传递给您foreach循环。 $sheet=$this->modelsManager->createBuilder() ->列(“comment.comment、comment.username、comment.email、comment.postedat、item.name、item.photo、item.view、item.categoryid、item.id”) ->来自('注释') ->innerjoin('item','comment.productid=item.id') ->其中(“comment.email=“$email”) ->getQuery() ->执行(); $table=” 姓名 电子邮件 鸟名 张贴时间 你的评论 全场视野 "; foreach($行){ $table.=” $row->username $row->电子邮件 $row->name $row->postedat $row->comment $row->view "; } $table.=''; $mpdf->WriteHTML($table); $mpdf->WriteHTML(
); $mpdf->WriteHTML(您的PDF页脚); //$filename=$info->id; $filename=“输入您的文件名”; //公用文件夹中的一个文件夹 $mpdf->Output(“Foldername/$filename.pdf”,“F”); $this->view->pick('pdf/response'); //$this->flashSession->success(“success::PDF生成”); //$this->response->redirect('user-home'); }
我认为Mpdf将是一个很好的选择。它更容易生成发票。我在我的一个项目中使用了它。它很容易安装到您的phalcon项目中

步骤1:通过cd转到项目文件夹。。指挥部

我希望您已经在项目中安装了composer

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

第3步:转到供应商饲料。您将看到“mpdf”已添加。忽略其他文件夹

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

第5步:复制需要一次DIR”../..//供应商/autoload.php'; 一切就绪

步骤6:转到公共函数pdfGeneration(){}之类的函数

步骤7:

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



}
公共函数pdfgenerateAction()
{
$email=$this->request->getPost(“电子邮件”);
$mpdf=new\mpdf\mpdf();
$mpdf->WriteHTML(
); //不用担心这个查询。您也可以使用模型。只需将数组值传递给您foreach循环。 $sheet=$this->modelsManager->createBuilder() ->列(“comment.comment、comment.username、comment.email、comment.postedat、item.name、item.photo、item.view、item.categoryid、item.id”) ->来自('注释') ->innerjoin('item','comment.productid=item.id') ->其中(“comment.email=“$email”) ->getQuery() ->执行(); $table=” 姓名 电子邮件 鸟名 张贴时间 你的评论 全场视野 "; foreach($行){ $table.=” $row->username $row->电子邮件 $row->name $row->postedat $row->comment $row->view "; } $table.=''; $mpdf->WriteHTML($table); $mpdf->WriteHTML(
); $mpdf->WriteHTML(您的PDF页脚); //$filename=$info->id; $filename=“输入您的文件名”; //公用文件夹中的一个文件夹 $mpdf->Output(“Foldername/$filename.pdf”,“F”); $this->view->pick('pdf/response'); //$this->flashSession->success(“success::PDF生成”); //$this->response->redirect('user-home'); }
如果需要身份验证,则呈现页面是否有问题?只要确保在用户经过身份验证的情况下呈现页面即可?生成
$view
之前,请检查用户是否已登录。$html变量为空render()中第二个参数的用途是什么?请确保设置了正确的
ViewsDir
路径。第二个参数对于可以在.phtml文件中使用的参数(变量)是可选的。例如,您想个性化您的邮件
Hello$userNames
等。如果需要身份验证,呈现页面是否有问题?只要确保在用户经过身份验证的情况下呈现页面即可?在生成
$view
之前,请检查用户是否已登录。$html变量为空第二个参数i的用途是什么