FPDF 返回文本而不是 PDF 文件
我使用 FPDF 从 PHP 生成一些 PDF。它对于小例子来说效果很好,但现在我遇到了一个问题:它返回一些我在控制台中看到的数据,但没有文件!
输出看起来像 - 这是 PDF 不是吗?
%PDF-1.4
0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>
......
<<
/Size 15
/Root 14 0 R
/Info 13 0 R
>>
startxref
22291
%%EOF
我的 php 很长,这是它的一个粘贴 http://pastebin.com/ri5uAeie
我的 php看起来正确,真的很奇怪不是吗?
更新
我通过我的参数抛出 URL 并使用 $_GET ,效果很好:) 感谢你们的帮助!
I use FPDF to generate some PDF from a PHP. It worked great for small exemple but now i got a problem with it : it return some data which i saw in the console but no file !
ouput look likes - it's the PDF no ?
%PDF-1.4
0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>
......
<<
/Size 15
/Root 14 0 R
/Info 13 0 R
>>
startxref
22291
%%EOF
And my php is pretty long, here is a pastebin of it http://pastebin.com/ri5uAeie
My php looks correct, really weird no ?
UPDATE
I pass my param throw the URL and with a $_GET and it works well :) Thanks for your help guys !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能在某处添加了一个空行,可能是在
inc/DataLib.php
处。更新#1:
根据您的回答,有两个可疑点:
Content-Type
应该是application/pdf
。理论上, PDF::Output() 方法会为您添加它(除非php_sapi_name()
返回cli
,这在 Web 环境中不应该是可能的)。不知道#1(防火墙?代理?防病毒?),但您至少可以检查 PDF 源以搜索 PHP 错误消息或不属于其中的内容。
更新#2:
我想我终于明白你的问题是什么了。您无法使用 JavaScript 通过 AJAX 检索 PDF 文件。 JavaScript 能做的就是:
我怀疑如果您直接从地址栏调用 PDF 生成器就可以了。
You've probably added a blank line somewhere, possibly at
inc/DataLib.php
.Update #1:
According to your answers, there's two suspicious points:
The
Content-Type
should beapplication/pdf
. In theory, the PDF::Output() method adds it for you (unlessphp_sapi_name()
returnscli
, which should not be possible in web environment).Getting a blank page in a desktop PDF reader (rather than an error) suggests that the document is damaged in a way that the reader cannot even detect it... or that the document is actually blank.
No idea about #1 (firewall? proxy? antivirus?) but you could at least inspect the PDF source in search of PHP error messages or something that does not belong there.
Update #2:
I think I finally understood what your problem is. You cannot use JavaScript to retrieve a PDF file though AJAX. All JavaScript can do is:
I suspect your PDF generator is just fine if you call it directly from the location bar.
这就是不带参数调用
FPDF::Output()
的工作原理。它只是设置适当的标题并将 PDF 作为文本发送到浏览器。如果您想要下载文件,则需要向Output()
提供文件名,并将第二个参数设置为'D'
That's how calling
FPDF::Output()
with no arguments work. It just sets appropriate headers and sends PDF as text to browser. If you want the file to be downloaded, you need to provideOutput()
with filename and set second argument to'D'