Removed duplicate code of node backend

Frontend traverser needs to be updated
This commit is contained in:
Felix Kaspar
2023-10-22 00:55:28 +02:00
parent b5ba2b25cd
commit 26cdb1d04f
19 changed files with 128 additions and 255 deletions

View File

@@ -1,14 +1,12 @@
const { PDFDocument, ParseSpeeds } = PDFLib;
export const extractPages = async (snapshot, pagesToExtractArray) => {
const pdfDoc = await PDFDocument.load(snapshot)
export async function extractPages(snapshot, pagesToExtractArray, PDFLib) {
const pdfDoc = await PDFLib.PDFDocument.load(snapshot)
// TODO: invent a better format for pagesToExtractArray and convert it.
return createSubDocument(pdfDoc, pagesToExtractArray);
return createSubDocument(pdfDoc, pagesToExtractArray, PDFLib);
};
export async function createSubDocument(pdfDoc, pagesToExtractArray) {
const subDocument = await PDFDocument.create();
export async function createSubDocument(pdfDoc, pagesToExtractArray, PDFLib) {
const subDocument = await PDFLib.PDFDocument.create();
// Check that array max number is not larger pdf pages number
if(Math.max(...pagesToExtractArray) >= pdfDoc.getPageCount()) {

View File

@@ -1,6 +1,4 @@
import * as pdfcpuWraopper from "../wasm/pdfcpu-wrapper-browser.js";
export async function impose(snapshot, nup, format) {
export async function impose(snapshot, nup, format, pdfcpuWraopper) {
return await pdfcpuWraopper.oneToOne([
"pdfcpu.wasm",
"nup",

View File

@@ -1,11 +1,9 @@
const { PDFDocument, ParseSpeeds } = PDFLib;
export const mergePDFs = async (snapshots, PDFLib) => {
export const mergePDFs = async (snapshots) => {
const mergedPdf = await PDFDocument.create();
const mergedPdf = await PDFLib.PDFDocument.create();
for (let i = 0; i < snapshots.length; i++) {
const pdfToMerge = await PDFDocument.load(snapshots[i]);
const pdfToMerge = await PDFLib.PDFDocument.load(snapshots[i]);
const copiedPages = await mergedPdf.copyPages(pdfToMerge, pdfToMerge.getPageIndices());
copiedPages.forEach((page) => mergedPdf.addPage(page));

View File

@@ -1,18 +0,0 @@
const { PDFDocument, ParseSpeeds, degrees } = PDFLib;
export const rotatePages = async (snapshot, rotation) => {
// Load the original PDF file
const pdfDoc = await PDFDocument.load(snapshot, {
parseSpeed: ParseSpeeds.Fastest,
});
const pages = pdfDoc.getPages();
pages.forEach(page => {
// Change page size
page.setRotation(degrees(rotation))
});
// Serialize the modified document
return pdfDoc.save();
};

View File

@@ -0,0 +1,16 @@
export async function rotatePages (snapshot, rotation, PDFLib) {
// Load the original PDF file
const pdfDoc = await PDFLib.PDFDocument.load(snapshot, {
parseSpeed: PDFLib.ParseSpeeds.Fastest,
});
const pages = pdfDoc.getPages();
pages.forEach(page => {
// Change page size
page.setRotation(PDFLib.degrees(rotation))
});
// Serialize the modified document
return pdfDoc.save();
};

View File

@@ -1,9 +1,7 @@
const { PDFDocument, ParseSpeeds } = PDFLib;
export const scaleContent = async (snapshot, scale_factor) => {
export async function scaleContent(snapshot, scaleFactor, PDFLib) {
// Load the original PDF file
const pdfDoc = await PDFDocument.load(snapshot, {
parseSpeed: ParseSpeeds.Fastest,
const pdfDoc = await PDFLib.PDFDocument.load(snapshot, {
parseSpeed: PDFLib.ParseSpeeds.Fastest,
});
const pages = pdfDoc.getPages();
@@ -13,10 +11,10 @@ export const scaleContent = async (snapshot, scale_factor) => {
const height = page.getHeight();
// Scale content
page.scaleContent(scale_factor, scale_factor);
page.scaleContent(scaleFactor, scaleFactor);
const scaled_diff = {
width: Math.round(width - scale_factor * width),
height: Math.round(height - scale_factor * height),
width: Math.round(width - scaleFactor * width),
height: Math.round(height - scaleFactor * height),
};
// Center content in new page format

View File

@@ -1,12 +1,10 @@
const { PDFDocument, ParseSpeeds } = PDFLib;
export const scalePage = async (snapshot, page_size) => {
export async function scalePage(snapshot, pageSize, PDFLib) {
// Load the original PDF file
const pdfDoc = await PDFDocument.load(snapshot, {
parseSpeed: ParseSpeeds.Fastest,
const pdfDoc = await PDFLib.PDFDocument.load(snapshot, {
parseSpeed: PDFLib.ParseSpeeds.Fastest,
});
const new_size = page_size;
const new_size = pageSize;
const pages = pdfDoc.getPages();

View File

@@ -1,9 +1,7 @@
import { createSubDocument } from "./extractPages.js";
const { PDFDocument, ParseSpeeds } = PDFLib;
export const splitPDF = async (snapshot, splitAfterPageArray) => {
const pdfDoc = await PDFDocument.load(snapshot)
export async function splitPDF(snapshot, splitAfterPageArray, PDFLib) {
const pdfDoc = await PDFLib.PDFDocument.load(snapshot)
const numberOfPages = pdfDoc.getPages().length;
@@ -13,13 +11,13 @@ export const splitPDF = async (snapshot, splitAfterPageArray) => {
for (let i = 0; i < numberOfPages; i++) {
if(i > splitAfter && pagesArray.length > 0) {
subDocuments.push(await createSubDocument(pdfDoc, pagesArray));
subDocuments.push(await createSubDocument(pdfDoc, pagesArray, PDFLib));
splitAfter = splitAfterPageArray.shift();
pagesArray = [];
}
pagesArray.push(i);
}
subDocuments.push(await createSubDocument(pdfDoc, pagesArray));
subDocuments.push(await createSubDocument(pdfDoc, pagesArray, PDFLib));
pagesArray = [];
return subDocuments;