mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-11 05:32:39 +01:00
a87ae37ce1
* Wip for testing * Rendering PDFs using pdf.js
79 lines
1.8 KiB
JavaScript
Vendored
79 lines
1.8 KiB
JavaScript
Vendored
/**
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
*
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
*
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
*
|
|
* @license https://opensource.org/licenses/AAL
|
|
*/
|
|
|
|
class PDF {
|
|
constructor(url, canvas) {
|
|
this.url = url;
|
|
this.canvas = canvas;
|
|
this.context = canvas.getContext("2d");
|
|
this.currentPage = 1;
|
|
this.maxPages = 1;
|
|
}
|
|
|
|
handlePreviousPage() {
|
|
if (this.currentPage == 1) {
|
|
return;
|
|
}
|
|
|
|
this.currentPage -= 1;
|
|
|
|
this.handle();
|
|
}
|
|
|
|
handleNextPage() {
|
|
if (this.currentPage == this.maxPages) {
|
|
return;
|
|
}
|
|
|
|
this.currentPage += 1;
|
|
|
|
this.handle();
|
|
}
|
|
|
|
prepare() {
|
|
let previousPageButton = document.getElementById(
|
|
"previous-page-button"
|
|
);
|
|
|
|
let nextPageButton = document.getElementById("next-page-button");
|
|
|
|
previousPageButton.addEventListener("click", () =>
|
|
this.handlePreviousPage()
|
|
);
|
|
|
|
nextPageButton.addEventListener("click", () => this.handleNextPage());
|
|
|
|
return this;
|
|
}
|
|
|
|
async handle() {
|
|
let pdf = await pdfjsLib.getDocument(this.url).promise;
|
|
|
|
let page = await pdf.getPage(this.currentPage);
|
|
|
|
this.maxPages = pdf.numPages;
|
|
|
|
let viewport = await page.getViewport({ scale: 1 });
|
|
|
|
this.canvas.height = viewport.height;
|
|
this.canvas.width = viewport.width;
|
|
|
|
page.render({
|
|
canvasContext: this.context,
|
|
viewport
|
|
});
|
|
}
|
|
}
|
|
|
|
const url = document.querySelector("meta[name='pdf-url'").content;
|
|
const canvas = document.getElementById("pdf-placeholder");
|
|
|
|
new PDF(url, canvas).prepare().handle();
|