简体   繁体   English

使用PHP无法正常显示PDF到浏览器

[英]Displaying PDF to Browser with PHP not working

Using php's ability to call upon the browser to display a given PDF it doesn't work on my web host. 使用php的能力调用浏览器显示给定的PDF,它在我的网络主机上不起作用。 But it works on my local xamp server with apache. 但它可以在我的本地xamp服务器上使用apache。

PHP: PHP:

    $title = $_GET['title'];
    $loc = $_GET['loc'];

    header("Content-type: application/pdf");
    header("Content-Disposition: inline; filename=".$title);
    @readfile($loc);

Expected Output: Supposed to be the PDF document being rendered like it does on my local web server. 预期输出:假设是在我的本地Web服务器上呈现的PDF文档。

Actual but unwanted output: %PDF-1.6 % 1638 0 obj <> endobj xref 1638 44 0000000016 00000 n ... 实际但不需要的输出: %PDF-1.6% 16380 obj <> endobj xref 1638 44 0000000016 00000 n ...

Can be seen here: http://neilau.me/view.php?title=business-studies-hsc-notes-2006.pdf&loc=pdf/criteria/business-studies/2006/business-studies-hsc-notes-2006.pdf 可以在这里看到: http//neilau.me/view.php?title = business-studies-hsc-notes-2006.pdf&loc = pdf / criteria / business-studie / 2006 / business-studies-hsc-notes-2006 .PDF

Is this fixed through changing something on my cpanel? 这是通过改变我的cpanel上的东西来解决的吗? As my code isn't faulty... It works on my local web server. 因为我的代码没有错误...它可以在我的本地Web服务器上运行。

Use this code 使用此代码

<?php
$file = 'dummy.pdf';
$filename = 'dummy.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
@readfile($file);
?>

You need to make sure you are not sending any text before writing headers. 在编写标题之前,您需要确保没有发送任何文本

Example of what not to do : 什么不该做的例子:

<!DOCTYPE html>
<html>
...
<?php
header("Content-type: application/pdf");

Example of how to fix that: 如何解决这个问题的示例:

<?php
header("Content-type: application/pdf");    
?>   
<!DOCTYPE html>
<html>
...

In addition your script is very insecure. 另外你的脚本非常不安全。 Here's what you should do, your entire PHP script should be: 这是你应该做的,你的整个PHP脚本应该是:

<?php
$loc = filter_input(INPUT_GET,"loc");
$title = filter_input(INPUT_GET,'title')?:basename($loc);
if (!is_readable($loc)) {
    http_response_code(404);
    echo "Cannot find that file";
    die();
}
if (strtolower(pathInfo($loc,PATHINFO_EXTENSION)) != "pdf") {
   http_response_code(403);
    echo "You are not allowed access to that file";
    die();
}

header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$title);
header("Content-Length: ".filesize($loc));
readfile($loc);

If you want to show things like a page title or a frame around it, you should use an iframe in another "wrapper" page: 如果要显示页面标题或框架周围的内容,则应在另一个“包装器”页面中使用iframe:

<?php 
$loc = filter_input(INPUT_GET,"loc");
$title = filter_input(INPUT_GET,'title')?:basename($loc);
?>
<html><head><title>My title</title></head>
<body>
<iframe src="/view.php?<?php echo ($loc?"loc=$loc":"").($title?"title=$title":"") ?>">
</iframe>
</body>
<html>

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

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