Skip to content

Commit c61fb81

Browse files
authored
Merge pull request #4976 from BitGo/WP-2765-jdpdf-nodejs
chore(key-card): make jspdf nodejs compatible
2 parents f1fabfe + 99c38e5 commit c61fb81

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

modules/key-card/src/drawKeycard.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
import { jsPDF } from 'jspdf';
1+
import type { jsPDF } from 'jspdf';
22
import * as QRCode from 'qrcode';
33
import { IDrawKeyCard } from './types';
44
import { splitKeys } from './utils';
5+
type jsPDFModule = typeof import('jspdf');
6+
7+
async function loadJSPDF(): Promise<jsPDFModule> {
8+
let jsPDF: jsPDFModule;
9+
10+
if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
11+
// We are in the browser
12+
jsPDF = await import('jspdf');
13+
} else {
14+
// We are in Node.js
15+
jsPDF = require('jspdf');
16+
}
17+
return jsPDF;
18+
}
519

620
enum KeyCurveName {
721
ed25519 = 'EDDSA',
@@ -97,12 +111,14 @@ export async function drawKeycard({
97111
walletLabel,
98112
curve,
99113
}: IDrawKeyCard): Promise<jsPDF> {
114+
const jsPDFModule = await loadJSPDF();
115+
100116
// document details
101117
const width = 8.5 * 72;
102118
let y = 0;
103119

104120
// Create the PDF instance
105-
const doc = new jsPDF('portrait', 'pt', 'letter'); // jshint ignore:line
121+
const doc = new jsPDFModule.jsPDF('portrait', 'pt', 'letter'); // jshint ignore:line
106122
doc.setFont('helvetica');
107123

108124
// PDF Header Area - includes the logo and company name

modules/key-card/src/index.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import jsPDF from 'jspdf';
2-
31
import { generateQrData } from './generateQrData';
42
import { generateFaq } from './faq';
53
import { drawKeycard } from './drawKeycard';
@@ -13,17 +11,16 @@ export * from './utils';
1311
export * from './types';
1412

1513
export async function generateKeycard(params: GenerateKeycardParams): Promise<void> {
16-
let keycard: jsPDF;
1714
if ('coin' in params) {
1815
const questions = generateFaq(params.coin.fullName);
1916
const qrData = generateQrData(params);
20-
keycard = await drawKeycard({ ...params, questions, qrData });
17+
const keycard = await drawKeycard({ ...params, questions, qrData });
18+
keycard.save(`BitGo Keycard for ${params.walletLabel}.pdf`);
2119
} else if ('curve' in params) {
2220
const data = generateParamsForKeyCreation(params);
23-
keycard = await drawKeycard(data);
21+
const keycard = await drawKeycard(data);
22+
keycard.save(`BitGo Keycard for ${params.walletLabel}.pdf`);
2423
} else {
2524
throw new Error('Either curve or coin must be provided');
2625
}
27-
// Save the PDF on the user's browser
28-
keycard.save(`BitGo Keycard for ${params.walletLabel}.pdf`);
2926
}

0 commit comments

Comments
 (0)