Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Commit 99e4ac3

Browse files
author
jcdang
committed
switched from using classnames to ids
1 parent 3a9234d commit 99e4ac3

File tree

2 files changed

+47
-42
lines changed

2 files changed

+47
-42
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "print-pdf",
3-
"version": "0.2.5",
3+
"version": "0.2.6",
44
"description": "Print DOM element to PDF",
55
"keywords": [
66
"print",

src/PrintPDF.ts

+46-41
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ export class PrintPDF {
149149
return elMap;
150150
}
151151

152-
public getUniqueClassName(): string {
152+
public getUniqueId(): string {
153153
this.uniqueCounter += 1;
154154

155-
return `print-pdf-cn-${this.uniqueCounter}`;
155+
return `print-pdf-${this.uniqueCounter}`;
156156
}
157157

158158
public traverseNodes(o: HTMLElement, d: HTMLElement, applyFn: (n1: HTMLElement, n2: HTMLElement) => void) {
@@ -254,16 +254,16 @@ export class PrintPDF {
254254
.forEach((propertyName: string) => {
255255
const propValue = computedStyle.getPropertyValue(propertyName);
256256
if (selector !== '' || defaultMap.get(propertyName) !== propValue) {
257-
textPairs.push(`\t${propertyName}: ${propValue} !important;`);
257+
textPairs.push(`\t${propertyName}: ${propValue};`);
258258
}
259259
});
260260
const cssText = textPairs.join('\n');
261-
if (dst.className.length === 0) {
262-
dst.className = this.getUniqueClassName();
261+
if (!dst.id) {
262+
dst.id = this.getUniqueId();
263+
dst.className = `print-cn-${dst.tagName}`;
263264
}
264-
const className = dst.className;
265265

266-
return `\n.${className}${selector} {\n${cssText}\n}\n`;
266+
return `\n#${dst.id}${selector} {\n${cssText}\n}\n`;
267267
}
268268

269269
public toPDF(progressFn: (percentComplete: number, statusMsg: string) => void = () => { return; }): Promise<JsPDF> {
@@ -292,32 +292,41 @@ export class PrintPDF {
292292
return this.copyFontFaces(globalStyle, progressFn);
293293
}).then(() => {
294294
let counter = 0;
295-
const copySubTaskPercent = .6;
295+
const copySubTaskPercent = .65;
296296
const nodePromises = [];
297297
this.traverseNodes(element, copyElement, (src: HTMLElement, dst: HTMLElement) => {
298-
nodePromises.push(new Promise(resolve => {
299-
this.win.setTimeout(() => {
300-
counter++;
301-
srcDstPairs.push({ src, dst });
302-
dst.className = '';
303-
dst.removeAttribute('style');
304-
const computedStyle = this.win.getComputedStyle(src);
305-
const text = this.getCSSText(computedStyle, dst);
306-
copiedStyles.push(text);
307-
PrintPDF.PSEUDO_ELEMENTS.forEach((pseudoSelector: string) => {
308-
const psComputedStyle = this.win.getComputedStyle(src, pseudoSelector);
309-
const content = psComputedStyle.getPropertyValue('content');
310-
if (content !== 'none') {
311-
const psText = this.getCSSText(psComputedStyle, dst, pseudoSelector);
312-
copiedStyles.push(psText);
313-
}
314-
});
315-
const statusMsg = `Reading Computed Style ${counter}/${nodePromises.length}`;
316-
const percentComplete = (counter / nodePromises.length * copySubTaskPercent) + .2;
317-
progressFn(percentComplete, statusMsg);
318-
resolve();
319-
}, counter * 100);
320-
}));
298+
srcDstPairs.push({ src, dst });
299+
dst.className = '';
300+
dst.removeAttribute('id');
301+
dst.removeAttribute('style');
302+
counter++;
303+
const itemNumber = counter;
304+
const nodePromise = new Promise<number>(resolve => {
305+
const computedStyle = this.win.getComputedStyle(src);
306+
const text = this.getCSSText(computedStyle, dst);
307+
copiedStyles.push(text);
308+
PrintPDF.PSEUDO_ELEMENTS.forEach((pseudoSelector: string) => {
309+
const psComputedStyle = this.win.getComputedStyle(src, pseudoSelector);
310+
const content = psComputedStyle.getPropertyValue('content');
311+
if (content !== 'none') {
312+
const psText = this.getCSSText(psComputedStyle, dst, pseudoSelector);
313+
copiedStyles.push(psText);
314+
}
315+
});
316+
resolve(itemNumber);
317+
});
318+
nodePromises.push(nodePromise);
319+
nodePromise.then(c => {
320+
return new Promise(resolve => {
321+
this.win.setTimeout(() => {
322+
const statusMsg = `Reading Computed Style ${c}/${nodePromises.length}`;
323+
const percentComplete = (c / nodePromises.length * copySubTaskPercent) + .2;
324+
progressFn(percentComplete, statusMsg);
325+
resolve();
326+
}, 1);
327+
});
328+
329+
});
321330
});
322331

323332
return Promise.all(nodePromises);
@@ -331,10 +340,6 @@ export class PrintPDF {
331340
copiedStyles.push(styleText);
332341
});
333342

334-
// Adding the default styling based on tag name
335-
this.traverseNodes(element, copyElement, (__: HTMLElement, dst: HTMLElement) => {
336-
dst.className = `print-cn-${dst.tagName} ${dst.className}`;
337-
});
338343
const stylesText = copiedStyles.join('');
339344
const textNode = this.doc.createTextNode(stylesText);
340345
globalStyle.appendChild(textNode);
@@ -351,16 +356,16 @@ export class PrintPDF {
351356

352357
return Promise.all(promises);
353358
}).then(() => {
354-
progressFn(.80, 'Appending Master StyleSheet');
359+
progressFn(.85, 'Appending Master StyleSheet');
355360
copyElement.appendChild(globalStyle);
356361

357362
return copyElement;
358363
}).then(copy => {
359-
progressFn(.83, 'Serializing');
364+
progressFn(.87, 'Serializing');
360365

361366
return this.serializer.serializeToString(copy);
362367
}).then(serialized => {
363-
progressFn(.84, 'Encoding to Data URI');
368+
progressFn(.88, 'Encoding to Data URI');
364369
const encoded = encodeURIComponent(serialized);
365370
const foreignObject = `<foreignObject width='100%' height='100%'>${encoded}</foreignObject>`;
366371
const namespace = 'http://www.w3.org/2000/svg';
@@ -371,7 +376,7 @@ export class PrintPDF {
371376

372377
return new Promise(resolve => {
373378
this.win.setTimeout(() => {
374-
progressFn(.85, 'Creating Canvas');
379+
progressFn(.90, 'Creating Canvas');
375380
const tmpCanvas = this.doc.createElement('canvas');
376381
tmpCanvas.width = width;
377382
tmpCanvas.height = height;
@@ -380,7 +385,7 @@ export class PrintPDF {
380385
tmpCtx.fillRect(0, 0, width, height);
381386
const img = new Image();
382387
img.onload = () => {
383-
progressFn(.90, 'Drawing');
388+
progressFn(.95, 'Drawing');
384389
tmpCtx.drawImage(img, 0, 0, width, height);
385390
const dataURL = tmpCanvas.toDataURL('image/jpeg', 1.0);
386391
resolve(dataURL);
@@ -390,7 +395,7 @@ export class PrintPDF {
390395
});
391396

392397
}).then(dataURL => {
393-
progressFn(.95, 'Creating PDF');
398+
progressFn(.98, 'Creating PDF');
394399
const orientation = (width > height) ? 'l' : 'p';
395400
const pdf = new JsPDF(orientation, 'pt', [width, height]);
396401
const w = Math.floor(pdf.internal.pageSize.getWidth());

0 commit comments

Comments
 (0)