通过 PHP/Perl 在用户浏览器中显示 PDF 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4679756/
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 13:57:14  来源:igfitidea点击:

Show a PDF files in users browser via PHP/Perl

phpperlpdf

提问by dimassony

I want to show my users PDF files. The reason why I use cgi to show the pdf is I want to track the clicks for the pdf, and cloak the real location of the saved pdf.

我想向我的用户展示 PDF 文件。我使用 cgi 来显示 pdf 的原因是我想跟踪 pdf 的点击次数,并隐藏保存的 pdf 的真实位置。

I've been searching on the Internet and only found how to show save dialog to the users and creating a pdf, not show the files to the users.

我一直在互联网上搜索,只找到了如何向用户显示保存对话框并创建 pdf,而不是向用户显示文件。

What I wanted for is showthe users my pdf files, not creating or downloadthe pdf. Here is what I got form the official php documentation:

我想要的是用户展示我的 pdf 文件,而不是创建或下载pdf。这是我从官方 php 文档中得到的:

<?php
header('Content-type: application/pdf');
readfile('the.pdf');
?>

Also my google-search-result perl code:

还有我的 google-search-result perl 代码:

open(PDF, "the.pdf") or die "could not open PDF [$!]";
binmode PDF;
my $output = do { local $/; <PDF> };
close (PDF);

print "Content-Type: application/pdf\n";
print "Content-Length: " .length($output) . "\n\n";
print $output

if you do it on ruby, please say it to me. But I'm not sure if my server support rails.

如果你是在 ruby​​ 上做的,请告诉我。但我不确定我的服务器是否支持导轨。

Sorry if my code is too far away from the method to show the pdf, since I don't know anything about pdf processing and how to implement this problem.

抱歉,如果我的代码与显示 pdf 的方法相距太远,因为我对 pdf 处理以及如何实现此问题一无所知。

Lets assume that the users have the Adobe Reader plug-in. So, how to fix my problem?

让我们假设用户有 Adob​​e Reader 插件。那么,如何解决我的问题?

edit: I want to show plain pdf file. My primary purpose: track my pdf files and use some fancy urls.

编辑:我想显示普通的 pdf 文件。我的主要目的:跟踪我的 pdf 文件并使用一些花哨的 url。

edit: Here's my main php code:

编辑:这是我的主要 php 代码:

<?php
$file='/files/the.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
@readfile($file);
?>

edit: Now the code is working. But the loading progress bar (on Adobe Reader X plugin) doesn't shows up. Why? Anyone can help me? Here's my main code:

编辑:现在代码正在运行。但是加载进度条(在 Adob​​e Reader X 插件上)没有显示。为什么?任何人都可以帮助我吗?这是我的主要代码:

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

edit: All my problems solved. Here's the final code:

编辑:我所有的问题都解决了。这是最终的代码:

<?php
$file = './path/to/the.pdf';
$filename = 'Custom file name for the.pdf'; /* Note: Always use .pdf at the end. */

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);
?>

Thanks! :)

谢谢!:)

回答by Stephen

I assume you want the PDF to display in the browser, rather than forcing a download. If that is the case, try setting the Content-Dispositionheader with a value of inline.

我假设您希望 PDF 显示在浏览器中,而不是强制下载。如果是这种情况,请尝试将Content-Disposition标头设置为inline

Also remember that this will also be affected by browser settings - some browsers may be configured to alwaysdownload PDF files or open them in a different application (e.g. Adobe Reader)

还要记住,这也会受到浏览器设置的影响——某些浏览器可能被配置为始终下载 PDF 文件或在不同的应用程序中打开它们(例如 Adob​​e Reader)

回答by Kareem

    $url ="https://yourFile.pdf";
    $content = file_get_contents($url);

    header('Content-Type: application/pdf');
    header('Content-Length: ' . strlen($content));
    header('Content-Disposition: inline; filename="YourFileName.pdf"');
    header('Cache-Control: private, max-age=0, must-revalidate');
    header('Pragma: public');
    ini_set('zlib.output_compression','0');

    die($content);

Tested and works fine. If you want the file to download instead, replace

经测试,工作正常。如果您希望文件改为下载,请替换

    Content-Disposition: inline

with

    Content-Disposition: attachment

回答by sarnold

You could modify a PDF renderer such as xpdf or evince to render into a graphics image on your server, and then deliver the image to the user. This is how Google's quick viewof PDF files works, they render it locally, then deliver images to the user. No downloaded PDF file, and the source is pretty well obscured. :)

您可以修改诸如 xpdf 或 evince 之类的 PDF 渲染器以在您的服务器上渲染成图形图像,然后将图像交付给用户。这就是 Google快速查看PDF 文件的工作原理,它们在本地呈现它,然后将图像传送给用户。没有下载的 PDF 文件,而且来源很模糊。:)

回答by Pekka

The safest way to have a PDF display instead of download seems to be embedding it using an objector iframeelement. There are also 3rd party solutions like Google's PDF viewer.

显示 PDF 而不是下载的最安全方法似乎是使用objectoriframe元素嵌入它。还有像 Google 的 PDF 查看器这样的 3rd 方解决方案。

See Best Way to Embed PDF in HTMLfor an overview.

有关概述,请参阅在 HTML 中嵌入 PDF 的最佳方法

There's also DoPDF, a Java based In-browser PDF viewer. I can't speak to its quality but it looks interesting.

还有DoPDF,一种基于 Java 的浏览器内 PDF 查看器。我不能说它的质量,但它看起来很有趣。

回答by tapananand

You can also use fpdf class available at: http://www.fpdf.org. It gives options for both outputting to a file and displaying on browser.

您还可以使用以下网址提供的 fpdf 类:http: //www.fpdf.org。它提供了输出到文件和在浏览器上显示的选项。