const url = "/dummy.pdf";
(async function () {
pdfjsLib.GlobalWorkerOptions.workerSrc = "//mozilla.github.io/pdf.js/build/pdf.worker.js";
const doc = await pdfjsLib.getDocument(url).promise;
const minPage = 1;
const maxPage = doc._pdfInfo.numPages;
let currentPage = 1;
await getPage(doc, currentPage);
document.getElementById("pageNumber").innerHTML = `Page ${currentPage} of ${maxPage}`;
document.getElementById("previous").addEventListener("click", async () => {
if (currentPage > minPage) {
await getPage(doc, currentPage--);
document.getElementById("pageNumber").innerHTML = `Page ${currentPage} of ${maxPage}`;
}
});
document.getElementById("next").addEventListener("click", async () => {
if (currentPage < maxPage) {
await getPage(doc, currentPage++);
document.getElementById("pageNumber").innerHTML = `Page ${currentPage} of ${maxPage}`;
}
});
})();
async function getPage(doc, pageNumber) {
if (pageNumber >= 1 && pageNumber <= doc._pdfInfo.numPages) {
const page = await doc.getPage(pageNumber);
const viewport = page.getViewport({ scale: 1.5 });
const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
canvas.height = viewport.height;
canvas.width = viewport.width;
return await page.render({
canvasContext: context,
viewport: viewport
}).promise;
} else {
console.log("Please specify a valid page number");
}
}