CakePHP和FPDF:浏览器中的视图标题 [英] CakePHP and FPDF: headers for view in browser

查看:137
本文介绍了CakePHP和FPDF:浏览器中的视图标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了许多有关 download pdf文件使用哪些标题的问题. 相反,我想在线(即使用嵌入式Chrome插件)查看它们,并可以选择将其下载.

I found many questions about which headers to use for download pdf files. Instead, I want to view them online (i.e. with the embedded Chrome plugin) and optionally download them with it.

这是我的CakePHP 3.7.9代码:

Here my code for CakePHP 3.7.9:

<?php
    header('Content-Type: application/pdf');
    require_once(ROOT . DS . 'vendor' . DS . 'fpdf182' . DS . 'fpdf.php');

    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();

相关控制器的功能为空.我将把要打印的变量放在那里. 浏览到测试pdf页面,我得到了未解码的数据:

the related controller's function is empty. I'm going to put there the variables to print. Browsing to the test pdf page I get the un-decoded data:

%PDF-1.3 3 0 obj<> endobj 4 0 obj<>流x 3R 2 35W( rQ w3T04 30PISp Z* [ (hx + (j * dd7W endstream endobj 1 0 obj<> endobj 5 0 obj<>流x ]R n 0 > L % DI 8 〜%E r ﻻvҪHX gvVk?/ Ῑ `] [ x5 3\z P } PO j Jݍ^ x6/f | 4} Z_K IQ Yd C K _%q 8> !! J''V!2&bGģ%r''H D }2EL1n h j e''aH : d 9c [ X1〜 "3 g Ñ.; o> endobj 2 0 obj<</ProcSet [/PDF/Text/ImageB/ImageC/ImageI]/Font<<</F1 6 0 R >>/XObject<<< >> >> endobj 7 0 obj</生产者(FPDF 1.82)/CreationDate(D:20191229180430)>> endobj 8 0 obj</lt/类型/目录/页1 0 R >> endobj外部参照0 9 0000000000 65535 f 0000000228 00000 n 0000000867 00000 n 0000000009 00000 n 0000000087 00000 n 0000000315 00000 n 0000000749 00000 n 0000000971 00000 n 0000001047 00000 n尾部<<<</<大小9/根8 0 R/Info 7 0 R >> startxref 1096 %% EOF

%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x�3R��2�35W(�r Q�w3T04�30PISp �Z*�[����(hx����+���(j*�d��7W endstream endobj 1 0 obj <> endobj 5 0 obj <> stream x�]R�n�0��>��L�%�DI�8���~�%Er�ﻻvҪHX�gvVk?/���Ῑ��`]�[�x5 �3\z��P�}����PO���j�Jݍ^���x6/f�����������|���4}�z�����}���@�,ۖ-��˺E�u�^�,���<� �Z_�K� IQ����Yd����C�K�_�%q�8>�!J"V!2&bGģ%r"H��D��}2EL1n��h�j���e��"aH����:��d��9c���[�X1~��"�3�g��Ñ�;O��> endobj 2 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 6 0 R >> /XObject << >> >> endobj 7 0 obj << /Producer (FPDF 1.82) /CreationDate (D:20191229180430) >> endobj 8 0 obj << /Type /Catalog /Pages 1 0 R >> endobj xref 0 9 0000000000 65535 f 0000000228 00000 n 0000000867 00000 n 0000000009 00000 n 0000000087 00000 n 0000000315 00000 n 0000000749 00000 n 0000000971 00000 n 0000001047 00000 n trailer << /Size 9 /Root 8 0 R /Info 7 0 R >> startxref 1096 %%EOF

在我看来,浏览器正在按原样读取内容,而不是将其解码为pdf. application/pdf标头不够吗? 我还需要其他哪些标题?

It seems clear to me that the browser is reading the content literally instead of decoding it as pdf. Isn't the application/pdf header enough? Which other headers I need?

如上所述,因为我不想默认下载文件,所以我没有设置文件名.

As said because I don't want to download the file by default, I'm not setting a filename.

推荐答案

在使用CakePHP时不要直接使用header(),也不要尝试手动将数据发送到浏览器,这只会引起问题,请务必使用CakePHP响应对象提供的抽象接口!

Do not user header() directly when using CakePHP, also don't try to send data to the browser manually, that's just going to cause problems, always use the abstract interfaces that the CakePHP response object provides!

如果要在视图层中设置标题,请使用$this->response,渲染后将其返回给控制器.但是,我倾向于说一个观点 template 通常不应该真正做出这样的决定,即观点本身,这是更合理的,而且在大多数情况下,控制器层是适当的地方.

If you want to set headers in the view layer, use $this->response, it's returned to the controller after rendering. However I would kinda be inclined to argue that a view template usually isn't really supposed to make such decisions, the view itself, that's more reasonable, and most of the time the controller layer will be the proper place.

无论如何,它在控制器,视图和视图模板级别上均相同,因此这里是一个示例:

Anyways, it works the same on controller, view and view template level, so here's an example:

$this->response = $this->response->withType('pdf');
$this->response = $this->response->withHeader(
    'Content-Disposition', 'inline; filename="some.pdf"'
);

请注意,您不能使用withDownload(),因为它将使用attachment而不是inline.

Note that you can't use withDownload() as that will use attachment instead of inline.

此外,如果使用视图模板,则应仅回显PDF内容,即相应地使用FPDF::Output()方法的$dest参数:

Also if you use a view template, you should just echo the PDF contents then, ie use the FPDF::Output() method's $dest argument accordingly:

echo $pdf->Output('S'); // S = return String

另请参见

这篇关于CakePHP和FPDF:浏览器中的视图标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆