Display PDF file in the body of the HTML page

Asked

Viewed 68,478 times

12

I am creating an HTML page and would like to know if it is possible to display a PDF file in the body of the page, so that it can be viewed without having to open the file or download.

Or I will have to save the PDF as an image and display it on the page?

If possible, I would like a solution suggestion that works on any browser

  • View on an html page ? you can put inside a <iframe></iframe> but if you’re just talking about viewing the pdf in a browser use firefox it has native support that allows you to open pdf in the browser...

  • I am creating an HTML page and wanted to know if it is possible to put a PDF to be viewed on the page. If you have to open and without downloading. It has to work in any browser

  • I understand from a glance at this link that he has the solution for you: http://answall.com/questions/10505/view%C3%A7%C3%A3o-de-pdf-in-iframe

  • 1

    What would be the browser compatibility requirement? I know the pdf.js, very complete and is from Mozilla itself. The only problem is that it does not run well on IE9 or smaller.

3 answers

13

The use of <iframe> brings some incompatibilities, especially if the browser does not have the plugin, sometimes the file downloads the PDF instead of opening or appearing some error message. Other than the use of <table> to organize the layout is not a good, prefer float or even grids (or grids of some "CSS framework").

One can use <object> (so if you don’t have support for a viewer you will send some message from the browser itself saying that it is not supported):

<object data="meuarquivo.pdf" type="application/pdf">
    <p>Seu navegador não tem um plugin pra PDF</p>
</object>

Or use the PDF.js (Github) you will not need plugins.

How to start: https://mozilla.github.io/pdf.js/getting_started/

To facilitate you can opt for Cdns:

Frequently Asked Questions about PDF.js: https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions

var url = 'https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf';

var pdfjsLib = window['pdfjs-dist/build/pdf'];

pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';

var pdfDoc = null,
    pageNum = 1,
    pageRendering = false,
    pageNumPending = null,
    scale = 0.8,
    canvas = document.getElementById('pdf-exemplo'),
    ctx = canvas.getContext('2d');


function renderPage(num) {
  pageRendering = true;

  pdfDoc.getPage(num).then(function(page) {
    var viewport = page.getViewport({scale: scale});
    canvas.height = viewport.height;
    canvas.width = viewport.width;

    var renderContext = {
      canvasContext: ctx,
      viewport: viewport
    };
    var renderTask = page.render(renderContext);

    renderTask.promise.then(function() {
      pageRendering = false;
      if (pageNumPending !== null) {
        renderPage(pageNumPending);
        pageNumPending = null;
      }
    });
  });

  document.getElementById('page_num').textContent = num;
}


function queueRenderPage(num) {
  if (pageRendering) {
    pageNumPending = num;
  } else {
    renderPage(num);
  }
}

/**
 * mostra a página anterior
 */
function onPrevPage() {
  if (pageNum <= 1) {
    return;
  }
  pageNum--;
  queueRenderPage(pageNum);
}
document.getElementById('prev').addEventListener('click', onPrevPage);

/**
 * mostra a proxima página
 */
function onNextPage() {
  if (pageNum >= pdfDoc.numPages) {
    return;
  }
  pageNum++;
  queueRenderPage(pageNum);
}
document.getElementById('next').addEventListener('click', onNextPage);

/**
 * Download assincrono do PDF.
 */
pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) {
  pdfDoc = pdfDoc_;
  document.getElementById('page_count').textContent = pdfDoc.numPages;

  renderPage(pageNum);
});
#pdf-exemplo {
    border: 1px solid black;
}
<script src="//mozilla.github.io/pdf.js/build/pdf.js"></script>

<div>
  <button id="prev">Proxima página</button>
  <button id="next">Página anterior</button>
  &nbsp; &nbsp;
  <span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
</div>

<canvas id="pdf-exemplo"></canvas>

  • I’m trying to see if stackoverflow has some PDF in some corner, it would be a good example in snippet

  • @Bacco thought about it, but I think the sandbox limits on something, then I’ll edit

  • I guess it wouldn’t work because of the domain, on second thought, the sandbox is in another one, right? (then check). Regardless, I understand that your answer is more suitable for general use than it currently accepts.

  • @Bacco is not iframe that has an attribute called sandbox and prevents some features, I think only reading the pdf should be blocked, maybe put a hello world with data Uri Scheme works. Although http://www.w3schools.com/tags/att_iframe_sandbox.asp is a bad referencing site, there is a brief explanation of the limitation, then I will try to add a functional example :)

  • But it is not "pdf" that you are reading. Note that the one who renders the PDF is the PDF.js, the file is a mere stream of bytes. What the sandbox will "squeak" right away is you want to take it from another domain.

  • @Bacco is likely to be right, but I did some answers in the past that accepted external documents, only canvas.todataurl caused Taint. I’ll call you back tonight, thanks man

Show 1 more comment

8


Put a iframe with the PDF file link:

<iframe src="http://devsa.info/teste.pdf" width="600" height="780" style="border: none;"></iframe>

UPDATE (2 Iframes):

<table cellpadding="0" cellspacing="0" align="center" width="100%" height="100%">
<tr>
<td><iframe src="http://devsa.info/teste.pdf" width="600" height="780" style="border: none;"></iframe></td>
<td><iframe src="http://devsa.info/teste.pdf" width="600" height="780" style="border: none;"></iframe></td>
</tr>
  • It is possible to use two on the same page?

  • Of course, just put another iframe, with the path of the other file...

  • I put two and I don’t see the second one. I put each of the Iframes inside <div> and it doesn’t appear..

  • Look again at the link of my reply and the update of it. I just created a table and put the iframes inside.

1

The use of iframe may bring some unwanted results such as appearing only this fragment in search results of searchers. In the method below, the PDF is directly embedded in the document. This method has shown a good functioning in all browsers:

<embed src="http://www.meudominio.com.br//meu_documento.pdf" width="760" height="500" type='application/pdf'>

Example: http://www.runtal.com.br/pg_00992.php

  • The correct syntax would be: <embed src="http://www.meudominio.com.br/meu_documento.pdf" width="760" height="500" type='application/pdf'>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.