【发布时间】:2019-03-13 04:04:32
【问题描述】:
这是我用过的代码。
<html>
<body>
<object width="400" height="400" data="pdf/1.pdf" type="application/pdf><embed src="pdf/1.pdf" type="application/pdf" /></object>
</body>
【问题讨论】:
这是我用过的代码。
<html>
<body>
<object width="400" height="400" data="pdf/1.pdf" type="application/pdf><embed src="pdf/1.pdf" type="application/pdf" /></object>
</body>
【问题讨论】:
大多数流行的移动浏览器本身不支持以嵌入模式打开 PDF 文件。而是下载了文件,这就是您所遇到的情况。
一种可能的解决方法是使用 Google 文档查看器。您可以通过将 PDF 的 URL 传递到 Google Docs Viewer 的查询字符串中来做到这一点:
https://docs.google.com/viewerng/viewer?url=http://link-to-your-pdf-file
如果您只想在移动浏览器上执行此操作,您可以添加一段 JavaScript 来检查用户代理字符串:
if (navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
) {
// use the Google Docs Viewer URL
} else {
// use the regular URL
}
另一种方法可能是使用像 PDFJs 这样的 JS 库:https://mozilla.github.io/pdf.js/
【讨论】:
我认为你真的应该考虑在谷歌文档中打开它
<iframe src="http://docs.google.com/gview?url=http://path.com/to/your/pdf.pdf&embedded=true"
style="width:600px; height:500px;" frameborder="0"></iframe>
【讨论】: