@@ -4,6 +4,8 @@ import { IDrawKeyCard } from './types';
44import { splitKeys } from './utils' ;
55type jsPDFModule = typeof import ( 'jspdf' ) ;
66
7+ const isNode = typeof window === 'undefined' || typeof window . document === 'undefined' ;
8+
79async function loadJSPDF ( ) : Promise < jsPDFModule > {
810 let jsPDF : jsPDFModule ;
911
@@ -53,7 +55,7 @@ function moveDown(y: number, ydelta: number): number {
5355}
5456
5557function drawOnePageOfQrCodes (
56- qrImages : HTMLCanvasElement [ ] ,
58+ qrImages : ( HTMLCanvasElement | string ) [ ] ,
5759 doc : jsPDF ,
5860 y : number ,
5961 qrSize : number ,
@@ -68,7 +70,11 @@ function drawOnePageOfQrCodes(
6870 return qrIndex ;
6971 }
7072
71- doc . addImage ( image , left ( 0 ) , y , qrSize , qrSize ) ;
73+ if ( typeof image === 'string' ) {
74+ doc . addImage ( image , 'PNG' , left ( 0 ) , y , qrSize , qrSize ) ;
75+ } else {
76+ doc . addImage ( image , left ( 0 ) , y , qrSize , qrSize ) ;
77+ }
7278
7379 if ( qrImages . length === 1 ) {
7480 return qrIndex + 1 ;
@@ -127,7 +133,13 @@ export async function drawKeycard({
127133
128134 if ( keyCardImage ) {
129135 const [ imgWidth , imgHeight ] = computeKeyCardImageDimensions ( keyCardImage ) ;
130- doc . addImage ( keyCardImage , left ( 0 ) , y , imgWidth , imgHeight ) ;
136+ if ( isNode ) {
137+ // In Node.js, jsPDF cannot extract pixels from an HTMLImageElement (no DOM/canvas).
138+ // The script passes a duck-typed object whose .src is a base64 data URL — use it directly.
139+ doc . addImage ( ( keyCardImage as unknown as { src : string } ) . src , 'PNG' , left ( 0 ) , y , imgWidth , imgHeight ) ;
140+ } else {
141+ doc . addImage ( keyCardImage , left ( 0 ) , y , imgWidth , imgHeight ) ;
142+ }
131143 }
132144
133145 // Activation Code
@@ -195,10 +207,14 @@ export async function drawKeycard({
195207 const textLeft = left ( qrSize + 15 ) ;
196208 let textHeight = 0 ;
197209
198- const qrImages : HTMLCanvasElement [ ] = [ ] ;
210+ const qrImages : ( HTMLCanvasElement | string ) [ ] = [ ] ;
199211 const keys = splitKeys ( qr . data , QRBinaryMaxLength ) ;
200212 for ( const key of keys ) {
201- qrImages . push ( await QRCode . toCanvas ( key , { errorCorrectionLevel : 'L' } ) ) ;
213+ if ( isNode ) {
214+ qrImages . push ( await QRCode . toDataURL ( key , { errorCorrectionLevel : 'L' } ) ) ;
215+ } else {
216+ qrImages . push ( await QRCode . toCanvas ( key , { errorCorrectionLevel : 'L' } ) ) ;
217+ }
202218 }
203219
204220 let nextQrIndex = drawOnePageOfQrCodes ( qrImages , doc , y , qrSize , 0 ) ;
0 commit comments