js tutorial pdf code example
Example 1: PDF.js Tutorial for beginners
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");
}
}
Example 2: javascript tutorial pdf
There is one really good pdf tutorial for javascript that I like.
Here's the link: https://goalkicker.com/JavaScriptBook