通过 PHP/Perl 在用户浏览器中显示 PDF 文件 [英] Show a PDF files in users browser via PHP/Perl

查看:42
本文介绍了通过 PHP/Perl 在用户浏览器中显示 PDF 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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,而不是向用户显示文件.

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文件,不创建或下载pdf文件.这是我从官方 php 文档中得到的:

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

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

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

Also my google-search-result perl code:

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

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

";
print $output

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

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

对不起,如果我的代码离显示pdf的方法太远,因为我对pdf处理以及如何实现这个问题一无所知.

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.

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

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

edit :我想显示纯 pdf 文件.我的主要目的:跟踪我的 pdf 文件并使用一些花哨的网址.

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

edit :这是我的主要 php 代码:

edit : Here's my main php code:

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

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

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:

<?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 :我所有的问题都解决了.最终代码如下:

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

谢谢!:)

推荐答案

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

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-Disposition header with a value of inline.

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

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

这篇关于通过 PHP/Perl 在用户浏览器中显示 PDF 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆