After all my searches and code digging didn't help, I'm asking here for a hint:
How, using Puppeteer PDF generation, do I set the metadata of the file (specifically title and author)?
I've tried setting meta tags in my HTML, but it didn't output these into the file metadata.
Puppeteer does not come with built-in functionality to edit or write metadata to a PDF.
Instead, you can install the exiftool
command line utility to edit the metadata of PDFs generated with Puppeteer:
sudo apt install libimage-exiftool-perl
Then you can use the Node.js child_process.exec()
function to call the command line utility from your program after the PDF has been generated:
'use strict';
const puppeteer = require('puppeteer');
const exec = require('util').promisify(require('child_process').exec);
const execute = async command => {
const {stdout, stderr} = await exec(command);
console.log((stderr || stdout).trim());
};
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com/');
await page.pdf({
path: 'example.pdf',
});
await execute('exiftool -title="Example PDF" -author="John Doe" /var/www/example.com/public_html/example.pdf');
await browser.close();
})();
The accepted answer is right, as for now Puppeeter doesn't support setting pdf metadata. But I just wanted to give a solution using a node package instead of a native library : pdf-lib.
You need to :
PdfDocument
import puppeteer from 'puppeteer'
import { PDFDocument } from 'pdf-lib'
import fs from 'fs'
// generate pdf page as usual with puppeeter
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setContent(`Some html`),
const puppeeterPdf = await page.pdf()
await browser.close()
// Give the buffer to pdf-lib
const pdfDoc = await PDFDocument.load(puppeeterPdf)
pdfDoc.setTitle('A title')
pdfDoc.setAuthor('An author')
const pdfBytes = await pdfDoc.save()
// write to disk
await fs.promises.writeFile('path/to/file.pdf', pdfBytes)
// send via http
res.send(Buffer.from(pdfBytes))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With