Removed duplicate code of node backend
Frontend traverser needs to be updated
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
import { PDFDocument, ParseSpeeds } from 'pdf-lib'
|
||||
|
||||
export const extractPages = async (snapshot, pagesToExtractArray) => {
|
||||
const pdfDoc = await PDFDocument.load(snapshot)
|
||||
|
||||
// TODO: invent a better format for pagesToExtractArray and convert it.
|
||||
return createSubDocument(pdfDoc, pagesToExtractArray);
|
||||
};
|
||||
|
||||
export async function createSubDocument(pdfDoc, pagesToExtractArray) {
|
||||
const subDocument = await PDFDocument.create();
|
||||
|
||||
// Check that array max number is not larger pdf pages number
|
||||
if(Math.max(...pagesToExtractArray) >= pdfDoc.getPageCount()) {
|
||||
throw new Error(`The PDF document only has ${pdfDoc.getPageCount()} pages and you tried to extract page ${Math.max(...pagesToExtractArray)}`);
|
||||
}
|
||||
|
||||
const copiedPages = await subDocument.copyPages(pdfDoc, pagesToExtractArray);
|
||||
|
||||
for (let i = 0; i < copiedPages.length; i++) {
|
||||
subDocument.addPage(copiedPages[i]);
|
||||
}
|
||||
|
||||
return subDocument.save();
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import * as pdfcpuWraopper from "../public/wasm/pdfcpu-wrapper-node.js";
|
||||
|
||||
export async function impose(snapshot, nup, format) {
|
||||
return await pdfcpuWraopper.oneToOne([
|
||||
"pdfcpu.wasm",
|
||||
"nup",
|
||||
"-c",
|
||||
"disable",
|
||||
'f:' + format,
|
||||
"/output.pdf",
|
||||
String(nup),
|
||||
"input.pdf",
|
||||
], snapshot);
|
||||
}
|
||||
|
||||
38
functions/index.js
Normal file
38
functions/index.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import PDFLib from 'pdf-lib';
|
||||
import * as pdfcpuWraopper from "../public/wasm/pdfcpu-wrapper-node.js";
|
||||
|
||||
import { extractPages as dependantExtractPages } from "../public/functions/extractPages.js";
|
||||
import { impose as dependantImpose } from '../public/functions/impose.js';
|
||||
import { mergePDFs as dependantMergePDFs } from '../public/functions/mergePDFs.js';
|
||||
import { rotatePages as dependantRotatePages } from '../public/functions/rotatePages.js';
|
||||
import { scaleContent as dependantScaleContent} from '../public/functions/scaleContent.js';
|
||||
import { scalePage as dependantScalePage } from '../public/functions/scalePage.js';
|
||||
import { splitPDF as dependantSplitPDF } from '../public/functions/splitPDF.js';
|
||||
|
||||
export async function extractPages(snapshot, pagesToExtractArray) {
|
||||
return dependantExtractPages(snapshot, pagesToExtractArray, PDFLib);
|
||||
}
|
||||
|
||||
export async function impose(snapshot, nup, format) {
|
||||
return dependantImpose(snapshot, nup, format, pdfcpuWraopper);
|
||||
}
|
||||
|
||||
export async function mergePDFs(snapshots) {
|
||||
return dependantMergePDFs(snapshots, PDFLib);
|
||||
}
|
||||
|
||||
export async function rotatePages(snapshot, rotation) {
|
||||
return dependantRotatePages(snapshot, rotation, PDFLib);
|
||||
}
|
||||
|
||||
export async function scaleContent(snapshot, scaleFactor) {
|
||||
return dependantScaleContent(snapshot, scaleFactor, PDFLib);
|
||||
}
|
||||
|
||||
export async function scalePage(snapshot, pageSize) {
|
||||
return dependantScalePage(snapshot, pageSize, PDFLib);
|
||||
}
|
||||
|
||||
export async function splitPDF(snapshot, splitAfterPageArray) {
|
||||
return dependantSplitPDF(snapshot, splitAfterPageArray, PDFLib);
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { PDFDocument, ParseSpeeds } from 'pdf-lib'
|
||||
|
||||
export const mergePDFs = async (snapshots) => {
|
||||
|
||||
const mergedPdf = await PDFDocument.create();
|
||||
|
||||
for (let i = 0; i < snapshots.length; i++) {
|
||||
const pdfToMerge = await PDFDocument.load(snapshots[i]);
|
||||
|
||||
const copiedPages = await mergedPdf.copyPages(pdfToMerge, pdfToMerge.getPageIndices());
|
||||
copiedPages.forEach((page) => mergedPdf.addPage(page));
|
||||
}
|
||||
|
||||
return mergedPdf.save();
|
||||
};
|
||||
@@ -1,18 +0,0 @@
|
||||
import { PDFDocument, ParseSpeeds } from 'pdf-lib'
|
||||
|
||||
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();
|
||||
};
|
||||
@@ -1,29 +0,0 @@
|
||||
import { PDFDocument, ParseSpeeds } from 'pdf-lib'
|
||||
|
||||
export const scaleContent = async (snapshot, scale_factor) => {
|
||||
// Load the original PDF file
|
||||
const pdfDoc = await PDFDocument.load(snapshot, {
|
||||
parseSpeed: ParseSpeeds.Fastest,
|
||||
});
|
||||
|
||||
const pages = pdfDoc.getPages();
|
||||
|
||||
pages.forEach(page => {
|
||||
const width = page.getWidth();
|
||||
const height = page.getHeight();
|
||||
|
||||
// Scale content
|
||||
page.scaleContent(scale_factor, scale_factor);
|
||||
const scaled_diff = {
|
||||
width: Math.round(width - scale_factor * width),
|
||||
height: Math.round(height - scale_factor * height),
|
||||
};
|
||||
|
||||
// Center content in new page format
|
||||
page.translateContent(Math.round(scaled_diff.width / 2), Math.round(scaled_diff.height / 2));
|
||||
|
||||
});
|
||||
|
||||
// Serialize the modified document
|
||||
return pdfDoc.save();
|
||||
};
|
||||
@@ -1,31 +0,0 @@
|
||||
import { PDFDocument, ParseSpeeds } from 'pdf-lib'
|
||||
|
||||
export const scalePage = async (snapshot, page_size) => {
|
||||
// Load the original PDF file
|
||||
const pdfDoc = await PDFDocument.load(snapshot, {
|
||||
parseSpeed: ParseSpeeds.Fastest,
|
||||
});
|
||||
|
||||
const new_size = page_size;
|
||||
|
||||
const pages = pdfDoc.getPages();
|
||||
|
||||
pages.forEach(page => {
|
||||
// Change page size
|
||||
page.setSize(new_size.width, new_size.height);
|
||||
});
|
||||
|
||||
// Serialize the modified document
|
||||
return pdfDoc.save();
|
||||
};
|
||||
|
||||
export const PageSize = {
|
||||
a4: {
|
||||
width: 594.96,
|
||||
height: 841.92
|
||||
},
|
||||
letter: {
|
||||
width: 612,
|
||||
height: 792
|
||||
}
|
||||
};
|
||||
@@ -1,24 +0,0 @@
|
||||
import { PDFDocument, ParseSpeeds } from 'pdf-lib'
|
||||
import { createSubDocument } from "./extractPages.js";
|
||||
|
||||
export const splitPDF = async (snapshot, splitAfterPageArray) => {
|
||||
const pdfDoc = await PDFDocument.load(snapshot)
|
||||
|
||||
const numberOfPages = pdfDoc.getPages().length;
|
||||
|
||||
let pagesArray = [];
|
||||
let splitAfter = splitAfterPageArray.shift();
|
||||
const subDocuments = [];
|
||||
|
||||
for (let i = 0; i < numberOfPages; i++) {
|
||||
if(i > splitAfter && pagesArray.length > 0) {
|
||||
subDocuments.push(await createSubDocument(pdfDoc, pagesArray));
|
||||
splitAfter = splitAfterPageArray.shift();
|
||||
pagesArray = [];
|
||||
}
|
||||
pagesArray.push(i);
|
||||
}
|
||||
subDocuments.push(await createSubDocument(pdfDoc, pagesArray));
|
||||
pagesArray = [];
|
||||
return subDocuments;
|
||||
};
|
||||
Reference in New Issue
Block a user