Last active
December 15, 2021 15:32
-
-
Save aaronamm/fe792b84f15fc1a8d17feecc767634b9 to your computer and use it in GitHub Desktop.
Update PDF title (meta data)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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