added 2 new root folders
This commit is contained in:
52
shared-operations/functions/editMetadata.js
Normal file
52
shared-operations/functions/editMetadata.js
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* @typedef {Object} Metadata
|
||||
* @property {string | null | undefined} Title - The title of the document.
|
||||
* @property {string | null | undefined} Author - The author of the document.
|
||||
* @property {string | null | undefined} Subject - The subject of the document.
|
||||
* @property {string[] | null | undefined} Keywords - An array of keywords associated with the document.
|
||||
* @property {string | null | undefined} Producer - The producer of the document.
|
||||
* @property {string | null | undefined} Creator - The creator of the document.
|
||||
* @property {Date | null | undefined} CreationDate - The date when the document was created.
|
||||
* @property {Date | null | undefined} ModificationDate - The date when the document was last modified.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Uint16Array} snapshot
|
||||
* @param {Metadata} metadata - Set property to null or "" to clear, undefined properties will be skipped.
|
||||
* @param {import('pdf-lib')} PDFLib
|
||||
* @returns
|
||||
*/
|
||||
export async function editMetadata(snapshot, metadata, PDFLib) {
|
||||
// Load the original PDF file
|
||||
const pdfDoc = await PDFLib.PDFDocument.load(snapshot, {
|
||||
parseSpeed: PDFLib.ParseSpeeds.Fastest,
|
||||
});
|
||||
|
||||
if(metadata.Title !== undefined)
|
||||
pdfDoc.setTitle(metadata.Title);
|
||||
|
||||
if(metadata.Author !== undefined)
|
||||
pdfDoc.setAuthor(metadata.Author)
|
||||
|
||||
if(metadata.Subject !== undefined)
|
||||
pdfDoc.setSubject(metadata.Subject)
|
||||
|
||||
if(metadata.Keywords !== undefined)
|
||||
pdfDoc.setKeywords(metadata.Keywords)
|
||||
|
||||
if(metadata.Producer !== undefined)
|
||||
pdfDoc.setProducer(metadata.Producer)
|
||||
|
||||
if(metadata.Creator !== undefined)
|
||||
pdfDoc.setCreator(metadata.Creator)
|
||||
|
||||
if(metadata.CreationDate !== undefined)
|
||||
pdfDoc.setCreationDate(metadata.CreationDate)
|
||||
|
||||
if(metadata.ModificationDate !== undefined)
|
||||
pdfDoc.setModificationDate(metadata.ModificationDate)
|
||||
|
||||
// Serialize the modified document
|
||||
return pdfDoc.save();
|
||||
};
|
||||
23
shared-operations/functions/extractPages.js
Normal file
23
shared-operations/functions/extractPages.js
Normal file
@@ -0,0 +1,23 @@
|
||||
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, PDFLib);
|
||||
};
|
||||
|
||||
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()) {
|
||||
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();
|
||||
}
|
||||
12
shared-operations/functions/impose.js
Normal file
12
shared-operations/functions/impose.js
Normal file
@@ -0,0 +1,12 @@
|
||||
export async function impose(snapshot, nup, format, pdfcpuWrapper) {
|
||||
return await pdfcpuWrapper.oneToOne([
|
||||
"pdfcpu.wasm",
|
||||
"nup",
|
||||
"-c",
|
||||
"disable",
|
||||
'f:' + format,
|
||||
"/output.pdf",
|
||||
String(nup),
|
||||
"input.pdf",
|
||||
], snapshot);
|
||||
}
|
||||
13
shared-operations/functions/mergePDFs.js
Normal file
13
shared-operations/functions/mergePDFs.js
Normal file
@@ -0,0 +1,13 @@
|
||||
export const mergePDFs = async (snapshots, PDFLib) => {
|
||||
|
||||
const mergedPdf = await PDFLib.PDFDocument.create();
|
||||
|
||||
for (let i = 0; i < snapshots.length; i++) {
|
||||
const pdfToMerge = await PDFLib.PDFDocument.load(snapshots[i]);
|
||||
|
||||
const copiedPages = await mergedPdf.copyPages(pdfToMerge, pdfToMerge.getPageIndices());
|
||||
copiedPages.forEach((page) => mergedPdf.addPage(page));
|
||||
}
|
||||
|
||||
return mergedPdf.save();
|
||||
};
|
||||
16
shared-operations/functions/rotatePages.js
Normal file
16
shared-operations/functions/rotatePages.js
Normal 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();
|
||||
};
|
||||
27
shared-operations/functions/scaleContent.js
Normal file
27
shared-operations/functions/scaleContent.js
Normal file
@@ -0,0 +1,27 @@
|
||||
export async function scaleContent(snapshot, scaleFactor, PDFLib) {
|
||||
// Load the original PDF file
|
||||
const pdfDoc = await PDFLib.PDFDocument.load(snapshot, {
|
||||
parseSpeed: PDFLib.ParseSpeeds.Fastest,
|
||||
});
|
||||
|
||||
const pages = pdfDoc.getPages();
|
||||
|
||||
pages.forEach(page => {
|
||||
const width = page.getWidth();
|
||||
const height = page.getHeight();
|
||||
|
||||
// Scale content
|
||||
page.scaleContent(scaleFactor, scaleFactor);
|
||||
const scaled_diff = {
|
||||
width: Math.round(width - scaleFactor * width),
|
||||
height: Math.round(height - scaleFactor * 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();
|
||||
};
|
||||
29
shared-operations/functions/scalePage.js
Normal file
29
shared-operations/functions/scalePage.js
Normal file
@@ -0,0 +1,29 @@
|
||||
export async function scalePage(snapshot, pageSize, PDFLib) {
|
||||
// Load the original PDF file
|
||||
const pdfDoc = await PDFLib.PDFDocument.load(snapshot, {
|
||||
parseSpeed: PDFLib.ParseSpeeds.Fastest,
|
||||
});
|
||||
|
||||
const new_size = pageSize;
|
||||
|
||||
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
|
||||
}
|
||||
};
|
||||
24
shared-operations/functions/splitPDF.js
Normal file
24
shared-operations/functions/splitPDF.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import { createSubDocument } from "./extractPages.js";
|
||||
|
||||
export async function splitPDF(snapshot, splitAfterPageArray, PDFLib) {
|
||||
const pdfDoc = await PDFLib.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, PDFLib));
|
||||
splitAfter = splitAfterPageArray.shift();
|
||||
pagesArray = [];
|
||||
}
|
||||
pagesArray.push(i);
|
||||
}
|
||||
subDocuments.push(await createSubDocument(pdfDoc, pagesArray, PDFLib));
|
||||
pagesArray = [];
|
||||
|
||||
return subDocuments;
|
||||
};
|
||||
2
shared-operations/index.js
Normal file
2
shared-operations/index.js
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
export {};
|
||||
12
shared-operations/package.json
Normal file
12
shared-operations/package.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "shared-operations",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC"
|
||||
}
|
||||
Reference in New Issue
Block a user