【问题标题】:Pass data to dompdf (not with Laravel)将数据传递给 dompdf(不适用于 Laravel)
【发布时间】:2023-01-12 14:55:17
【问题描述】:

我已经看到很多使用 Laravel 的答案,但我没有使用框架,而是想传递一些数据。是否可以?我尝试将 $data 作为第二个参数传递给 loadHtml,但这不起作用。

        $options = new Options();
        $options->set('defaultFont', 'Arial');
        $dompdf = new Dompdf($options);

        $data = ['test' => 'here is the data I want to pass'];
    
        ob_start();
        require("mypath/templates/pdf-file.php");
        $html = ob_get_contents();
        ob_get_clean();
    
        
        $dompdf->loadHtml($html);
    
        $dompdf->setPaper('A4', 'portrait');
        
        $dompdf->set_option('isHtml5ParserEnabled', true);
        $dompdf->set_option('isRemoteEnabled', true);

        $dompdf->render();

        $dompdf->stream();

【问题讨论】:

  • @DarkBee 好笑,我以前试过,但似乎没有用,但现在再试一次,它成功了!太棒了,谢谢!您能否将其添加为您的答案,以便我接受。

标签: php dompdf


【解决方案1】:

在 require 或 include 之前定义的变量可以在 required/included 文件中访问。这意味着如果pdf-file.php 是生成HTML 的文件,您可以只使用pdf-file.php 内的变量$data 来填充构建PDF 所需的(动态)信息

【讨论】:

    猜你喜欢
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 2015-10-15
    • 2016-05-29
    • 2016-04-14
    • 1970-01-01
    相关资源
    最近更新 更多