Modifying Generated PDFs
Edit metadata
Set the PDF title
The title of a PDF may be displayed when the file is opened, and may also be used by search engines.
The title of PDF files generated by Puppeteer are the same as the title of the page used to generate it.
So, one way to change the PDF title is to modify the page using the callback
option.
const pageOptions = { callback: async (page) => { const title = 'my new title' // variables need to be passed as arguments await page.evaluate((title) => (document.title = title), title) }}
Other metadata
Tools like exiftool
can be used to modify other PDF metadata.
Alternatively, PDF parsers should also be able to read and edit the metadata of the PDF files.
For example, the generated PDF may include other metadata like the author (which is set to the default user agent) and the encoding software.
Merge PDFs
astro-pdf
can generate one or more PDF files for any webpage, but cannot generate a PDF file of multiple pages on its own.
However, you can acheive this with the help of other packages to merge generated PDF files.
The following example uses pdf-merger-js
:
import { rm } from 'node:fs/promises'import { fileURLToPath } from 'node:url'import PDFMerger from 'pdf-merger-js'// astro-pdf optionsconst options = { pages: { '/': true, '/page1': true, '/page2': true, 'https://en.wikipedia.org/wiki/HTML': 'html.pdf' }, runAfter: async (dir) => { const pages = ['index.pdf', 'page1.pdf', 'html.pdf', 'page2.pdf'].map((file) => fileURLToPath(new URL(file, dir)) ) const merger = new PDFMerger() for (const page of pages) { await merger.add(page) } await merger.setMetadata({ title: 'Merged pages' }) await merger.save(fileURLToPath(new URL('merged.pdf', dir))) // delete the individual PDFs await Promise.all( pages.map((page) => { // return the promise return rm(page) }) ) }}