Stirling-PDF/shared-operations/functions/updateMetadata.ts

68 lines
2.5 KiB
TypeScript
Raw Normal View History

import { PDFDocument, ParseSpeeds } from 'pdf-lib';
export type Metadata = {
2023-11-11 18:27:28 +01:00
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
}
2023-11-11 18:27:28 +01:00
/**
*
* @param {Uint16Array} snapshot
* @param {Metadata} metadata - Set property to null or "" to clear, undefined properties will be skipped.
* @returns Promise<Uint8Array>
*/
export async function updateMetadata(snapshot: string | Uint8Array | ArrayBuffer, metadata: Metadata): Promise<Uint8Array> {
// Load the original PDF file
const pdfDoc = await PDFDocument.load(snapshot, {
2023-11-11 18:27:28 +01:00
parseSpeed: ParseSpeeds.Slow,
updateMetadata: false,
});
2023-11-11 18:27:28 +01:00
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();
}
2023-11-11 18:27:28 +01:00
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)
2023-11-11 18:27:28 +01:00
// TODO add trapped and custom metadata. May need another library
// Serialize the modified document
return pdfDoc.save();
2023-11-11 18:27:28 +01:00
};