Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #513 from enketo/fix/pdf-drawing-512
Browse files Browse the repository at this point in the history
Fix: works around issue with canvas not printing in puppeteer's PDF g…
  • Loading branch information
eyelidlessness authored Feb 1, 2023
2 parents 65050c7 + fdf29de commit 906a32b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/lib/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,34 @@ async function get(url, options = {}) {
throw e;
});

/*
* This works around an issue with puppeteer not printing canvas
* images that were loaded from a file.
* It is likely this issue: https://bugs.chromium.org/p/chromium/issues/detail?id=809065
* (though not WebGL-related as some of the commenters suggest)
*/
await page.evaluate(() => {
/* eslint-env browser */
function canvasToImage(element) {
const image = document.createElement('img');
image.src = element.toDataURL();

['width', 'height', 'position', 'left', 'top'].forEach(
(property) =>
(image.style[property] = element.style[property])
);
// overriding a general image style
image.style['max-width'] = '100%';
image.className = element.className;

element.parentNode &&
element.parentNode.insertBefore(image, element);
element.parentNode && element.parentNode.removeChild(element);
}

document.querySelectorAll('canvas').forEach(canvasToImage);
});

pdf = await page.pdf({
landscape: options.landscape,
format: options.format,
Expand Down

0 comments on commit 906a32b

Please sign in to comment.