Recommended way to embed PDF in HTML?

前端 未结 24 1695
天命终不由人
天命终不由人 2020-11-21 04:54

What is the recommended way to embed PDF in HTML?

  • iFrame?
  • Object?
  • Embed?

What does Adobe say itself about it?

In my

相关标签:
24条回答
  • 2020-11-21 05:39

    FDView combines PDF2SWF (which itself is based on xpdf) with an SWF viewer so you can convert and embed PDF documents on the fly on your server.

    xpdf is not a perfect PDF converter. If you need better results then Ghostview has some ability to convert PDF documents into other formats which you may be able to more easily build a Flash viewer for.

    But for simple PDF documents, FDView should work reasonably well.

    0 讨论(0)
  • 2020-11-21 05:40

    Before I got a problem with embeding base64 encoded with PDF because the URI limitation, so any files over 2MB won't render properly on Chrome.

    My solution is:

    1. Convert uri encoded to Blob:

    2. Generate the temporary DOM String base on Blob.

      const blob = dataURItoBlob(this.dataUrl);

      var temp_url = window.URL.createObjectURL(blob);

    3. Decide where you want to attach the iframe to DOM:

      const target = document.querySelector(targetID);

      target.innerHTML = `<iframe src='${temp_url}' type="application/pdf"></iframe>

    0 讨论(0)
  • 2020-11-21 05:45

    You can also use Google PDF viewer for this purpose. As far as I know it's not an official Google feature (am I wrong on this?), but it works for me very nicely and smoothly. You need to upload your PDF somewhere before and just use its URL:

    <iframe src="http://docs.google.com/gview?url=http://example.com/mypdf.pdf&embedded=true" style="width:718px; height:700px;" frameborder="0"></iframe>
    

    What is important is that it doesn't need a Flash player, it uses JavaScript.

    0 讨论(0)
  • 2020-11-21 05:45

    Have a look for this code- To embed the PDF in HTML

    <!-- Embed PDF File -->
    <object data="YourFile.pdf" type="application/x-pdf" title="SamplePdf" width="500" height="720">
        <a href="YourFile.pdf">shree</a> 
    </object>
    
    0 讨论(0)
  • 2020-11-21 05:45

    Scribd no longer require you to host your documents on their server. If you create an account with them so you get a publisher ID. It only takes a few lines of JavaScript code to load up PDF files stored on your own server.

    For more details, see Developer Tools.

    0 讨论(0)
  • 2020-11-21 05:46

    Our problem is that for legal reasons we are not allowed to temporarily store a PDF on the hard disk. In addition, the entire page should not be reloaded when displaying a PDF as Preview in the Browser.

    First we tried PDF.jS. It worked with Base64 in the viewer for Firefox and Chrome. However, it was unacceptably slow for our PDF. IE/Edge didn't work at all.

    We therefore tried it with a Base64 string in an HTML object tag. This again didn't work for IE/Edge (maybe the same problem as with PDF.js). In Chrome/Firefox/Safari again no problem. That's why we chose a hybrid solution. IE/Edge we use an IFrame and for all other browsers the object-tag.

    The IFrame solution would of course also work for Chrome and co. The reason why we didn't use this solution for Chrome is that although the PDF is displayed correctly, Chrome makes a new request to the server as soon as you click on "download" in the preview. The required hidden-field pdfHelperTransferData (for sending our form data needed for PDF generation) is no longer set because the PDF is displayed in an IFrame. For this feature/bug see Chrome sends two requests when downloading a PDF (and cancels one of them).

    Now the problem children IE9 and IE10 remain. For these we gave up a preview solution and simply send the document by clicking the preview button as a download to the user (instead of the preview). We have tried a lot but even if we had found a solution the extra effort for this tiny part of users would not have been worth the effort. You can find our solution for the download here: Download PDF without refresh with IFrame.

    Our Javascript:

    var transferData = getFormAsJson()
    if (isMicrosoftBrowser()) {
            // Case IE / Edge (because doesn't recoginzie Pdf-Base64 use Iframe)
            var form = document.getElementById('pdf-helper-form');
            $("#pdfHelperTransferData").val(transferData);
            form.target = "iframe-pdf-shower";
            form.action = "serverSideFunctonWhichWritesPdfinResponse";
            form.submit();
     } else {
            // Case non IE use Object tag instead of iframe
            $.ajax({
                url: "serverSideFunctonWhichRetrivesPdfAsBase64",
                type: "post",
                data: { downloadHelperTransferData: transferData },
                success: function (result) {
                    $("#object-pdf-shower").attr("data", result);
                }
            })
     }
    

    Our HTML:

    <div id="pdf-helper-hidden-container" style="display:none">
       <form id="pdf-helper-form" method="post">
            <input type="hidden" name="pdfHelperTransferData" id="pdfHelperTransferData" />
       </form>
    </div>
    
    <div id="pdf-wrapper" class="modal-content">
        <iframe id="iframe-pdf-shower" name="iframe-pdf-shower"></iframe>
        <object id="object-pdf-shower" type="application/pdf"></object>
    </div>
    

    To check the browser type for IE/Edge see here: How can I detect Internet Explorer (IE) and Microsoft Edge using JavaScript? I hope these findings will save someone else the time.

    0 讨论(0)
提交回复
热议问题