【问题标题】:Open generated pdf in browser in another tab在另一个选项卡中的浏览器中打开生成的 pdf
【发布时间】:2026-01-13 08:15:02
【问题描述】:

我正在通过 FPDF 生成 pdf。现在我只需要在浏览器中打开这个生成的 pdf。

搜索了很多,但得到的只是现有 pdf 的解决方案,在这里我们需要通过 fpdf 生成 pdf 的解决方案。

以下是我的代码:

<?php   require('../pdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);  
$pdf->Cell(40,10,'This is demo');
$pdf->Output();
?>

【问题讨论】:

  • 已解决,禁用 IDM 扩展后即可使用!

标签: php fpdf


【解决方案1】:

快速浏览FPDF documentation 表明您可以向Output() 函数调用添加几个参数,以在浏览器中提供显示或下载功能

string Output([string dest [, string name [, boolean isUTF8]]])

查看更多here

例如:

<?php   require('../pdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);  
$pdf->Cell(40,10,'This is demo');
$pdf->Output('I');
?>

上面的例子使用 'I' 表示内联。其他选项是:

I: send the file inline to the browser. The PDF viewer is used if available.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string.

documentation 中都有。

【讨论】:

  • 如果你能在上面的代码中展示如何实现它会很有帮助
【解决方案2】:

对于仍在寻找的人,将 target="_blank" 添加到您的表单标签将在新窗口中打开 PDF。

HTML:

<form method="GET" action="/target-destination/" id="pdfForm"  target="_blank">
 <!-- Content goes here -->
</form>

mPDF:

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Hello World');

$mpdf->Output('filename.pdf', 'I');

【讨论】:

    【解决方案3】:

    FPDF 内联方法演示将尝试以不安全的方式打开 About:Blank,这不应该作为内联显示的方法,它只是触发浏览器中的安全响应以自动下载到沙箱中的 AV 检查。使用 A Href=download 之类的传统方法之一,我会更相信它,并且可以先在您的链接上运行 URL 检查器...

    【讨论】:

      最近更新 更多