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

查看:64
本文介绍了通过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.

我一直在Internet上搜索,只发现了如何向用户显示保存对话框并创建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搜索结果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\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.

很抱歉,如果我的代码与显示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.

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

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

编辑:我想显示纯pdf文件.我的主要目的:跟踪我的pdf文件并使用一些精美的网址.

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

编辑:这是我的主要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);
?>

编辑:现在,代码可以正常工作了.但是,加载进度栏(在Adobe 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 : 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文件或在其他应用程序(例如Adobe 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天全站免登陆