PHP - DOMPDF 1 函数有效,另一个则无效

发布于 2024-12-25 19:16:24 字数 1146 浏览 2 评论 0原文

我目前正在使用 DOMPDF,并试图找出为什么我的 PDF 没有正确创建。

当我运行以下代码时,

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=TRUE) 
{
    require_once("dompdf/dompdf_config.inc.php");

    $dompdf = new DOMPDF();
    $dompdf->set_base_path($_SERVER['DOCUMENT_ROOT']);
    $dompdf->output_html();
    //$dompdf->load_html($html);
    //$dompdf->set_paper("a4", "portrait" );
    //$dompdf->render();
    //$dompdf->stream($filename . ".pdf");
}
?>

在浏览器中我看到了我期望生成的 PDF 内容,但是如果我运行此代码,

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=TRUE) 
{
    require_once("dompdf/dompdf_config.inc.php");

    $dompdf = new DOMPDF();
    $dompdf->set_base_path($_SERVER['DOCUMENT_ROOT']);
    //$dompdf->output_html();
    $dompdf->load_html($html);
    $dompdf->set_paper("a4", "portrait" );
    $dompdf->render();
    $dompdf->stream($filename . ".pdf");
}
?>

我不会得到任何响应,而我期望得到一个下载窗口。这是有原因的吗?我想知道格式不正确的 HTML 是否会导致问题?

I am working with DOMPDF currently, and I trying to work out why my PDF's are not being created correctly.

When I run the following code,

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=TRUE) 
{
    require_once("dompdf/dompdf_config.inc.php");

    $dompdf = new DOMPDF();
    $dompdf->set_base_path($_SERVER['DOCUMENT_ROOT']);
    $dompdf->output_html();
    //$dompdf->load_html($html);
    //$dompdf->set_paper("a4", "portrait" );
    //$dompdf->render();
    //$dompdf->stream($filename . ".pdf");
}
?>

In my browser I see the PDF content that I expect to be generated, however if I run this code,

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=TRUE) 
{
    require_once("dompdf/dompdf_config.inc.php");

    $dompdf = new DOMPDF();
    $dompdf->set_base_path($_SERVER['DOCUMENT_ROOT']);
    //$dompdf->output_html();
    $dompdf->load_html($html);
    $dompdf->set_paper("a4", "portrait" );
    $dompdf->render();
    $dompdf->stream($filename . ".pdf");
}
?>

I get no repsonse, where I would be expecting to get a download window. Is there a reason for this, I wonder if poorly formatted HTML could the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2025-01-01 19:16:24

您的 CI 插件代码应该可以在 1.7.x 上运行。心意如下,供大家参考:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

function pdf_create($html, $filename, $stream=true) {
        require_once("dompdf/dompdf_config.inc.php");
        spl_autoload_register('DOMPDF_autoload');
        $dompdf = new DOMPDF();
        $dompdf->load_html($html);
        $dompdf->render();
        if ($stream) {
                $dompdf->stream($filename);
        } else {
                $CI =& get_instance();
                $CI->load->helper('file');
                write_file($filename, $dompdf->output());
        }
}

Your CI plugin code should work on 1.7.x. Here's mind for your reference:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

function pdf_create($html, $filename, $stream=true) {
        require_once("dompdf/dompdf_config.inc.php");
        spl_autoload_register('DOMPDF_autoload');
        $dompdf = new DOMPDF();
        $dompdf->load_html($html);
        $dompdf->render();
        if ($stream) {
                $dompdf->stream($filename);
        } else {
                $CI =& get_instance();
                $CI->load->helper('file');
                write_file($filename, $dompdf->output());
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文