Skip to content

Commit

Permalink
feat: Add metadata into PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Apr 14, 2022
1 parent b6079a0 commit 9ed20ba
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
77 changes: 77 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -63,6 +63,7 @@
},
"dependencies": {
"pagedjs": "^0.2.0",
"pdf-lib": "^1.17.1",
"yargs": "^16.2.0"
}
}
54 changes: 52 additions & 2 deletions src/printer.js
Expand Up @@ -3,7 +3,7 @@
* @author Nicholas C. Zakas
*/

/*global window, CSS*/
/*global window, document, CSS*/

//-----------------------------------------------------------------------------
// Imports
Expand All @@ -14,13 +14,15 @@ import path from "node:path";
import { createRequire } from "node:module";
import { pathToFileURL, fileURLToPath } from "node:url";
import EventEmitter from "node:events";
import { PDFDocument } from "pdf-lib";

//-----------------------------------------------------------------------------
// Types
//-----------------------------------------------------------------------------

/** @typedef {import("puppeteer").PDFOptions} PuppeteerPDFOptions */
/** @typedef {import("puppeteer").LaunchOptions} PuppeteerLaunchOptions */
/** @typedef {import("puppeteer").Page} PuppeteerPage */
/**
* @typedef {Object} PDFOptions
* @property {"portrait"|"landscape"} [orientation] The page orientation.
Expand Down Expand Up @@ -82,6 +84,51 @@ function createPdfOptions(options = {}) {
};
}

/**
*
* @param {PuppeteerPage} page The page to pull data from.
* @param {PDFDocument} pdf The PDF document to add meta data to.
* @returns {void}
*/
async function setPdfMeta(page, pdf) {
const meta = await page.evaluate(() => {
const result = {
title: document.title.trim(),
lang: document.querySelector("html").getAttribute("lang")
};

const tags = document.querySelectorAll("meta");
for (const tag of tags) {
if (tag.name) {
result[tag.name.toLowerCase()] = tag.content;
}
}

return result;
});


if (meta.title) {
pdf.setTitle(meta.title);
}

if (meta.lang) {
pdf.setLanguage(meta.lang);
}

if (meta.author) {
pdf.setAuthor(meta.author);
}

if (meta.keywords) {
pdf.setAuthor(meta.keywords.split(/\s*,\s*/g));
}

pdf.setCreationDate(new Date());
pdf.setModificationDate(new Date());

}

//-----------------------------------------------------------------------------
// Exports
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -248,11 +295,14 @@ export class Printer extends EventEmitter {
const blob = await page.pdf(createPdfOptions());
this.emit("pdfend", { url });

const pdf = await PDFDocument.load(blob);
await setPdfMeta(page, pdf);

// cleanup
await page.close();
await browser.close();

return blob;
return pdf.save();
}

}

0 comments on commit 9ed20ba

Please sign in to comment.