2023-04-29 12:43:12 +02:00
|
|
|
class PdfContainer {
|
|
|
|
fileName;
|
|
|
|
pagesContainer;
|
|
|
|
pagesContainerWrapper;
|
|
|
|
pdfAdapters;
|
|
|
|
|
|
|
|
constructor(id, wrapperId, pdfAdapters) {
|
|
|
|
this.fileName = null;
|
|
|
|
this.pagesContainer = document.getElementById(id)
|
|
|
|
this.pagesContainerWrapper = document.getElementById(wrapperId);
|
|
|
|
this.movePageTo = this.movePageTo.bind(this);
|
|
|
|
this.addPdfs = this.addPdfs.bind(this);
|
|
|
|
this.rotateElement = this.rotateElement.bind(this);
|
|
|
|
this.rotateAll = this.rotateAll.bind(this);
|
|
|
|
this.exportPdf = this.exportPdf.bind(this);
|
|
|
|
|
|
|
|
this.pdfAdapters = pdfAdapters;
|
|
|
|
|
|
|
|
this.pdfAdapters.forEach(adapter => {
|
|
|
|
adapter.setActions({
|
|
|
|
movePageTo: this.movePageTo,
|
|
|
|
addPdfs: this.addPdfs,
|
|
|
|
rotateElement: this.rotateElement,
|
|
|
|
})
|
|
|
|
})
|
2023-04-28 21:20:56 +02:00
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
window.addPdfs = this.addPdfs;
|
|
|
|
window.exportPdf = this.exportPdf;
|
|
|
|
window.rotateAll = this.rotateAll;
|
|
|
|
}
|
2023-04-28 21:20:56 +02:00
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
movePageTo(startElement, endElement, scrollTo = false) {
|
|
|
|
const childArray = Array.from(this.pagesContainer.childNodes);
|
2023-04-28 21:20:56 +02:00
|
|
|
const startIndex = childArray.indexOf(startElement);
|
|
|
|
const endIndex = childArray.indexOf(endElement);
|
2023-04-29 12:43:12 +02:00
|
|
|
this.pagesContainer.removeChild(startElement);
|
2023-04-28 21:20:56 +02:00
|
|
|
if(!endElement) {
|
2023-04-29 12:43:12 +02:00
|
|
|
this.pagesContainer.append(startElement);
|
2023-04-28 21:20:56 +02:00
|
|
|
} else {
|
2023-04-29 12:43:12 +02:00
|
|
|
this.pagesContainer.insertBefore(startElement, endElement);
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(scrollTo) {
|
|
|
|
const { width } = startElement.getBoundingClientRect();
|
|
|
|
const vector = (endIndex !== -1 && startIndex > endIndex)
|
|
|
|
? 0-width
|
|
|
|
: width;
|
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
this.pagesContainerWrapper.scroll({
|
|
|
|
left: this.pagesContainerWrapper.scrollLeft + vector,
|
2023-04-28 21:20:56 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
addPdfs(nextSiblingElement) {
|
2023-04-28 21:20:56 +02:00
|
|
|
var input = document.createElement('input');
|
|
|
|
input.type = 'file';
|
|
|
|
input.multiple = true;
|
|
|
|
input.setAttribute("accept", "application/pdf");
|
|
|
|
|
|
|
|
input.onchange = async(e) => {
|
|
|
|
const files = e.target.files;
|
2023-04-29 12:43:12 +02:00
|
|
|
this.fileName = files[0].name;
|
2023-04-28 21:20:56 +02:00
|
|
|
for (var i=0; i < files.length; i++) {
|
2023-04-29 12:43:12 +02:00
|
|
|
this.addPdfFile(files[i], nextSiblingElement);
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
document.querySelectorAll(".enable-on-file").forEach(element => {
|
|
|
|
element.disabled = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
input.click();
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
rotateElement(element, deg) {
|
2023-04-28 21:20:56 +02:00
|
|
|
var lastTransform = element.style.rotate;
|
|
|
|
if (!lastTransform) {
|
|
|
|
lastTransform = "0";
|
|
|
|
}
|
|
|
|
const lastAngle = parseInt(lastTransform.replace(/[^\d-]/g, ''));
|
|
|
|
const newAngle = lastAngle + deg;
|
|
|
|
|
|
|
|
element.style.rotate = newAngle + "deg";
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
async addPdfFile(file, nextSiblingElement) {
|
|
|
|
const { renderer, pdfDocument } = await this.loadFile(file);
|
2023-04-28 21:20:56 +02:00
|
|
|
|
|
|
|
for (var i=0; i < renderer.pageCount; i++) {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
|
|
|
|
div.classList.add("page-container");
|
|
|
|
|
|
|
|
var img = document.createElement('img');
|
|
|
|
img.classList.add('page-image')
|
|
|
|
const imageSrc = await renderer.renderPage(i)
|
|
|
|
img.src = imageSrc;
|
|
|
|
img.pageIdx = i;
|
|
|
|
img.rend = renderer;
|
|
|
|
img.doc = pdfDocument;
|
|
|
|
div.appendChild(img);
|
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
this.pdfAdapters.forEach((adapter) => {
|
|
|
|
adapter.adapt?.(div)
|
|
|
|
})
|
2023-04-28 21:20:56 +02:00
|
|
|
if (nextSiblingElement) {
|
2023-04-29 12:43:12 +02:00
|
|
|
this.pagesContainer.insertBefore(div, nextSiblingElement);
|
2023-04-28 21:20:56 +02:00
|
|
|
} else {
|
2023-04-29 12:43:12 +02:00
|
|
|
this.pagesContainer.appendChild(div);
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
async loadFile(file) {
|
|
|
|
var objectUrl = URL.createObjectURL(file);
|
|
|
|
var pdfDocument = await this.toPdfLib(objectUrl);
|
|
|
|
var renderer = await this.toRenderer(objectUrl);
|
|
|
|
return { renderer, pdfDocument };
|
|
|
|
}
|
|
|
|
|
|
|
|
async toRenderer(objectUrl) {
|
2023-04-28 21:20:56 +02:00
|
|
|
const pdf = await pdfjsLib.getDocument(objectUrl).promise;
|
|
|
|
return {
|
|
|
|
document: pdf,
|
|
|
|
pageCount: pdf.numPages,
|
|
|
|
renderPage: async function(pageIdx) {
|
|
|
|
const page = await this.document.getPage(pageIdx+1);
|
|
|
|
|
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
|
|
|
|
// set the canvas size to the size of the page
|
|
|
|
if (page.rotate == 90 || page.rotate == 270) {
|
|
|
|
canvas.width = page.view[3];
|
|
|
|
canvas.height = page.view[2];
|
|
|
|
} else {
|
|
|
|
canvas.width = page.view[2];
|
|
|
|
canvas.height = page.view[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
// render the page onto the canvas
|
|
|
|
var renderContext = {
|
|
|
|
canvasContext: canvas.getContext("2d"),
|
|
|
|
viewport: page.getViewport({ scale: 1 })
|
|
|
|
};
|
|
|
|
|
|
|
|
await page.render(renderContext).promise;
|
|
|
|
return canvas.toDataURL();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
async toPdfLib(objectUrl) {
|
2023-04-28 21:20:56 +02:00
|
|
|
const existingPdfBytes = await fetch(objectUrl).then(res => res.arrayBuffer());
|
|
|
|
const pdfDoc = await PDFLib.PDFDocument.load(existingPdfBytes);
|
|
|
|
return pdfDoc;
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
|
2023-04-28 21:20:56 +02:00
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
rotateAll(deg) {
|
|
|
|
for (var i=0; i<this.pagesContainer.childNodes.length; i++) {
|
|
|
|
const img = this.pagesContainer.childNodes[i].querySelector("img");
|
2023-04-28 21:20:56 +02:00
|
|
|
if (!img) continue;
|
2023-04-29 12:43:12 +02:00
|
|
|
this.rotateElement(img, deg)
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
async exportPdf() {
|
2023-04-28 21:20:56 +02:00
|
|
|
const pdfDoc = await PDFLib.PDFDocument.create();
|
2023-04-29 12:43:12 +02:00
|
|
|
for (var i=0; i<this.pagesContainer.childNodes.length; i++) {
|
|
|
|
const img = this.pagesContainer.childNodes[i].querySelector("img");
|
2023-04-28 21:20:56 +02:00
|
|
|
if (!img) continue;
|
|
|
|
const pages = await pdfDoc.copyPages(img.doc, [img.pageIdx])
|
|
|
|
const page = pages[0];
|
|
|
|
|
|
|
|
const rotation = img.style.rotate;
|
|
|
|
if (rotation) {
|
|
|
|
const rotationAngle = parseInt(rotation.replace(/[^\d-]/g, ''));
|
|
|
|
page.setRotation(PDFLib.degrees(page.getRotation().angle + rotationAngle))
|
|
|
|
}
|
|
|
|
|
|
|
|
pdfDoc.addPage(page);
|
|
|
|
}
|
|
|
|
const pdfBytes = await pdfDoc.save();
|
|
|
|
const pdfBlob = new Blob([pdfBytes], { type: 'application/pdf' });
|
|
|
|
const url = URL.createObjectURL(pdfBlob);
|
|
|
|
const downloadOption = localStorage.getItem('downloadOption');
|
|
|
|
|
|
|
|
if (downloadOption === 'sameWindow') {
|
|
|
|
// Open the file in the same window
|
|
|
|
window.location.href = url;
|
|
|
|
} else if (downloadOption === 'newWindow') {
|
|
|
|
// Open the file in a new window
|
|
|
|
window.open(url, '_blank');
|
|
|
|
} else {
|
|
|
|
// Download the file
|
|
|
|
const downloadLink = document.createElement('a');
|
|
|
|
downloadLink.href = url;
|
2023-04-29 12:43:12 +02:00
|
|
|
downloadLink.download = this.fileName ? this.fileName : 'managed.pdf';
|
2023-04-28 21:20:56 +02:00
|
|
|
downloadLink.click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
export default PdfContainer;
|