📅  最后修改于: 2023-12-03 15:35:36.617000             🧑  作者: Mango
Are you tired of displaying PDFs in a plain and boring manner on your web applications? By using Bootstrap and HTML, you can visually enhance the way PDFs are displayed on your webpage!
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div id="pdf-viewer"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.550/pdf.min.js"></script>
<script>
var pdfPath = '/path/to/pdf/file.pdf'; // Change this to the correct path
pdfjsLib.getDocument(pdfPath).promise.then(function(pdfDoc_) {
pdfDoc = pdfDoc_;
renderPage(pageNum);
});
</script>
function renderPage(num) {
pdfDoc.getPage(num).then(function(page) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var viewport = page.getViewport({scale: 1});
canvas.height = viewport.height;
canvas.width = viewport.width;
document.getElementById('pdf-viewer').appendChild(canvas);
page.render({canvasContext: ctx, viewport: viewport});
});
}
var pdfDoc = null;
var pageNum = 1;
document.getElementById('prev-page').addEventListener('click', function() {
if(pageNum <= 1)
return;
pageNum--;
document.getElementById('pdf-viewer').innerHTML = '';
renderPage(pageNum);
});
document.getElementById('next-page').addEventListener('click', function() {
if(pageNum >= pdfDoc.numPages)
return;
pageNum++;
document.getElementById('pdf-viewer').innerHTML = '';
renderPage(pageNum);
});
By using Bootstrap and HTML, you can improve the way PDF files are displayed on your web page. This not only enhances the visual aspect, but also improves the user experience.
Give it a try and let us know how it works for you!