如何在 <iframe> 中打开 PDF 文件? [英] How to open a PDF file in an <iframe>?

查看:34
本文介绍了如何在 <iframe> 中打开 PDF 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 iframe 中打开 pdf 文件.我正在使用以下代码:

I want to open the pdf file in an iframe. I am using following code:

<a class="iframeLink" href="https://something.com/HTC_One_XL_User_Guide.pdf"> User guide </a>

在 Firefox 中可以正常打开,但在 IE8 中无法打开.

It is opening fine in Firefox, but it is not opening in IE8.

有谁知道如何使它也适用于 IE 吗?

Does anyone know how to make it work also for IE ?

推荐答案

使用 iframe 来渲染"PDF 不适用于所有浏览器;这取决于浏览器如何处理 PDF 文件.某些浏览器(例如 Firefox 和 Chrome)具有内置的 PDF 渲染功能,允许它们内嵌显示 PDF,而某些较旧的浏览器(可能是较旧版本的 IE 会尝试下载文件).

Using an iframe to "render" a PDF will not work on all browsers; it depends on how the browser handles PDF files. Some browsers (such as Firefox and Chrome) have a built-in PDF rendered which allows them to display the PDF inline where as some older browsers (perhaps older versions of IE attempt to download the file instead).

相反,我建议查看 PDFObject,这是一个将 PDF 嵌入 HTML 文件的 Javascript 库.它可以很好地处理浏览器兼容性,并且很可能在 IE8 上运行.

Instead, I recommend checking out PDFObject which is a Javascript library to embed PDFs in HTML files. It handles browser compatibility pretty well and will most likely work on IE8.

在你的 HTML 中,你可以设置一个 div 来显示 PDF:

In your HTML, you could set up a div to display the PDFs:

<div id="pdfRenderer"></div>

然后,您可以使用 Javascript 代码在该 div 中嵌入 PDF:

Then, you can have Javascript code to embed a PDF in that div:

var pdf = new PDFObject({
  url: "https://something.com/HTC_One_XL_User_Guide.pdf",
  id: "pdfRendered",
  pdfOpenParams: {
    view: "FitH"
  }
}).embed("pdfRenderer");

这篇关于如何在 &lt;iframe&gt; 中打开 PDF 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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