Skip to content

Instantly share code, notes, and snippets.

@aaronamm
Last active December 15, 2021 15:32
Show Gist options
  • Save aaronamm/fe792b84f15fc1a8d17feecc767634b9 to your computer and use it in GitHub Desktop.
Save aaronamm/fe792b84f15fc1a8d17feecc767634b9 to your computer and use it in GitHub Desktop.
Update PDF title (meta data)
/*
To run the project:
yarn install
node index.js
*/
import { PDFDocument } from 'pdf-lib';
import { promises } from 'fs';
import { parse } from 'path';
const filePath = './path-and-file-name.pdf';
const newTitle = 'new title';
async function EditMetaData() {
const fileData = await promises.readFile(filePath);
const pdfDoc = await PDFDocument.load(fileData);
pdfDoc.setTitle(newTitle);
const pdfBytes = await pdfDoc.save();
const fileName = parse(filePath).name;
const fileExtension = parse(filePath).ext;
await promises.writeFile(`./${fileName}-edited${fileExtension}`, pdfBytes)
};
EditMetaData();
{
"name": "edit-pdf",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"pdf-lib": "^1.17.1"
},
"type": "module"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment