Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer PDF Title and Author (metadata)

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.

like image 981
liron Avatar asked Jul 03 '18 11:07

liron


2 Answers

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();
})();
like image 155
Grant Miller Avatar answered Nov 04 '22 19:11

Grant Miller


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 :

  • generate the pdf with puppeeter
  • use the returned buffer to load a pdf'lib's PdfDocument
  • set the metadata you want
  • send (and/or save) the result document
      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))
like image 5
Stephane L Avatar answered Nov 04 '22 19:11

Stephane L