Slim Framework 为 PDF 设置名称

Slim Framework set name for PDF

我从 Slim Framework 下载 PDF 时没有设置 pdf 名称,因为我已经通过 PDF 设置选项为 pdf 分配了名称

像这样:

<?php
    $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
    $response = $this->response->withHeader('Content-type', 'application/pdf');
    $response->write($pdf->Output('My_cool_PDF.pdf', 'S'));
?>

但是文件是通过分配通过 html 的路由名称来下载的。

示例 Url:http://localhost:8080/collections/getBranchWisePDF/1

如果参数设置为 1,则文件名是 1,它被分配给 pdf 并且文件被下载

使用 TCP Pdf 和 Slim Framework 更改 pdf 名称的任何选项。

提前致谢

我不知道你用哪个库来处理 PDF 文件,所以我不能完全理解 $pdf->Output('My_cool_PDF.pdf', 'S') 部分,但是你可以尝试通过发送额外的 header 你的回复:

<?php
    $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
    $response = $this->response->withHeader('Content-type', 'application/pdf')
        // Here we appned another header to let the browser know about the file name
        ->withAddedHeader('Content-Disposition', 'attachment; filename=My_cool_PDF.pdf');
    $response->write($pdf->Output('My_cool_PDF.pdf', 'S'));
?>