created a PoC for the client

This commit is contained in:
Saud Fatayerji
2023-10-26 23:41:48 +03:00
parent 13c4f664c2
commit 6d505715e0
16 changed files with 168 additions and 90 deletions

View File

@@ -1,5 +1,6 @@
import PDFLib from 'pdf-lib';
import { PDFDocument, ParseSpeeds } from 'pdf-lib';
/**
* @typedef {Object} Metadata
@@ -17,13 +18,12 @@ import PDFLib from 'pdf-lib';
*
* @param {Uint16Array} snapshot
* @param {Metadata} metadata - Set property to null or "" to clear, undefined properties will be skipped.
* @param {PDFLib} PDFLib
* @returns
* @returns Promise<Uint8Array>
*/
export async function editMetadata(snapshot, metadata) {
// Load the original PDF file
const pdfDoc = await PDFLib.PDFDocument.load(snapshot, {
parseSpeed: PDFLib.ParseSpeeds.Fastest,
const pdfDoc = await PDFDocument.load(snapshot, {
parseSpeed: ParseSpeeds.Fastest,
});
if(metadata.Title !== undefined)

View File

@@ -1,15 +1,15 @@
import PDFLib from 'pdf-lib';
import { PDFDocument } from 'pdf-lib';
export async function extractPages(snapshot, pagesToExtractArray) {
const pdfDoc = await PDFLib.PDFDocument.load(snapshot)
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 PDFLib.PDFDocument.create();
const subDocument = await PDFDocument.create();
// Check that array max number is not larger pdf pages number
if(Math.max(...pagesToExtractArray) >= pdfDoc.getPageCount()) {

View File

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

View File

@@ -1,5 +1,5 @@
import PDFLib from 'pdf-lib';
import { PDFDocument } from 'pdf-lib';
/**
* @typedef {"CUSTOM_PAGE_ORDER"|"REVERSE_ORDER"|"DUPLEX_SORT"|"BOOKLET_SORT"|"ODD_EVEN_SPLIT"|"REMOVE_FIRST"|"REMOVE_LAST"|"REMOVE_FIRST_AND_LAST"} OrderOperation
@@ -14,8 +14,8 @@ import PDFLib from 'pdf-lib';
* @returns
*/
export async function organizePages(snapshot, operation, customOrderString) {
const pdfDoc = await PDFLib.PDFDocument.load(snapshot);
let subDocument = await PDFLib.PDFDocument.create();
const pdfDoc = await PDFDocument.load(snapshot);
let subDocument = await PDFDocument.create();
const copiedPages = await subDocument.copyPages(pdfDoc, pdfDoc.getPageIndices());

View File

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

View File

@@ -1,10 +1,10 @@
import PDFLib from 'pdf-lib';
import { PDFDocument, ParseSpeeds } from 'pdf-lib';
export async function scaleContent(snapshot, scaleFactor) {
// Load the original PDF file
const pdfDoc = await PDFLib.PDFDocument.load(snapshot, {
parseSpeed: PDFLib.ParseSpeeds.Fastest,
const pdfDoc = await PDFDocument.load(snapshot, {
parseSpeed: ParseSpeeds.Fastest,
});
const pages = pdfDoc.getPages();

View File

@@ -1,10 +1,10 @@
import PDFLib from 'pdf-lib';
import { PDFDocument, ParseSpeeds } from 'pdf-lib';
export async function scalePage(snapshot, pageSize) {
// Load the original PDF file
const pdfDoc = await PDFLib.PDFDocument.load(snapshot, {
parseSpeed: PDFLib.ParseSpeeds.Fastest,
const pdfDoc = await PDFDocument.load(snapshot, {
parseSpeed: ParseSpeeds.Fastest,
});
const new_size = pageSize;

View File

@@ -1,10 +1,10 @@
import PDFLib from 'pdf-lib';
import { PDFDocument } from 'pdf-lib';
import { createSubDocument } from "./extractPages.js";
export async function splitPDF(snapshot, splitAfterPageArray) {
const pdfDoc = await PDFLib.PDFDocument.load(snapshot)
const pdfDoc = await PDFDocument.load(snapshot)
const numberOfPages = pdfDoc.getPages().length;