From 1b0a881ad6667c9c7ce04ebf9d1b3c6af7e1b770 Mon Sep 17 00:00:00 2001 From: Saud Fatayerji Date: Sat, 11 Nov 2023 20:27:28 +0300 Subject: [PATCH] Fixed up updateMetadata --- .../src/routes/api/operations-controller.ts | 4 +- shared-operations/functions/updateMetadata.ts | 76 +++++++++++-------- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/server-node/src/routes/api/operations-controller.ts b/server-node/src/routes/api/operations-controller.ts index 7a71ae30..58042733 100644 --- a/server-node/src/routes/api/operations-controller.ts +++ b/server-node/src/routes/api/operations-controller.ts @@ -40,7 +40,7 @@ router.post('/update-metadata', upload.single("pdfFile"), async function(req: Re 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); @@ -51,7 +51,7 @@ router.post('/update-metadata', upload.single("pdfFile"), async function(req: Re return; } - const processed = await Operations.updateMetadata(req.file.buffer, value.angle) + const processed = await Operations.updateMetadata(req.file.buffer, value) const newFilename = appendToFilename(req.file.originalname, '_edited-metadata'); respondWithBinaryPdf(res, processed, newFilename); }); diff --git a/shared-operations/functions/updateMetadata.ts b/shared-operations/functions/updateMetadata.ts index 8a87dbab..6d028235 100644 --- a/shared-operations/functions/updateMetadata.ts +++ b/shared-operations/functions/updateMetadata.ts @@ -3,15 +3,19 @@ import { PDFDocument, ParseSpeeds } from 'pdf-lib'; export type Metadata = { - Title?: string; // The title of the document. - Author?: string; // The author of the document. - Subject?: string; // The subject of the document. - Keywords?: string[]; // An array of keywords associated with the document. - Producer?: string; // The producer of the document. - Creator?: string; // The creator of the document. - CreationDate?: Date; // The date when the document was created. - ModificationDate?: Date; // The date when the document was last modified. + deleteAll?: boolean, // Delete all metadata if set to true + author?: string, // The author of the document + creationDate?: Date, // The creation date of the document (format: yyyy/MM/dd HH:mm:ss) + creator?: string, // The creator of the document + keywords?: string, // The keywords for the document + modificationDate?: Date, // The modification date of the document (format: yyyy/MM/dd HH:mm:ss) + producer?: string, // The producer of the document + 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 } + /** * * @param {Uint16Array} snapshot @@ -21,33 +25,43 @@ export type Metadata = { export async function updateMetadata(snapshot: string | Uint8Array | ArrayBuffer, metadata: Metadata): Promise { // Load the original PDF file const pdfDoc = await PDFDocument.load(snapshot, { - parseSpeed: ParseSpeeds.Fastest, + parseSpeed: ParseSpeeds.Slow, + updateMetadata: false, }); - if(metadata.Title) - pdfDoc.setTitle(metadata.Title); + if (!metadata || metadata.deleteAll) { + pdfDoc.setAuthor(""); + pdfDoc.setCreationDate(new Date(0)) + pdfDoc.setCreator("") + pdfDoc.setKeywords([]) + pdfDoc.setModificationDate(new Date(0)) + pdfDoc.setProducer("") + pdfDoc.setSubject("") + pdfDoc.setTitle("") + } + if (!metadata) { + return pdfDoc.save(); + } - if(metadata.Author) - pdfDoc.setAuthor(metadata.Author) + if(metadata.author) + pdfDoc.setAuthor(metadata.author); + if(metadata.creationDate) + pdfDoc.setCreationDate(metadata.creationDate) + if(metadata.creator) + pdfDoc.setCreator(metadata.creator) + if(metadata.keywords) + pdfDoc.setKeywords(metadata.keywords.split(",")) + if(metadata.modificationDate) + pdfDoc.setModificationDate(metadata.modificationDate) + if(metadata.producer) + pdfDoc.setProducer(metadata.producer) + if(metadata.subject) + pdfDoc.setSubject(metadata.subject) + if(metadata.title) + pdfDoc.setTitle(metadata.title) - if(metadata.Subject) - pdfDoc.setSubject(metadata.Subject) - - if(metadata.Keywords) - pdfDoc.setKeywords(metadata.Keywords) - - if(metadata.Producer) - pdfDoc.setProducer(metadata.Producer) - - if(metadata.Creator) - pdfDoc.setCreator(metadata.Creator) - - if(metadata.CreationDate) - pdfDoc.setCreationDate(metadata.CreationDate) - - if(metadata.ModificationDate) - pdfDoc.setModificationDate(metadata.ModificationDate) + // TODO add trapped and custom metadata. May need another library // Serialize the modified document return pdfDoc.save(); -}; \ No newline at end of file +};