@@ -149,10 +149,10 @@ export class PrintPDF {
149
149
return elMap ;
150
150
}
151
151
152
- public getUniqueClassName ( ) : string {
152
+ public getUniqueId ( ) : string {
153
153
this . uniqueCounter += 1 ;
154
154
155
- return `print-pdf-cn- ${ this . uniqueCounter } ` ;
155
+ return `print-pdf-${ this . uniqueCounter } ` ;
156
156
}
157
157
158
158
public traverseNodes ( o : HTMLElement , d : HTMLElement , applyFn : ( n1 : HTMLElement , n2 : HTMLElement ) => void ) {
@@ -254,16 +254,16 @@ export class PrintPDF {
254
254
. forEach ( ( propertyName : string ) => {
255
255
const propValue = computedStyle . getPropertyValue ( propertyName ) ;
256
256
if ( selector !== '' || defaultMap . get ( propertyName ) !== propValue ) {
257
- textPairs . push ( `\t${ propertyName } : ${ propValue } !important ;` ) ;
257
+ textPairs . push ( `\t${ propertyName } : ${ propValue } ;` ) ;
258
258
}
259
259
} ) ;
260
260
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 } ` ;
263
264
}
264
- const className = dst . className ;
265
265
266
- return `\n. ${ className } ${ selector } {\n${ cssText } \n}\n` ;
266
+ return `\n# ${ dst . id } ${ selector } {\n${ cssText } \n}\n` ;
267
267
}
268
268
269
269
public toPDF ( progressFn : ( percentComplete : number , statusMsg : string ) => void = ( ) => { return ; } ) : Promise < JsPDF > {
@@ -292,32 +292,41 @@ export class PrintPDF {
292
292
return this . copyFontFaces ( globalStyle , progressFn ) ;
293
293
} ) . then ( ( ) => {
294
294
let counter = 0 ;
295
- const copySubTaskPercent = .6 ;
295
+ const copySubTaskPercent = .65 ;
296
296
const nodePromises = [ ] ;
297
297
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
+ } ) ;
321
330
} ) ;
322
331
323
332
return Promise . all ( nodePromises ) ;
@@ -331,10 +340,6 @@ export class PrintPDF {
331
340
copiedStyles . push ( styleText ) ;
332
341
} ) ;
333
342
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
- } ) ;
338
343
const stylesText = copiedStyles . join ( '' ) ;
339
344
const textNode = this . doc . createTextNode ( stylesText ) ;
340
345
globalStyle . appendChild ( textNode ) ;
@@ -351,16 +356,16 @@ export class PrintPDF {
351
356
352
357
return Promise . all ( promises ) ;
353
358
} ) . then ( ( ) => {
354
- progressFn ( .80 , 'Appending Master StyleSheet' ) ;
359
+ progressFn ( .85 , 'Appending Master StyleSheet' ) ;
355
360
copyElement . appendChild ( globalStyle ) ;
356
361
357
362
return copyElement ;
358
363
} ) . then ( copy => {
359
- progressFn ( .83 , 'Serializing' ) ;
364
+ progressFn ( .87 , 'Serializing' ) ;
360
365
361
366
return this . serializer . serializeToString ( copy ) ;
362
367
} ) . then ( serialized => {
363
- progressFn ( .84 , 'Encoding to Data URI' ) ;
368
+ progressFn ( .88 , 'Encoding to Data URI' ) ;
364
369
const encoded = encodeURIComponent ( serialized ) ;
365
370
const foreignObject = `<foreignObject width='100%' height='100%'>${ encoded } </foreignObject>` ;
366
371
const namespace = 'http://www.w3.org/2000/svg' ;
@@ -371,7 +376,7 @@ export class PrintPDF {
371
376
372
377
return new Promise ( resolve => {
373
378
this . win . setTimeout ( ( ) => {
374
- progressFn ( .85 , 'Creating Canvas' ) ;
379
+ progressFn ( .90 , 'Creating Canvas' ) ;
375
380
const tmpCanvas = this . doc . createElement ( 'canvas' ) ;
376
381
tmpCanvas . width = width ;
377
382
tmpCanvas . height = height ;
@@ -380,7 +385,7 @@ export class PrintPDF {
380
385
tmpCtx . fillRect ( 0 , 0 , width , height ) ;
381
386
const img = new Image ( ) ;
382
387
img . onload = ( ) => {
383
- progressFn ( .90 , 'Drawing' ) ;
388
+ progressFn ( .95 , 'Drawing' ) ;
384
389
tmpCtx . drawImage ( img , 0 , 0 , width , height ) ;
385
390
const dataURL = tmpCanvas . toDataURL ( 'image/jpeg' , 1.0 ) ;
386
391
resolve ( dataURL ) ;
@@ -390,7 +395,7 @@ export class PrintPDF {
390
395
} ) ;
391
396
392
397
} ) . then ( dataURL => {
393
- progressFn ( .95 , 'Creating PDF' ) ;
398
+ progressFn ( .98 , 'Creating PDF' ) ;
394
399
const orientation = ( width > height ) ? 'l' : 'p' ;
395
400
const pdf = new JsPDF ( orientation , 'pt' , [ width , height ] ) ;
396
401
const w = Math . floor ( pdf . internal . pageSize . getWidth ( ) ) ;
0 commit comments