diff --git a/server-node/src/routes/api/operations-controller.ts b/server-node/src/routes/api/operations-controller.ts index 58042733..2b3e580d 100644 --- a/server-node/src/routes/api/operations-controller.ts +++ b/server-node/src/routes/api/operations-controller.ts @@ -1,6 +1,7 @@ import Operations from '../../utils/pdf-operations'; -import { respondWithBinaryPdf, response_mustHaveExactlyOneFile } from '../../utils/endpoint-utils'; +import { respondWithPdfFile, response_mustHaveExactlyOneFile } from '../../utils/endpoint-utils'; +import { PdfFile, fromMulterFile } from '@stirling-pdf/shared-operations/wrappers/PdfFile' import express, { Request, Response } from 'express'; const router = express.Router(); @@ -8,6 +9,36 @@ import multer from 'multer'; const upload = multer(); import Joi from 'joi'; +router.post('/merge-pdfs', upload.single("pdfFile"), async function(req: Request, res: Response) { + const schema = Joi.object({ + deleteAll: Joi.string(), + author: Joi.string(), + creationDate: Joi.string(), + creator: Joi.string(), + keywords: Joi.string(), + modificationDate: Joi.string(), + producer: Joi.string(), + subject: Joi.string(), + title: Joi.string(), + trapped: Joi.string(), + allRequestParams: Joi.object().pattern(Joi.string(), Joi.string()), + }).required(); + const { error, value } = schema.validate(req.body); + if (error) { + res.status(400).send(error.details); + return; + } + if (!req.file) { + response_mustHaveExactlyOneFile(res); + return; + } + + const arrayFile = fromMulterFile(req.file); + const processed = await Operations.updateMetadata(arrayFile, value) + const newFilename = appendToFilename(req.file.originalname, '_edited-metadata'); + respondWithPdfFile(res, processed); +}); + router.post('/rotate-pdf', upload.single("pdfFile"), async function(req: Request, res: Response) { const schema = Joi.object({ angle: Joi.number().required() @@ -22,9 +53,10 @@ router.post('/rotate-pdf', upload.single("pdfFile"), async function(req: Request return; } - const rotated = await Operations.rotatePages(req.file.buffer, value.angle) - const newFilename = appendToFilename(req.file.originalname, '_rotated'); - respondWithBinaryPdf(res, rotated, newFilename); + const arrayFile = fromMulterFile(req.file); + const rotated = await Operations.rotatePages(arrayFile, value.angle) + rotated.filename = appendToFilename(arrayFile.filename, '_rotated'); + respondWithPdfFile(res, rotated); }); router.post('/update-metadata', upload.single("pdfFile"), async function(req: Request, res: Response) { @@ -51,9 +83,10 @@ router.post('/update-metadata', upload.single("pdfFile"), async function(req: Re return; } - const processed = await Operations.updateMetadata(req.file.buffer, value) - const newFilename = appendToFilename(req.file.originalname, '_edited-metadata'); - respondWithBinaryPdf(res, processed, newFilename); + const arrayFile = fromMulterFile(req.file); + const processed = await Operations.updateMetadata(arrayFile, value) + processed.filename = appendToFilename(arrayFile.filename, '_edited-metadata'); + respondWithPdfFile(res, processed); }); /** diff --git a/server-node/src/utils/endpoint-utils.ts b/server-node/src/utils/endpoint-utils.ts index e49701c0..2933da1f 100644 --- a/server-node/src/utils/endpoint-utils.ts +++ b/server-node/src/utils/endpoint-utils.ts @@ -1,13 +1,15 @@ -import express, { Request, Response } from 'express'; +import { Response } from 'express'; +import { PdfFile } from '@stirling-pdf/shared-operations/wrappers/PdfFile' -export function respondWithBinaryPdf(res: Response, buffer: Uint8Array, filename: string) { +export async function respondWithPdfFile(res: Response, file: PdfFile): Promise { + const byteFile = await file.convertToByteArrayFile(); res.writeHead(200, { 'Content-Type': "application/pdf", - 'Content-disposition': 'attachment;filename=' + filename, - 'Content-Length': buffer.length + 'Content-disposition': 'attachment;filename=' + byteFile.filename, + 'Content-Length': byteFile.byteArray?.length }); - res.end(buffer) + res.end(byteFile.byteArray) } export function response_mustHaveExactlyOneFile(res: Response): void { diff --git a/shared-operations/functions/mergePDFs.ts b/shared-operations/functions/mergePDFs.ts index 883bacfb..e24429a9 100644 --- a/shared-operations/functions/mergePDFs.ts +++ b/shared-operations/functions/mergePDFs.ts @@ -14,5 +14,5 @@ export async function mergePDFs(files: PdfFile[]): Promise { copiedPages.forEach((page) => mergedPdf.addPage(page)); } - return fromPdfLib(mergedPdf); + return fromPdfLib(mergedPdf, files[0].filename); }; \ No newline at end of file diff --git a/shared-operations/functions/rotatePages.ts b/shared-operations/functions/rotatePages.ts index 7a0a4b5d..40c51b20 100644 --- a/shared-operations/functions/rotatePages.ts +++ b/shared-operations/functions/rotatePages.ts @@ -1,19 +1,26 @@ -import { PDFDocument, ParseSpeeds, degrees } from 'pdf-lib'; - -export async function rotatePages(snapshot: string | Uint8Array | ArrayBuffer, rotation: number): Promise { - // Load the original PDF file - const pdfDoc = await PDFDocument.load(snapshot, { - parseSpeed: ParseSpeeds.Fastest, - }); +import { degrees } from 'pdf-lib'; +import { PdfFile, fromPdfLib } from '../wrappers/PdfFile'; +export async function rotatePages(file: PdfFile, rotation: number|number[]): Promise { + const pdfDoc = await file.getAsPdfLib(); const pages = pdfDoc.getPages(); - pages.forEach(page => { - // Change page size - page.setRotation(degrees(rotation)) - }); + if (Array.isArray(rotation)) { + if (rotation.length != pages.length) { + throw new Error(`Number of given rotations '${rotation.length}' is not the same as the number of pages '${pages.length}'`) + } + for (let i=0; i { + // Change page size + const oldRotation = page.getRotation().angle + page.setRotation(degrees(oldRotation + rotation)) + }); + } - // Serialize the modified document - return pdfDoc.save(); + return fromPdfLib(pdfDoc, file.filename); }; \ No newline at end of file diff --git a/shared-operations/functions/scaleContent.ts b/shared-operations/functions/scaleContent.ts index 8592d085..f30fa2e1 100644 --- a/shared-operations/functions/scaleContent.ts +++ b/shared-operations/functions/scaleContent.ts @@ -1,30 +1,36 @@ -import { PDFDocument, ParseSpeeds } from 'pdf-lib'; - -export async function scaleContent(snapshot: string | Uint8Array | ArrayBuffer, scaleFactor: number): Promise { - // Load the original PDF file - const pdfDoc = await PDFDocument.load(snapshot, { - parseSpeed: ParseSpeeds.Fastest, - }); +import { PDFPage } from 'pdf-lib'; +import { PdfFile, fromPdfLib } from '../wrappers/PdfFile'; +export async function scaleContent(file: PdfFile, scaleFactor: number|number[]): Promise { + const pdfDoc = await file.getAsPdfLib(); 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), - }; + if (Array.isArray(scaleFactor)) { + if (scaleFactor.length != pages.length) { + throw new Error(`Number of given scale factors '${scaleFactor.length}' is not the same as the number of pages '${pages.length}'`) + } + for (let i=0; i scalePage(page, scaleFactor)); + } - // Center content in new page format - page.translateContent(Math.round(scaled_diff.width / 2), Math.round(scaled_diff.height / 2)); + return fromPdfLib(pdfDoc, file.filename); +}; - }); +function scalePage(page: PDFPage, scaleFactor: number) { + const width = page.getWidth(); + const height = page.getHeight(); - // Serialize the modified document - return pdfDoc.save(); -}; \ No newline at end of file + // 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)); +} \ No newline at end of file diff --git a/shared-operations/functions/scalePage.ts b/shared-operations/functions/scalePage.ts index 5e47adff..8ffb8e6a 100644 --- a/shared-operations/functions/scalePage.ts +++ b/shared-operations/functions/scalePage.ts @@ -1,26 +1,50 @@ -import { PDFDocument, ParseSpeeds } from 'pdf-lib'; - -export async function scalePage(snapshot: string | Uint8Array | ArrayBuffer, pageSize: {width:number,height:number}): Promise { - // Load the original PDF file - const pdfDoc = await PDFDocument.load(snapshot, { - parseSpeed: ParseSpeeds.Fastest, - }); - - const new_size = pageSize; +import { PDFPage } from 'pdf-lib'; +import { PdfFile, fromPdfLib } from '../wrappers/PdfFile'; +export async function scalePage(file: PdfFile, pageSize: {width?:number,height?:number}|{width?:number,height?:number}[]): Promise { + const pdfDoc = await file.getAsPdfLib(); 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(); + if (Array.isArray(pageSize)) { + if (pageSize.length != pages.length) { + throw new Error(`Number of given sizes '${pageSize.length}' is not the same as the number of pages '${pages.length}'`) + } + for (let i=0; i resize(page, pageSize)); + } + + return fromPdfLib(pdfDoc, file.filename); }; -export const PageSize = { +function resize(page: PDFPage, newSize: {width?:number,height?:number}) { + const calculatedSize = calculateSize(page, newSize); + page.setSize(calculatedSize.width, calculatedSize.height); + + const xRatio = calculatedSize.width / page.getWidth(); + const yRatio = calculatedSize.height / page.getHeight(); + page.scaleContent(xRatio, yRatio); +} + +function calculateSize(page: PDFPage, newSize: {width?:number,height?:number}): {width:number,height:number} { + if (!newSize.width && !newSize.height){ + throw new Error(`Sizes '${newSize}' cannot have null width and null height`); + } else if (!newSize.width && newSize.height) { + const oldSize = page.getSize(); + const ratio = oldSize.width / oldSize.height; + return { width: newSize.height * ratio, height: newSize.height }; + } else if (newSize.width && !newSize.height) { + const oldSize = page.getSize(); + const ratio = oldSize.height / oldSize.width; + return { width: newSize.width, height: newSize.width * ratio }; + } + return { width: newSize.width!, height: newSize.height! }; +} + +export const PageSize = Object.freeze({ a4: { width: 594.96, height: 841.92 @@ -29,4 +53,4 @@ export const PageSize = { width: 612, height: 792 } -}; \ No newline at end of file +}); \ No newline at end of file diff --git a/shared-operations/functions/splitOn.ts b/shared-operations/functions/splitOn.ts index 9ff8d430..fe88d67c 100644 --- a/shared-operations/functions/splitOn.ts +++ b/shared-operations/functions/splitOn.ts @@ -1,12 +1,9 @@ -import { PDFDocument } from 'pdf-lib'; -import * as PDFJS from 'pdfjs-dist'; import jsQR from "jsqr"; import { detectEmptyPages } from "./common/detectEmptyPages.js"; import { getImagesOnPage } from "./common/getImagesOnPage.js"; import { selectPages } from "./subDocumentFunctions"; -import { TypedArray, DocumentInitParameters } from 'pdfjs-dist/types/src/display/api.js'; import { PdfFile } from '../wrappers/PdfFile.js'; export async function splitOn( diff --git a/shared-operations/functions/subDocumentFunctions.ts b/shared-operations/functions/subDocumentFunctions.ts index 9c393d42..4609f021 100644 --- a/shared-operations/functions/subDocumentFunctions.ts +++ b/shared-operations/functions/subDocumentFunctions.ts @@ -48,7 +48,7 @@ export async function selectPages(file: PdfFile, pagesToExtractArray: number[]): subDocument.addPage(copiedPages[i]); } - return fromPdfLib(subDocument); + return fromPdfLib(subDocument, file.filename); } export async function removePages(file: PdfFile, pagesToRemoveArray: number[]): Promise { diff --git a/shared-operations/functions/updateMetadata.ts b/shared-operations/functions/updateMetadata.ts index 6d028235..5e16019b 100644 --- a/shared-operations/functions/updateMetadata.ts +++ b/shared-operations/functions/updateMetadata.ts @@ -1,6 +1,6 @@ import { PDFDocument, ParseSpeeds } from 'pdf-lib'; - +import { PdfFile, fromPdfLib } from '../wrappers/PdfFile'; export type Metadata = { deleteAll?: boolean, // Delete all metadata if set to true @@ -13,21 +13,11 @@ export type Metadata = { subject?: string, // The subject of the document title?: string, // The title of the document //trapped?: string, // The trapped status of the document - //allRequestParams?: object, // Map list of key and value of custom parameters. Note these must start with customKey and customValue if they are non-standard + //allRequestParams?: {[key: string]: [key: string]}, // Map list of key and value of custom parameters. Note these must start with customKey and customValue if they are non-standard } -/** - * - * @param {Uint16Array} snapshot - * @param {Metadata} metadata - Set property to null or "" to clear, undefined properties will be skipped. - * @returns Promise - */ -export async function updateMetadata(snapshot: string | Uint8Array | ArrayBuffer, metadata: Metadata): Promise { - // Load the original PDF file - const pdfDoc = await PDFDocument.load(snapshot, { - parseSpeed: ParseSpeeds.Slow, - updateMetadata: false, - }); +export async function updateMetadata(file: PdfFile, metadata: Metadata|null): Promise { + const pdfDoc = await file.getAsPdfLib(); if (!metadata || metadata.deleteAll) { pdfDoc.setAuthor(""); @@ -40,7 +30,7 @@ export async function updateMetadata(snapshot: string | Uint8Array | ArrayBuffer pdfDoc.setTitle("") } if (!metadata) { - return pdfDoc.save(); + return fromPdfLib(pdfDoc, file.filename); } if(metadata.author) @@ -62,6 +52,5 @@ export async function updateMetadata(snapshot: string | Uint8Array | ArrayBuffer // TODO add trapped and custom metadata. May need another library - // Serialize the modified document - return pdfDoc.save(); + return fromPdfLib(pdfDoc, file.filename); }; diff --git a/shared-operations/wrappers/PdfFile.ts b/shared-operations/wrappers/PdfFile.ts index 5faa6828..0403c425 100644 --- a/shared-operations/wrappers/PdfFile.ts +++ b/shared-operations/wrappers/PdfFile.ts @@ -7,12 +7,13 @@ export class PdfFile { byteArray: Uint8Array | null; pdfLib: PDFDocument | null; pdfJs: PDFDocumentProxy | null; - filename?: string; + filename: string; constructor() { this.byteArray = null; this.pdfLib = null; this.pdfJs = null; + this.filename = ""; } async convertToByteArrayFile(): Promise { @@ -57,22 +58,22 @@ export class PdfFile { } } -export function fromMulterFile(value: Express.Multer.File, filename?: string): PdfFile { - return fromUint8Array(value.buffer, filename) +export function fromMulterFile(value: Express.Multer.File): PdfFile { + return fromUint8Array(value.buffer, value.originalname) } -export function fromUint8Array(value: Uint8Array, filename?: string): PdfFile { +export function fromUint8Array(value: Uint8Array, filename: string): PdfFile { const out = new PdfFile(); out.byteArray = value; out.filename = filename; return out; } -export function fromPdfLib(value: PDFDocument, filename?: string): PdfFile { +export function fromPdfLib(value: PDFDocument, filename: string): PdfFile { const out = new PdfFile(); out.pdfLib = value; out.filename = filename; return out; } -export function fromPdfJs(value: PDFDocumentProxy, filename?: string): PdfFile { +export function fromPdfJs(value: PDFDocumentProxy, filename: string): PdfFile { const out = new PdfFile(); out.pdfJs = value; out.filename = filename;