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(),
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);
});

View File

@ -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<Uint8Array> {
// 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();
};
};