Last active
September 2, 2025 14:00
-
-
Save documentprocessing/15458e85e1e245260ddc132558c842e4 to your computer and use it in GitHub Desktop.
Read and edit metadata of PDF documents in JavaScript using pdf-lib library. Check https://products.documentprocessing.com/metadata/javascript/pdf-lib/ for more details.
This file contains hidden or 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
| // Import necessary modules from 'pdf-lib' | |
| import { PDFDocument } from 'pdf-lib'; | |
| // Import 'fs/promises' for file system operations | |
| import fs from 'fs/promises'; | |
| // Function to set metadata of a PDF document | |
| async function setMetadata(pdfDoc) { | |
| // Change the Metadata of the PDF Document | |
| // Set the author metadata | |
| pdfDoc.setAuthor("Document Processing"); | |
| // Set the title metadata | |
| pdfDoc.setTitle("Test Document"); | |
| // Save the modified PDF as bytes | |
| const modifiedPdfBytes = await pdfDoc.save(); | |
| // Write the modified PDF bytes to a new file | |
| await fs.writeFile("modified-pdf.pdf", modifiedPdfBytes); | |
| } | |
| // Read the PDF file as bytes | |
| const existingpdf = await fs.readFile('documentprocessing.pdf'); | |
| // Load the PDF using pdf-lib | |
| const pdfDoc = await PDFDocument.load(existingpdf); | |
| // Call setMetadata() to change metadata of the loaded PDF document | |
| setMetadata(pdfDoc) | |
| .then(() => { | |
| // Log success message if metadata is changed successfully | |
| console.log('Metadata Changed Successfully'); | |
| }) | |
| .catch((error) => { | |
| // Log an error message if an error occurs during metadata modification | |
| console.error('Error: ', error); | |
| }); |
This file contains hidden or 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
| // Import necessary modules from 'pdf-lib' | |
| import { PDFDocument } from 'pdf-lib'; | |
| // Import 'fs/promises' for file system operations | |
| import fs from 'fs/promises'; | |
| async function getMetadata(pdfDoc) { | |
| // Get and Display the Metadata of a document | |
| console.log("Author: ", pdfDoc.getAuthor()); | |
| console.log("Creator: ", pdfDoc.getCreator()); | |
| console.log("Creation Date: ", pdfDoc.getCreationDate()); | |
| console.log("Modification Date: ", pdfDoc.getModificationDate()); | |
| console.log("Number of pages: ", pdfDoc.getPageCount()); | |
| } | |
| // Read a PDF Document | |
| const existingpdf = await fs.readFile('modified-pdf.pdf'); | |
| const pdfDoc = await PDFDocument.load(existingpdf); | |
| // Call getMetadata() to fetch metadata of a function | |
| getMetadata(pdfDoc) | |
| .then(() => { | |
| // Log success message if metadata is changed successfully | |
| console.log('Metadata Fetched Successfully'); | |
| }) | |
| .catch((error) => { | |
| // Log an error message if an error occurs during metadata modification | |
| console.error('Error: ', error); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Working with PDF documents often requires handy tools, especially when you need to convert different files. Many people are faced with the task of how to convert correctly to preserve structure and quality. There was a discussion on the forum where a useful link https://www.reddit.com/r/studytips/comments/1kued92/i_dont_know_how_to_convert_html_to_pdf_without_it/ with tips was shared. These tips helped me save time and find working methods. Using trusted sources is more convenient and efficient.