【问题标题】:Want to embed a pdf in my html webpage. It is showing perfectly in laptop's browser but in mobile's browser it is downloading instead of opening想在我的 html 网页中嵌入 pdf。它在笔记本电脑的浏览器中完美显示,但在移动浏览器中它正在下载而不是打开
【发布时间】: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 pdf


    【解决方案1】:

    大多数流行的移动浏览器本身不支持以嵌入模式打开 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/

    【讨论】:

      【解决方案2】:

      我认为你真的应该考虑在谷歌文档中打开它

      <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>
      

      【讨论】:

        猜你喜欢
        • 2012-08-18
        • 2017-10-28
        • 2013-10-25
        • 2022-11-17
        • 2020-11-03
        • 2020-01-20
        相关资源
        最近更新 更多