【问题标题】:How to display PDF file in HTML?如何以 HTML 格式显示 PDF 文件?
【发布时间】:2013-07-20 23:29:57
【问题描述】:

我有一个由 itext 自动生成的 PDF 文件,我需要以 HTML 格式显示该 PDF 文件。我的问题是:如何使用 pdf.js 在 HTML 中显示本地 PDF 文件?该 PDF 文件是否应该按某些标准生成?

【问题讨论】:

标签: html pdf


【解决方案1】:

在您的 HTML 网页中实现 PDF 文件非常容易。

<embed src="file_name.pdf" width="800px" height="2100px" />

确保根据需要更改宽度和高度。 祝你好运!

【讨论】:

  • &lt;iframe src="file_name.pdf" style="width: 100%;height: 100%;border: none;"&gt;&lt;/iframe&gt;
  • 它正在下载文件而不是显示该文件
  • 在chrome浏览器上显示移动设备不支持该插件。
  • @SumanBogati 链接失效,新链接:jsgyan.blogspot.com/2017/12/…
  • 2021 年 CanIUse 在桌面和移动设备上显示 great support
【解决方案2】:

我使用 Google Docs 可嵌入的 PDF 查看器。这些文档不必上传到 Google Docs,但必须在线提供。

<iframe src="https://docs.google.com/gview?url=https://path.com/to/your/pdf.pdf&embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe>

【讨论】:

  • 这里是链接:Embeddable Google Docs
  • 您需要将 iframe 与 google 文档查看器一起使用,否则您可能会收到“应用程序防火墙检测到错误”消息,具体取决于您的主机。
  • 请注意,此方法仅支持最大 25mb 的 PDF
  • 如果您想在 google 驱动器中嵌入 pdf 文件,请进一步评论。点击进入pdf后,找到“Embed item”,它会生成iframe HTML。
  • 这里有没有人提到它有什么样的浏览器支持?
【解决方案3】:

1.浏览器原生 HTML 内联嵌入:

<embed
    src="http://infolab.stanford.edu/pub/papers/google.pdf#toolbar=0&navpanes=0&scrollbar=0"
    type="application/pdf"
    frameBorder="0"
    scrolling="auto"
    height="100%"
    width="100%"
></embed>
<iframe
    src="http://infolab.stanford.edu/pub/papers/google.pdf#toolbar=0&navpanes=0&scrollbar=0"
    frameBorder="0"
    scrolling="auto"
    height="100%"
    width="100%"
></iframe>

专业版:

  • 没有 PDF 文件大小限制(甚至数百 MB)
  • 这是最快的解决方案

缺点:

  • 它不适用于移动浏览器

2。 Google 文档查看器:

<iframe
    src="https://drive.google.com/viewerng/viewer?embedded=true&url=http://infolab.stanford.edu/pub/papers/google.pdf#toolbar=0&scrollbar=0"
    frameBorder="0"
    scrolling="auto"
    height="100%"
    width="100%"
></iframe>

专业版:

  • 适用于桌面和移动浏览器

缺点:

  • 25MB 文件限制
  • 需要额外的时间来下载查看器

3.其他嵌入 PDF 的解决方案:


重要提示:

请检查 X-Frame-Options HTTP 响应标头。 应该是 SAMEORIGIN。

X-Frame-Options SAMEORIGIN;

【讨论】:

    【解决方案4】:

    如果你想使用pdf.js,我建议你阅读THIS

    您还可以将您的 pdf 文件上传到某处(例如 Google Drive)并在 iframe 中使用其网址

    <object data="data/test.pdf" type="application/pdf" width="300" height="200">
    <a href="data/test.pdf">test.pdf</a>
    </object>
    

    【讨论】:

      【解决方案5】:

      你可以在这样的html页面中轻松显示

      &lt;embed src="path_of_your_pdf/your_pdf_file.pdf" type="application/pdf"   height="700px" width="500"&gt;

      【讨论】:

        【解决方案6】:

        在电脑的html页面中很容易实现

        <embed src="study/sample.pdf" type="application/pdf"   height="300px" width="100%">
        

        但无法通过此代码在手机中显示 pdf,您必须需要一个插件

        如果您没有响应您的网站。然后上面的代码pdf不会显示在移动设备中,但您可以在代码后放置下载选项

        <embed src="study/sample.pdf" type="application/pdf"   height="300px" width="100%" class="responsive">
        <a href="study/sample.pdf">download</a>
        

        【讨论】:

        • 在 chrome 上可以,但在 IE 上不行。 IE上怎么办
        【解决方案7】:

        便携式文档格式 (PDF)。

        • 任何浏览器 « 使用 _Embeddable Google Document Viewer 将 PDF 文件嵌入到 iframe

          <iframe src="http://docs.google.com/gview?
              url=http://infolab.stanford.edu/pub/papers/google.pdf&embedded=true"
              style="width:600px; height:500px;" frameborder="0">
          </iframe>
          
        • 仅适用于 chrome 浏览器 « 使用插件的 Chrome PDF 查看器。 pluginspage=http://www.adobe.com/products/acrobat/readstep2.html

          <embed type="application/pdf" 
          src="http://www.oracle.com/events/global/en/java-outreach/resources/java-a-beginners-guide-1720064.pdf" 
          width="100%" height="500" alt="pdf" pluginspage="http://www.adobe.com/products/acrobat/readstep2.html" 
          background-color="0xFF525659" top-toolbar-height="56" full-frame="" internalinstanceid="21" 
          title="CHROME">
          

        示例 Sippet:

        <html>
           <head></head>
           <body style=" height: 100%;">
              <div style=" position: relative;">
              <div style="width: 100%; /*overflow: auto;*/ position: relative;height: auto; margin-top: 70px;">
                 <p>An 
                    <a href="https://en.wikipedia.org/wiki/Image_file_formats" >image</a> is an artifact that depicts visual perception
                 </p>
                 <!-- To make div with scroll data [max-height: 500;]-->
                 <div style="/* overflow: scroll; */ max-height: 500; float: left; width: 49%; height: 100%; ">
                    <img width="" height="400" src="https://peach.blender.org/wp-content/uploads/poster_bunny_bunnysize.jpg?x11217" title="Google" style="-webkit-user-select: none;background-position: 0px 0px, 10px 10px;background-size: 20px 20px;background-image:linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee 100%),linear-gradient(45deg, #eee 25%, white 25%, white 75%, #eee 75%, #eee 100%);cursor: zoom-in;" />
                    <p>Streaming an Image form Response Stream (binary data) «  This buffers the output in smaller chunks of data rather than sending the entire image as a single block. 
                       <a href="http://www.chestysoft.com/imagefile/streaming.asp" >StreamToBrowser</a>
                    </p>
                 </div>
                 <div style="float: left; width: 10%; background-color: red;"></div>
                 <div style="float: left;width: 49%; ">
                    <img width="" height="400" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot"/>
                    <p>Streaming an Image form Base64 String « embedding images directly into your HTML.
                       <a href="https://en.wikipedia.org/wiki/Data_URI_scheme">
                       <sup>Data URI scheme</sup>
                       </a>
                       <a href="https://codebeautify.org/image-to-base64-converter">
                       <sup>, Convert Your Image to Base64</sup>
                       </a>
                    <pre>data:[&lt;media type&gt;][;base64],&lt;data&gt;</pre>
                    </p>
                 </div>
              </div>
              <div style="width: 100%;overflow: auto;position: relative;height: auto; margin-top: 70px;">
              <video style="height: 500px;width: 100%;" name="media" controls="controls">
                 <!-- autoplay -->
                 <source src="http://download.blender.org/peach/trailer/trailer_400p.ogg" type="video/mp4">
                 <source src="http://download.blender.org/peach/trailer/trailer_400p.ogg" type="video/ogg">
              </video>
              <p>Video courtesy of 
                 <a href="https://www.bigbuckbunny.org/" >Big Buck Bunny</a>.
              </p>
              <div>
                 <div style="width: 100%;overflow: auto;position: relative;height: auto; margin-top: 70px;">
                    <p>Portable Document Format 
                       <a href="https://acrobat.adobe.com/us/en/acrobat/about-adobe-pdf.html?promoid=CW7625ZK&mv=other" >(PDF)</a>.
                    </p>
                    <div style="float: left;width: 49%; overflow: auto;position: relative;height: auto;">
                       <embed type="application/pdf" src="http://www.oracle.com/events/global/en/java-outreach/resources/java-a-beginners-guide-1720064.pdf" width="100%" height="500" alt="pdf" pluginspage="http://www.adobe.com/products/acrobat/readstep2.html" background-color="0xFF525659" top-toolbar-height="56" full-frame="" internalinstanceid="21" title="CHROME">
                       <p>Chrome PDF viewer 
                          <a href="https://productforums.google.com/forum/#!topic/chrome/MP_1qzVgemo">
                          <sup>extension</sup>
                          </a>
                          <a href="https://chrome.google.com/webstore/detail/surfingkeys/gfbliohnnapiefjpjlpjnehglfpaknnc">
                          <sup> (surfingkeys)</sup>
                          </a>
                       </p>
                    </div>
                    <div style="float: left; width: 10%; background-color: red;"></div>
                    <div style="float: left;width: 49%; ">
                       <iframe src="https://docs.google.com/gview?url=http://infolab.stanford.edu/pub/papers/google.pdf&embedded=true#:page.7" style="" width="100%" height="500px" allowfullscreen="" webkitallowfullscreen=""></iframe>
                       <p>Embeddable 
                          <a href="https://googlesystem.blogspot.in/2009/09/embeddable-google-document-viewer.html" >Google</a> Document Viewer. Here's the code I used to embed the PDF file: 
        <pre>
        &lt;iframe 
        src="http://docs.google.com/gview?
        url=http://infolab.stanford.edu/pub/papers/google.pdf&embedded=true" 
        style="width:600px; height:500px;" frameborder="0"&gt;&lt;/iframe&gt;
        </pre>
                       </p>
                    </div>
                 </div>
              </div>
           </body>
        </html>

        【讨论】:

          【解决方案8】:

          最简单的方法就是使用,

          <iframe src="pdf-link">
          </iframe>
          

          如果它仍然下载而不是查看,请检查服务器响应标头,它应该有 Content-Disposition:Inline 而不是 Content-Disposition:Attachment

          【讨论】:

            【解决方案9】:

            所有浏览器都支持该元素,并在 HTML 文档中定义一个嵌入对象。

            底线:OBJECT 是 Good,EMBED 是 Old。除了 IE 的 PARAM 标签之外,如果浏览器不支持 OBJECT 的引用插件,则 OBJECT 标签之间的任何内容都会被渲染,显然,无论是否渲染,内容都会被请求 http。 Reference

            工作代码:https://www.w3schools.com/code/tryit.asp?filename=G7L8BK6XC0A6

            <!DOCTYPE html>
            <html>
            <body>
            
            <object width="400px" height="400px" data="https://s3.amazonaws.com/dq-blog-files/pandas-cheat-sheet.pdf"></object>
            </body>
            </html>

            【讨论】:

              【解决方案10】:

              我以前也有过类似的,用的很正常的标签

              <a href="path_of_your_pdf/your_pdf_file.pdf" tabindex="-1"><strong>click here</strong></a>
              

              但是发现上面的一些其他方法很有趣!

              【讨论】:

                【解决方案11】:

                这个方法也很有用:

                桌面移动浏览器上显示pdf文件:

                <object data="./filename.pdf" type="application/pdf" width="500px" height="600px">
                  <p>Your web browser doesn't have a PDF plugin.
                   <a href="./filename.pdf">click here to download the PDF file.</a></p>
                </object>

                【讨论】:

                  【解决方案12】:

                  你可以使用

                  <iframe src="your_pdf_file_path" height="100%" width="100%" scrolling="auto"></iframe>
                  

                  或者,如果你想让它占据整个页面:

                  <a href="your_pdf_file_path">Link</a>
                  

                  【讨论】:

                    【解决方案13】:

                    我了解您想使用 HTMl 显示,但您也可以使用 php 通过指出路径打开 PDF 文件,浏览器将通过几个简单的步骤呈现它

                    <?php
                    $your_file_name = "url_here";
                    
                    //Content type and this case its a PDF
                    header("Content-type: application/pdf");
                    
                    header("Content-Length: " . filesize($your_file_name )); 
                    
                    //Display the file
                    readfile($your_file_name );
                    ?>
                    

                    【讨论】:

                      【解决方案14】:

                      更新 - Adob​​e PDF Embed API

                      Adobe 发布了完全免费的Adobe PDF Embed API。因为他们自己创建了 PDF 格式,所以他们的 API 可能是最适合这个的。

                      • 它提供了最高质量的 PDF 渲染。
                      • 您可以完全自定义用户体验并选择如何显示 PDF。
                      • 您还将获得有关 PDF 使用情况的分析,以便了解用户如何与 PDF 交互,包括在页面上花费的时间和搜索时间。

                      您所要做的就是创建一个api_key 并在代码 sn-p 中使用它。

                      将 PDF 显示为缓冲区(例如本地文件)

                      这是代码 sn-p 的示例,您可以将其添加到 HTML 中,如果您有缓冲区(例如本地文件),则可以利用它们的 API 来显示 PDF。

                      <div id="adobe-dc-view"></div>
                      
                      <script src="https://documentcloud.adobe.com/view-sdk/main.js"></script>
                      
                      <script type="text/javascript">
                        document.addEventListener("adobe_dc_view_sdk.ready", function(){
                          var adobeDCView = new AdobeDC.View({clientId: "api_key", divId: "adobe-dc-view"});
                          adobeDCView.previewFile({
                            content: { promise: <FILE_PROMISE> }
                            metaData: { fileName: "file_name_to_display" }
                          }, {});
                        });
                      </script>
                      

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2016-02-08
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多