【问题标题】:Dompdf stream just downloads without dialog promptDompdf 流仅在没有对话框提示的情况下下载
【发布时间】:2018-08-02 02:15:14
【问题描述】:

我在我的 Symfony 3 项目中使用 Dompdf,并且在我尝试下载它之前一切正常。目前,它只是在没有我想要的对话框提示的情况下下载,根据文档,将值 1 传递给 Attachment 选项应该这样做。

这是我的代码:

$html = $this->renderView('pages/invoice_print.html.twig', array(
            'invoice' => $invoice_array
        ));

$dompdf = new Dompdf();
$dompdf->set_option('isHtml5ParserEnabled', true);
$dompdf->loadHtml($html);

$dompdf->setPaper('A4', 'portrait');

$dompdf->render();

$output = $dompdf->output();

$dompdf->stream("invoice_" . $invoice->getId(), array('Attachment' => 1));

说实话,我的印象是 Attachment 默认设置为 1 - 所以我不确定发生了什么。我正在使用 Firefox Quantum。

编辑:

现在附上 PDF 如何在浏览器中呈现的屏幕截图:

【问题讨论】:

    标签: symfony dompdf


    【解决方案1】:

    我过去没有与dompdf 合作过,但是,只要浏览一下 Github 上的代码,我相信我知道您需要什么。

    当您调用stream() 时,您有:

    if (!isset($options["compress"])) $options["compress"] = true;
    if (!isset($options["Attachment"])) $options["Attachment"] = true;
    

    因此,如果不存在 Attachment 选项,则该选项将设置为 true

    接下来,在第 1311 行,您有:

    $attachment = $options["Attachment"] ? "attachment" : "inline";
    header(Helpers::buildContentDispositionHeader($attachment, $filename));
    

    这基本上归结为Content-Disposition 标头:

    $contentDisposition = "Content-Disposition: $dispositionType; filename=\"$fallbackfilename\"";
    

    最后,根据标准,如果你想在网页中提供文件(例如没有提示对话框),你需要将 disposition 设置为inline

    综上所述,您是否尝试将Attachment 设置为false?

    $dompdf->stream("invoice_" . $invoice->getId(), array('Attachment' => false));
    

    希望这会有所帮助...

    【讨论】:

    • 如果我将 Attachment 设置为 0(我之前尝试过),它只会在屏幕上显示跳跃的文本(显然试图将 PDF 呈现给浏览器但失败了)。 PDF 会下载,但不会提示用户将其保存在让我的客户感到困惑的地方。
    • 等等,你真的想要文件在没有提示的情况下被下载吗?如果是这种情况,您肯定需要继续使用'Attachment' => false。您能否附上那个乱码 PDF 输出的屏幕截图示例?
    • 不,我需要对话框提示。设置为 false 将尝试在浏览器中呈现 PDF,这对我来说是失败的。我的客户需要下载它,但认为它失败了,因为没有直接指示它已下载,就像大多数时候弹出一个对话框并说“你想把它保存在哪里”等等。
    • 知道了,谢谢。确保您的客户知道inline 处置将文件下载到临时文件,因此他/她不希望在默认下载目录中找到它。您能否附上屏幕截图,以便我们了解渲染失败的情况?
    • 我附上了如果Attachment 设置为零会发生什么的屏幕截图。
    猜你喜欢
    • 2014-11-07
    • 1970-01-01
    • 2013-06-15
    • 2020-08-29
    • 2012-04-02
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    相关资源
    最近更新 更多