Recommended way to embed PDF in HTML?

前端 未结 24 1998
天命终不由人
天命终不由人 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:22

    You do have some control over how the PDF appears in the browser by passing some options in the query string. I was happy to this working, until I realized it does not work in IE8. :(

    It works in Chrome 9 and Firefox 3.6, but in IE8 it shows the message "Insert your error message here, if the PDF cannot be displayed."

    I haven't yet tested older versions of any of the above browsers, though. But here's the code I have anyway in case it helps anyone. This sets the zoom to 85%, removes scrollbars, toolbars and nav panes. I'll update my post if I do come across something that works in IE as well.

    <object width="400" height="500" type="application/pdf" data="/my_pdf.pdf?#zoom=85&scrollbar=0&toolbar=0&navpanes=0">
        <p>Insert your error message here, if the PDF cannot be displayed.</p>
    </object>
    
    0 讨论(0)
  • You can use the relative location of the saved pdf like this:

    Example1

    <embed src="example.pdf" width="1000" height="800" frameborder="0" allowfullscreen>
    

    Example2

    <iframe src="example.pdf" style="width:1000px; height:800px;" frameborder="0" allowfullscreen></iframe>

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

    This is the way I did with AXIOS and Vue.js:

            axios({
                url: `urltoPDFfile.pdf`,
                method: 'GET',
                headers: headers,
                responseType: 'blob'
            })
            .then((response) => {
                this.urlPdf = URL.createObjectURL(response.data)
            })
            .catch((error) => {
                console.log('ERROR   ', error)
            })
    

    add urlPDF dynamically to HTML:

    <object width='100%' height='600px' :data='urlPdf' type='application/pdf'></object>
    
    0 讨论(0)
  • 2020-11-21 05:24

    Convert it to PNG via ImageMagick, and display the PNG (quick and dirty).

    <?php
      $dir = '/absolute/path/to/my/directory/';
      $name = 'myPDF.pdf';
      exec("/bin/convert $dir$name $dir$name.png");
      print '<img src="$dir$name.png" />';
    ?>
    

    This is a good option if you need a quick solution, want to avoid cross-browser PDF viewing problems, and if the PDF is only a page or two. Of course, you need ImageMagick installed (which in turn needs Ghostscript) on your webserver, an option that might not be available in shared hosting environments. There is also a PHP plugin (called imagick) that works like this but it has it's own special requirements.

    0 讨论(0)
  • One of the options you should consider is Notable PDF
    It has a free plan unless you are planning on doing real-time online collaboration on pdfs

    Embed the following iframe to any html and enjoy the results:

    <iframe width='1000' height='800' src='http://bit.ly/1JxrtjR' frameborder='0' allowfullscreen></iframe>
    
    0 讨论(0)
  • 2020-11-21 05:27

    To stream the file to the browser, see Stack Overflow question How to stream a PDF file as binary to the browser using .NET 2.0 - note that, with minor variations, this should work whether you're serving up a file from the file system or dynamically generated.

    With that said, the referenced MSDN article takes a rather simplistic view of the world, so you may want to read Successfully Stream a PDF to browser through HTTPS as well for some of the headers you may need to supply.

    Using that approach, an iframe is probably the best way to go. Have one webform that streams the file, and then put the iframe on another page with its src attribute set to the first form.

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