pdfView = async (url, opts) => {
const options = Object.assign(
{},
{
startPage: 1,
scale: 1,
backgroundColor: "#f4f4f4",
fontColor: "#6e6e6e"
},
opts
);
const { startPage } = options;
let rootEl;
let prevButton;
let nextButton;
let currentPage = startPage || 1;
let renderWidth;
const uid = initializeStyles(options);
const ns = uid.id;
const pdfDoc = await pdfjsLib.getDocument(url).promise;
const totalPages = pdfDoc.numPages;
const canvas = html`<canvas class="canvas"></canvas>`;
const pagePicker = pagePickerInput(currentPage, totalPages);
function scaleWidth(width) {
return Math.floor(width / dpr);
}
function updateUI(width) {
pagePicker.value = currentPage;
if (rootEl && width) {
rootEl.style.maxWidth = `${scaleWidth(width)}px`;
}
if (currentPage <= 1) {
prevButton[0].disabled = true;
} else {
prevButton[0].disabled = false;
}
if (currentPage >= totalPages) {
nextButton[0].disabled = true;
} else {
nextButton[0].disabled = false;
}
}
async function renderCurrentPage() {
return renderPageToCanvas(canvas, pdfDoc, currentPage, options);
}
async function showPage(num) {
if (num >= 1 && num <= totalPages) {
currentPage = num;
const { width } = await renderCurrentPage();
updateUI(width);
}
}
function showNextPage() {
const nextPage =
currentPage + 1 > totalPages ? currentPage : currentPage + 1;
showPage(nextPage);
}
function showPreviousPage() {
const nextPage = currentPage - 1 < 1 ? currentPage : currentPage - 1;
showPage(nextPage);
}
const pageChangeHander = (e) => {
const target = e.currentTarget;
const nextPage = +target.value;
showPage(nextPage);
};
pagePicker.addEventListener("change", pageChangeHander);
invalidation.then(() =>
pagePicker.removeEventListener("change", pageChangeHander)
);
prevButton = Inputs.button("Previous", {
reduce: () => showPreviousPage()
});
nextButton = Inputs.button("Next", {
reduce: () => showNextPage()
});
const { width } = await renderCurrentPage();
updateUI();
const paginationEl =
totalPages > 1
? html`<div class="${ns}__pagination">
<div class="${ns}__actions">
<div>${prevButton}</div>
<div>${nextButton}</div>
</div>
<div class="${ns}__info">
${pagePicker} of <span>${totalPages}</span>
</div>
</div>`
: null;
rootEl = html`<div class="${ns}" style="max-width: ${scaleWidth(width)}px;">
<div class="${ns}__canvas-wrapper">
${canvas}
</div>
${paginationEl}
</div>`;
return rootEl;
}