2020-04-03 22:12:12 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-13 09:22:36 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-04-03 22:12:12 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
class PDF {
|
|
|
|
constructor(url, canvas) {
|
|
|
|
this.url = url;
|
|
|
|
this.canvas = canvas;
|
2020-06-17 16:52:39 +02:00
|
|
|
this.context = canvas.getContext('2d');
|
2020-04-03 22:12:12 +02:00
|
|
|
this.currentPage = 1;
|
|
|
|
this.maxPages = 1;
|
2021-11-02 19:11:40 +01:00
|
|
|
this.currentScale = 1.25;
|
2020-06-17 16:52:39 +02:00
|
|
|
this.currentScaleText = document.getElementById('zoom-level');
|
2020-07-29 16:36:27 +02:00
|
|
|
|
|
|
|
if (matchMedia('only screen and (max-width: 480px)').matches) {
|
2021-11-02 19:11:40 +01:00
|
|
|
this.currentScale = 1.25;
|
2020-07-29 16:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.currentScaleText.textContent = this.currentScale * 100 + '%';
|
2020-04-03 22:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePreviousPage() {
|
|
|
|
if (this.currentPage == 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentPage -= 1;
|
|
|
|
|
|
|
|
this.handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleNextPage() {
|
|
|
|
if (this.currentPage == this.maxPages) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentPage += 1;
|
|
|
|
|
|
|
|
this.handle();
|
|
|
|
}
|
|
|
|
|
2020-06-17 16:52:39 +02:00
|
|
|
handleZoomChange(zoom = null) {
|
|
|
|
if (this.currentScale == 0.25 && !zoom) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zoom) {
|
|
|
|
this.currentScale += 0.25;
|
|
|
|
this.currentScaleText.textContent = this.currentScale * 100 + '%';
|
|
|
|
|
|
|
|
return this.handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentScale -= 0.25;
|
|
|
|
this.currentScaleText.textContent = this.currentScale * 100 + '%';
|
|
|
|
|
|
|
|
return this.handle();
|
|
|
|
}
|
|
|
|
|
2020-04-03 22:12:12 +02:00
|
|
|
prepare() {
|
2020-06-17 16:52:39 +02:00
|
|
|
let previousPageButton = document
|
|
|
|
.getElementById('previous-page-button')
|
|
|
|
.addEventListener('click', () => this.handlePreviousPage());
|
2020-04-03 22:12:12 +02:00
|
|
|
|
2020-06-17 16:52:39 +02:00
|
|
|
let nextPageButton = document
|
|
|
|
.getElementById('next-page-button')
|
|
|
|
.addEventListener('click', () => this.handleNextPage());
|
2020-04-03 22:12:12 +02:00
|
|
|
|
2020-06-17 16:52:39 +02:00
|
|
|
let zoomInButton = document
|
|
|
|
.getElementById('zoom-in')
|
|
|
|
.addEventListener('click', () => this.handleZoomChange(true));
|
2020-04-03 22:12:12 +02:00
|
|
|
|
2020-06-17 16:52:39 +02:00
|
|
|
let zoomOutButton = document
|
|
|
|
.getElementById('zoom-out')
|
|
|
|
.addEventListener('click', () => this.handleZoomChange());
|
2020-04-03 22:12:12 +02:00
|
|
|
|
2021-09-20 17:33:45 +02:00
|
|
|
document
|
|
|
|
.querySelector('meta[name=pdf-url]')
|
|
|
|
.addEventListener('change', () => {
|
|
|
|
this.canvas.getContext('2d').clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
|
|
this.url = document.querySelector("meta[name='pdf-url']").content;
|
|
|
|
|
|
|
|
this.handle();
|
|
|
|
})
|
|
|
|
|
2020-04-03 22:12:12 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-04-16 23:18:07 +02:00
|
|
|
setPagesInViewport() {
|
|
|
|
let currentPageContainer = document.getElementById(
|
2020-06-17 16:52:39 +02:00
|
|
|
'current-page-container'
|
2020-04-16 23:18:07 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let totalPageContainer = document.getElementById(
|
2020-06-17 16:52:39 +02:00
|
|
|
'total-page-container'
|
2020-04-16 23:18:07 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let paginationButtonContainer = document.getElementById(
|
2020-06-17 16:52:39 +02:00
|
|
|
'pagination-button-container'
|
2020-04-16 23:18:07 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
currentPageContainer.innerText = this.currentPage;
|
|
|
|
totalPageContainer.innerText = this.maxPages;
|
|
|
|
|
|
|
|
if (this.maxPages > 1) {
|
2020-06-17 16:52:39 +02:00
|
|
|
paginationButtonContainer.style.display = 'flex';
|
2020-04-16 23:18:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 22:12:12 +02:00
|
|
|
async handle() {
|
|
|
|
let pdf = await pdfjsLib.getDocument(this.url).promise;
|
|
|
|
|
|
|
|
let page = await pdf.getPage(this.currentPage);
|
|
|
|
|
|
|
|
this.maxPages = pdf.numPages;
|
|
|
|
|
2020-06-17 16:52:39 +02:00
|
|
|
let viewport = await page.getViewport({ scale: this.currentScale });
|
2020-04-03 22:12:12 +02:00
|
|
|
|
|
|
|
this.canvas.height = viewport.height;
|
|
|
|
this.canvas.width = viewport.width;
|
|
|
|
|
|
|
|
page.render({
|
|
|
|
canvasContext: this.context,
|
2020-06-17 16:52:39 +02:00
|
|
|
viewport,
|
2020-04-03 22:12:12 +02:00
|
|
|
});
|
2020-04-16 23:18:07 +02:00
|
|
|
|
|
|
|
this.setPagesInViewport();
|
2020-04-03 22:12:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 08:51:10 +01:00
|
|
|
const url = document.querySelector("meta[name='pdf-url']").content;
|
2020-06-17 16:52:39 +02:00
|
|
|
const canvas = document.getElementById('pdf-placeholder');
|
2020-04-03 22:12:12 +02:00
|
|
|
|
|
|
|
new PDF(url, canvas).prepare().handle();
|