使用 dompdf 使用动态变量值渲染 html

render html with dynamic variable values with dompdf

我正在尝试将值传递给模板视图文件中的变量,并使用 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 如何加载值集?

包括 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>