Skip to content

Commit

Permalink
fix: Preformatted code across page boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Jun 1, 2022
1 parent 328751c commit d784ab3
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/printer.js
Expand Up @@ -11,6 +11,7 @@

import puppeteer from "puppeteer";
import path from "node:path";
import fs from "fs/promises";
import { createRequire } from "node:module";
import { pathToFileURL, fileURLToPath } from "node:url";
import EventEmitter from "node:events";
Expand Down Expand Up @@ -86,6 +87,28 @@ function createPdfOptions(options = {}) {
};
}

/**
* PagedJS has a bug when rendering preformatted code across page
* boundaries. This patches the polyfill so that it works correctly.
* @returns {void}
* @see https://github.com/pagedjs/pagedjs/issues/75
*/
async function patchPagedJS() {

const CODE_TO_REPLACE = "const significantWhitespaces = node.parentElement && node.parentElement.nodeName === \"PRE\";";
const REPLACEMENT_CODE = "const significantWhitespaces = node.parentElement && getComputedStyle(node.parentElement).whiteSpace === \"pre\";";
const code = await fs.readFile(pagedJSFilePath, "utf8");

if (code.includes(CODE_TO_REPLACE)) {
await fs.writeFile(
pagedJSFilePath,
code.replace(CODE_TO_REPLACE, REPLACEMENT_CODE),
"utf8"
);
}

}

/**
* Extracts meta information from the page and places it into the PDF.
* @param {PuppeteerPage} page The page to pull data from.
Expand Down Expand Up @@ -185,6 +208,8 @@ export class Printer extends EventEmitter {
*/
async printUrlToPdf(url) {

patchPagedJS();

const browser = await puppeteer.launch(
createPuppeteerOptions()
);
Expand Down

0 comments on commit d784ab3

Please sign in to comment.