简体   繁体   English

FPDF向我返回文本而不是PDF文件

[英]FPDF return me text instead of PDF File

I use FPDF to generate some PDF from a PHP. 我使用FPDF从PHP生成一些PDF。 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吗?

%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 而且我的php很长,这是它的pastebin http://pastebin.com/ri5uAeie

My php looks correct, really weird no ? 我的PHP看起来正确,真的很奇怪吗?

UPDATE 更新

I pass my param throw the URL and with a $_GET and it works well :) Thanks for your help guys ! 我通过参数抛出URL并带有$ _GET,它很好用:)谢谢您的帮助!

You've probably added a blank line somewhere, possibly at inc/DataLib.php . 您可能已在inc/DataLib.php某处添加了空行。

Update #1: 更新#1:

According to your answers, there's two suspicious points: 根据您的回答,有两个可疑点:

  1. The Content-Type should be application/pdf . Content-Type应为application/pdf In theory, the PDF::Output() method adds it for you (unless php_sapi_name() returns cli , which should not be possible in web environment). 从理论上讲, PDF :: Output()方法为您添加了它(除非php_sapi_name()返回cli ,这在网络环境中是不可能的)。

  2. 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. 在台式机PDF阅读器中获取空白页(而不是出现错误),表明文档已损坏,阅读器甚至无法检测到该文档...或者文档实际上是空白的。

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. 对于#1(防火墙,代理或防病毒?)一无所知,但您至少可以检查PDF源,以查找PHP错误消息或不属于此的内容。

Update #2: 更新#2:

I think I finally understood what your problem is. 我想我终于明白了您的问题所在。 You cannot use JavaScript to retrieve a PDF file though AJAX. 您不能使用JavaScript通过AJAX检索PDF文件。 All JavaScript can do is: JavaScript所有可以做的是:

  1. Manipulate the page's DOM (and PDF is not HTML). 处理页面的DOM(PDF不是HTML)。
  2. Replace the complete DOM page with something else (and there's no way to tell the browser what content-type it is). 用其他内容替换整个DOM页面(并且无法告诉浏览器它是什么内容类型)。

I suspect your PDF generator is just fine if you call it directly from the location bar. 我怀疑您直接从位置栏调用PDF生成器就可以了。

That's how calling FPDF::Output() with no arguments work. 这就是不带参数调用FPDF::Output() It just sets appropriate headers and sends PDF as text to browser. 它只是设置适当的标题,然后将PDF作为文本发送到浏览器。 If you want the file to be downloaded, you need to provide Output() with filename and set second argument to 'D' 如果要下载文件,则需要为Output()提供文件名,并将第二个参数设置为'D'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM