mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-01-23 00:06:08 +01:00
Fixed up updateMetadata
This commit is contained in:
parent
fd6b0740fd
commit
1b0a881ad6
@ -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);
|
||||
});
|
||||
|
@ -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,32 +25,42 @@ 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();
|
||||
|
Loading…
Reference in New Issue
Block a user