Fixed up updateMetadata

This commit is contained in:
Saud Fatayerji 2023-11-11 20:27:28 +03:00
parent fd6b0740fd
commit 1b0a881ad6
2 changed files with 47 additions and 33 deletions

View File

@ -40,7 +40,7 @@ router.post('/update-metadata', upload.single("pdfFile"), async function(req: Re
title: Joi.string(), title: Joi.string(),
trapped: Joi.string(), trapped: Joi.string(),
allRequestParams: Joi.object().pattern(Joi.string(), Joi.string()), allRequestParams: Joi.object().pattern(Joi.string(), Joi.string()),
}); }).required();
const { error, value } = schema.validate(req.body); const { error, value } = schema.validate(req.body);
if (error) { if (error) {
res.status(400).send(error.details); res.status(400).send(error.details);
@ -51,7 +51,7 @@ router.post('/update-metadata', upload.single("pdfFile"), async function(req: Re
return; 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'); const newFilename = appendToFilename(req.file.originalname, '_edited-metadata');
respondWithBinaryPdf(res, processed, newFilename); respondWithBinaryPdf(res, processed, newFilename);
}); });

View File

@ -3,15 +3,19 @@ import { PDFDocument, ParseSpeeds } from 'pdf-lib';
export type Metadata = { export type Metadata = {
Title?: string; // The title of the document. deleteAll?: boolean, // Delete all metadata if set to true
Author?: string; // The author of the document. author?: string, // The author of the document
Subject?: string; // The subject of the document. creationDate?: Date, // The creation date of the document (format: yyyy/MM/dd HH:mm:ss)
Keywords?: string[]; // An array of keywords associated with the document. creator?: string, // The creator of the document
Producer?: string; // The producer of the document. keywords?: string, // The keywords for the document
Creator?: string; // The creator of the document. modificationDate?: Date, // The modification date of the document (format: yyyy/MM/dd HH:mm:ss)
CreationDate?: Date; // The date when the document was created. producer?: string, // The producer of the document
ModificationDate?: Date; // The date when the document was last modified. 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 * @param {Uint16Array} snapshot
@ -21,33 +25,43 @@ export type Metadata = {
export async function updateMetadata(snapshot: string | Uint8Array | ArrayBuffer, metadata: Metadata): Promise<Uint8Array> { export async function updateMetadata(snapshot: string | Uint8Array | ArrayBuffer, metadata: Metadata): Promise<Uint8Array> {
// Load the original PDF file // Load the original PDF file
const pdfDoc = await PDFDocument.load(snapshot, { const pdfDoc = await PDFDocument.load(snapshot, {
parseSpeed: ParseSpeeds.Fastest, parseSpeed: ParseSpeeds.Slow,
updateMetadata: false,
}); });
if(metadata.Title) if (!metadata || metadata.deleteAll) {
pdfDoc.setTitle(metadata.Title); 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) if(metadata.author)
pdfDoc.setAuthor(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) // TODO add trapped and custom metadata. May need another library
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)
// Serialize the modified document // Serialize the modified document
return pdfDoc.save(); return pdfDoc.save();
}; };