【发布时间】:2023-01-04 23:25:29
【问题描述】:
我正在使用 DomPDF 生成 PDF 文档。代码看起来像这样:
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('This is my HTML');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream('My_file.pdf', ['Attachment' => false]);
换句话说,完全标准。我遇到的问题是,当我将 PDF 内容“流”到客户端时,它带有 HTTP 标头:
Cache-Control: private
这意味着该文档可能会被浏览器缓存。当我更改文档并使用相同的链接再次打开它时,这一点很明显。我会看到旧版本。按 F5(在 Windows 上)可以解决这个问题,但我想将标题更改为:
Cache-Control: no-cache, no-store, must-revalidate
如果我像这样在 PHP 中设置标头:
header('Cache-Control: no-cache, no-store, must-revalidate');
在流式传输 PDF 之前,它会被覆盖,一旦流式传输完成,我显然无法更改它。
我找不到办法做到这一点。
有人知道如何更改 DomPDF 使用的 HTTP 标头吗?
【问题讨论】:
-
相关的 github 问题在这里打开:github.com/dompdf/dompdf/issues/3098
标签: php http-headers dompdf