Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions modules/key-card/src/drawKeycard.ts

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this belongs in a different PR

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { IDrawKeyCard } from './types';
import { splitKeys } from './utils';
type jsPDFModule = typeof import('jspdf');

const isNode = typeof window === 'undefined' || typeof window.document === 'undefined';

async function loadJSPDF(): Promise<jsPDFModule> {
let jsPDF: jsPDFModule;

Expand Down Expand Up @@ -53,7 +55,7 @@ function moveDown(y: number, ydelta: number): number {
}

function drawOnePageOfQrCodes(
qrImages: HTMLCanvasElement[],
qrImages: (HTMLCanvasElement | string)[],
doc: jsPDF,
y: number,
qrSize: number,
Expand All @@ -68,7 +70,11 @@ function drawOnePageOfQrCodes(
return qrIndex;
}

doc.addImage(image, left(0), y, qrSize, qrSize);
if (typeof image === 'string') {
doc.addImage(image, 'PNG', left(0), y, qrSize, qrSize);
} else {
doc.addImage(image, left(0), y, qrSize, qrSize);
}

if (qrImages.length === 1) {
return qrIndex + 1;
Expand Down Expand Up @@ -127,7 +133,13 @@ export async function drawKeycard({

if (keyCardImage) {
const [imgWidth, imgHeight] = computeKeyCardImageDimensions(keyCardImage);
doc.addImage(keyCardImage, left(0), y, imgWidth, imgHeight);
if (isNode) {
// In Node.js, jsPDF cannot extract pixels from an HTMLImageElement (no DOM/canvas).
// The script passes a duck-typed object whose .src is a base64 data URL — use it directly.
doc.addImage((keyCardImage as unknown as { src: string }).src, 'PNG', left(0), y, imgWidth, imgHeight);
} else {
doc.addImage(keyCardImage, left(0), y, imgWidth, imgHeight);
}
}

// Activation Code
Expand Down Expand Up @@ -195,10 +207,14 @@ export async function drawKeycard({
const textLeft = left(qrSize + 15);
let textHeight = 0;

const qrImages: HTMLCanvasElement[] = [];
const qrImages: (HTMLCanvasElement | string)[] = [];
const keys = splitKeys(qr.data, QRBinaryMaxLength);
for (const key of keys) {
qrImages.push(await QRCode.toCanvas(key, { errorCorrectionLevel: 'L' }));
if (isNode) {
qrImages.push(await QRCode.toDataURL(key, { errorCorrectionLevel: 'L' }));
} else {
qrImages.push(await QRCode.toCanvas(key, { errorCorrectionLevel: 'L' }));
}
}

let nextQrIndex = drawOnePageOfQrCodes(qrImages, doc, y, qrSize, 0);
Expand Down
1 change: 1 addition & 0 deletions modules/key-card/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './extractKeycardFromPDF';
export * from './faq';
export * from './generateQrData';
export * from './parseKeycard';
export * from './upgradeWalletEncryption';
export * from './utils';
export * from './types';

Expand Down
Loading
Loading