Skip to content

Commit

Permalink
Fix - use Image from canvas in nodejs for PDF
Browse files Browse the repository at this point in the history
svg2pdf uses Image class to detect image dimension.
This functionality provided by node-canvas.
  • Loading branch information
linev committed Feb 17, 2025
1 parent 5eb6b8e commit 122d3cf
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions modules/base/makepdf.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ async function makePDF(svg, args) {
}
});

let pr = Promise.resolve();

if (nodejs) {
const doc = internals.nodejs_document;
doc.originalCreateElementNS = doc.createElementNS;
Expand All @@ -80,6 +82,10 @@ async function makePDF(svg, args) {
};
return res;
};

pr = import('canvas').then(handle => {
globalThis.Image = handle.Image;
});
}

const orientation = (svg.width < svg.height) ? 'portrait' : 'landscape';
Expand Down Expand Up @@ -116,10 +122,9 @@ async function makePDF(svg, args) {
doc.addFont(filename, fcfg.n, fcfg.s || 'normal');
});

let pr = Promise.resolve();
if (need_symbols && !custom_fonts[kSymbol] && settings.LoadSymbolTtf) {
const handler = new FontHandler(122, 10);
pr = handler.load().then(() => {
pr = pr.then(() => handler.load()).then(() => {
handler.addCustomFontToSvg(d3_select(svg.node));
doc.addFileToVFS(kSymbol + '.ttf', handler.base64);
doc.addFont(kSymbol + '.ttf', kSymbol, 'normal');
Expand Down Expand Up @@ -152,8 +157,10 @@ async function makePDF(svg, args) {
globalThis.document = undefined;
globalThis.CSSStyleSheet = undefined;
globalThis.CSSStyleRule = undefined;
globalThis.Image = undefined;
internals.nodejs_document.createElementNS = internals.nodejs_document.originalCreateElementNS;
if (args?.as_buffer) return Buffer.from(res);
if (args?.as_buffer)
return Buffer.from(res);
}

return res;
Expand Down

0 comments on commit 122d3cf

Please sign in to comment.