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>
<span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
</div>
<canvas id="pdf-exemplo"></canvas>
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...– Gabriel Rodrigues
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
– ChrisAdler
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
– Gabriel Rodrigues
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.– Wakim