使用 PHP 在浏览器中打开 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4750252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:14:39  来源:igfitidea点击:

open PDF in browser with PHP

phphtmlpdf

提问by user583311

I am doing:

我在做:

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
header('Content-Length: ' . filesize($file));
@readfile($file);

and it works fine. The PDF opens in the browser.

它工作正常。PDF 在浏览器中打开。

Can I do an if-then-else before? I tried and I get raw PDF

我可以先做 if-then-else 吗?我试过了,我得到了原始 PDF

%PDF-1.2 9 0 obj << /Type/Font /Subtype/Type1 /Name/F1 /FontDescriptor 8 0

Why is the if-then-else changing the headers?

为什么 if-then-else 更改标题?

thanks.

谢谢。

采纳答案by Michael

It would be helpful if you posted exactly what is your if-then-else code. My guess is you have some whitespace output before the headers are outputted.

如果您准确发布了 if-then-else 代码,那将会很有帮助。我的猜测是在输出标题之前你有一些空白输出。

回答by John Flatness

Make sure the code in your if-then-else block isn't causing anything to be output to the page.

确保 if-then-else 块中的代码不会导致任何内容输出到页面。

Once something's been output, calls to header()won't work anymore.

一旦输出了某些东西,对 的调用header()将不再起作用。