【问题标题】:render html with dynamic variable values with dompdf使用 dompdf 呈现具有动态变量值的 html
【发布时间】:2018-06-05 08:14:25
【问题描述】:

我正在尝试将值传递给模板视图文件中的变量,并使用 dompdf 加载 html。

但是传递的值不会被评估和渲染。

这是我的控制器功能:

    $dompdf = new Dompdf();
    $this->f3->set('user_name', 'Ross Geller');
    $this->f3->set('total_amount_due', 2270.00);
    $this->f3->set('amount_received', 1000.00);
    $this->f3->set('remaining_amount', 1270.00);
    $this->f3->set('received_by',  'Rakesh Mali');
    $this->f3->set('received_on', '2018-06-05 06:00:00');

    $template = new \View;
    $html =  $template->render('lunch/invoice.html');
    $dompdf->loadHtml($html);

// (Optional) Setup the paper size and orientation
    $dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
    $dompdf->render();

// Output the generated PDF to Browser
    $dompdf->stream();

模板文件(invoice.html):

<label class="control-label col-sm-2" for="email">A/C Payee:</label>
<div class="col-sm-3">
    {{@user_name}}
</div>

<label class="control-label col-sm-2" for="pwd">Pending Total Amount:</label>
<div class="col-sm-10"> 
    Rs. <span class="amount_holder">{{@total_amount_due}}</span>
</div>

这是渲染的:

A/C Payee:
{{@user_name}}
Pending Total Amount:
Rs. {{@total_amount_due}}

如何用设置的值加载html?

【问题讨论】:

    标签: php html pdf dompdf fat-free-framework


    【解决方案1】:

    包含 html 模板文件,但作为 PHP 文件.. 然后像在 PHP 脚本中那样使用 $ 而不是 @

    引用变量

    示例

        $dompdf = new Dompdf();
        $user_name = 'Ross Geller';
        $total_amount_due = 2270.00;
    
        ob_start();
        require('invoice_template.php');
        $html = ob_get_contents();
        ob_get_clean();
        $dompdf->loadHtml($html);
    
    // (Optional) Setup the paper size and orientation
        $dompdf->setPaper('A4', 'landscape');
    
    // Render the HTML as PDF
        $dompdf->render();
    
    // Output the generated PDF to Browser
        $dompdf->stream();
    

    那么在invoice_template.php文件中有以下内容;

    <label class="control-label col-sm-2" for="email">A/C Payee:</label>
    <div class="col-sm-3">
        <?=$user_name?>
    </div>
    
    <label class="control-label col-sm-2" for="pwd">Pending Total Amount:</label>
    <div class="col-sm-10"> 
        Rs. <span class="amount_holder"><?=$total_amount_due?></span>
    </div> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多