查看 HTML 格式的 PDF 文件而不是下载它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9530249/
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
View a PDF file in HTML not to download it
提问by woninana
I have a very simple pure html file in which I have many PDF files. I have link it like this:
我有一个非常简单的纯 html 文件,其中有很多 PDF 文件。我有这样的链接:
<a href="pdffile.pdf"> PDF 1 here </a>
When I click the link, the PDF file is downaloaded and viewed in the native Reader program. I want it to open in another window of the browser, and read it there, rather then saving a copy manually to my computer and opening it.
当我单击链接时,PDF 文件被下载并在本机阅读器程序中查看。我希望它在浏览器的另一个窗口中打开,并在那里阅读,而不是手动将副本保存到我的计算机并打开它。
采纳答案by Aziz
2018 Update
2018 更新
Almost all modern browsers have built-in PDF viewers. You can directly link to the PDF file and the browser will view it. You can Also use an <iframe>
if you want to view it inside an HTML page (e.g. with your website headers, etc.).
几乎所有现代浏览器都有内置的 PDF 查看器。您可以直接链接到 PDF 文件,浏览器将查看它。<iframe>
如果您想在 HTML 页面中查看它(例如,使用您的网站标题等),您也可以使用。
Another approach, but more complicated and not necessary except for very special circumstances, is to convert the PDF files to HTML (as described in #1 of the 2012 answer below).
另一种方法是将 PDF 文件转换为 HTML(如下面 2012 答案的 #1 所述),但更复杂且不需要,除非是非常特殊的情况。
Original Answer (Outdated, from 2012)
原始答案(已过时,从 2012 年开始)
Viewing the PDF file in the browser (without download) requires an add-on to the client's browser. Google Chrome, for example, has a built-in PDF viewer and can open files directly, while IE and Firefox require that you install a plug-in (the one that comes with Adobe Reader).
在浏览器中查看 PDF 文件(无需下载)需要客户端浏览器的附加组件。例如,谷歌浏览器内置了 PDF 查看器,可以直接打开文件,而 IE 和 Firefox 则要求您安装插件(Adobe Reader 附带的插件)。
There are two other approaches:
还有另外两种方法:
Convert the file to HTML, image, or any other format that can be directly viewed in the browser. This conversion can be on-the-fly using a server-side (written in PHP, Python, ASP, whatever language), or you can just pre-convert all files to a readable one.
The other approach, which is the best, is to use a Flash-based PDF viewer (such as http://flexpaper.devaldi.com/). This is easy, flexible and doesn't require writing server-side code. This approach is used by many Document-sharing sites (e.g. http://www.scribd.com/, http://www.slideshare.net/, http://www.docstoc.com/)
将文件转换为 HTML、图像或任何其他可以在浏览器中直接查看的格式。这种转换可以使用服务器端(用 PHP、Python、ASP 或任何语言编写)即时进行,或者您可以将所有文件预先转换为可读文件。
另一种最好的方法是使用基于 Flash 的 PDF 查看器(例如http://flexpaper.devaldi.com/)。这很容易、灵活并且不需要编写服务器端代码。许多文档共享站点(例如http://www.scribd.com/、http://www.slideshare.net/、http://www.docstoc.com/)都使用这种方法
回答by Bert
i use this
我用这个
for the HTML
对于 HTML
<a href="view.php?download_file=MIKOGO Remote Support.pdf"><img src="images/view.png" alt=" " border="0"/></a>
and the view.php file for viewing it through PDF.
以及用于通过 PDF 查看的 view.php 文件。
<?php
$path = $_SERVER['DOCUMENT_ROOT']."/demo/documents/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: inline; filename=\"".$path_parts["basename"]."\"");
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
?>
Make sure the your browser has PDF add-on on it.
确保您的浏览器上有 PDF 插件。
回答by VishnuMoorthy
回答by Oleksi
Your browser needs a PDF reader plug in to read PDFs in browser. A quick google search should provide you with one for whatever browser you are using.
您的浏览器需要一个 PDF 阅读器插件才能在浏览器中阅读 PDF。一个快速的谷歌搜索应该为你提供一个你正在使用的浏览器。
回答by C?lin Darie
If the file is not cached, it has to be downloaded. That is, if you grab it using HTTP. If it's on your local filesystem, you could use the file URI scheme.
如果文件未缓存,则必须下载。也就是说,如果您使用 HTTP 抓取它。如果它在您的本地文件系统上,您可以使用文件 URI scheme。
回答by Librarian
right click Open with Choose default programme Select Adobe Reader OK If you want open chrome any other app Same steps Last Step Select Chrome
右键单击打开 选择默认程序 选择 Adobe Reader 确定 如果您想打开 chrome 任何其他应用程序 相同的步骤 最后一步 选择 Chrome