PluginProbe
PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin / 2.4.3
PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin v2.4.3
trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.1 All 71 releases
pdf-print / js / front-script.js

front-script.js in PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin 2.4.3, at js/front-script.js

155 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 var beforeImageToPdf, afterImageToPdf;
2
3 /* Image of page to PDF */
4 function imageToPdf() {
5 ( function( $ ) {
6
7 window.scrollTo( 0, 0 );
8
9 var deferreds = [];
10 var fileSettings = {
11 'pageSize': pdfprnt_file_settings.page_size.toLowerCase(),
12 'marginLeft': Number( pdfprnt_file_settings.margin_left ),
13 'marginRight': Number( pdfprnt_file_settings.margin_right ),
14 'marginBottom': Number( pdfprnt_file_settings.margin_bottom ),
15 'marginTop': Number( pdfprnt_file_settings.margin_top ),
16 'fileAction': pdfprnt_file_settings.file_action,
17 'fileName': pdfprnt_file_settings.file_name
18 };
19
20 var pdf = new jsPDF( 'portrait', 'px', fileSettings['pageSize'] );
21 var width = pdf.internal.pageSize.getWidth();
22 var height = pdf.internal.pageSize.getHeight();
23 // add to page Header and Footer
24 function addImageCustom( canvas, imgWidth, imgHeight ) {
25 pdf.addImage( canvas.toDataURL('image/jpeg', 1.0 ), 'JPEG',
26 fileSettings['marginLeft'],
27 fileSettings['marginTop'],
28 imgWidth, imgHeight
29 );
30 }
31
32 window.scrollTo(0, 0);
33 if ( 'undefined' != typeof beforeImageToPdf ) {
34 beforeImageToPdf();
35 }
36 /* hide default wp panel */
37 document.getElementById("wpadminbar") ? document.getElementById("wpadminbar").style.display = "none" : '';
38
39 var deferred = $.Deferred();
40 deferreds.push( deferred.promise() );
41 var canvasTop, canvasBottom;
42 generateCanvas( 'body', pdf, deferred );
43
44 function generateCanvas( selector, pdf, deferred ) {
45 html2canvas( document.querySelector( 'body' ) ).then( canvas => {
46 var contentWidth = canvas.width;
47 var contentHeight = canvas.height;
48 var headerSize = fileSettings['marginTop'] + fileSettings['marginBottom'];
49 var imgHeaderSize = ( contentWidth / width * headerSize );
50 // The height of the canvas which one pdf page can show;
51 var pageHeight = contentWidth / width * height;
52
53 // the height of canvas that haven't render to pdf
54 var leftHeight = contentHeight;
55 var imgWidth = width - fileSettings['marginLeft'] - fileSettings['marginRight'];
56 var imgHeight = width / contentWidth * contentHeight - headerSize;
57
58 if ( ( leftHeight + headerSize ) < pageHeight ) {
59 addImageCustom( canvas, imgWidth, imgHeight + headerSize );
60 } else {
61 var countPages = Math.ceil(leftHeight / pageHeight );
62 var leftHeightAfter = ( countPages * headerSize ) + leftHeight;
63
64 var canvasNew = document.createElement('canvas');
65 canvasNew.width = contentWidth;
66 //var theColorBody = $('body').css("background-color");
67 var row = 0;
68 for ( var i = 0; i < countPages - 1; i++ ) {
69 canvasNew.height = pageHeight - imgHeaderSize;
70 var context = canvasNew.getContext('2d');
71 context.drawImage( canvas, 0, i * -pageHeight + row );
72 imgHeight = ( width / contentWidth * pageHeight - headerSize );
73 let n = 0;
74 // var imgData = context.getImageData( fileSettings['marginLeft'], 0, contentWidth - fileSettings['marginRight'] - fileSettings['marginLeft'], pageHeight );
75 // let n = 1;
76 // let j = imgData.data.length - ( contentWidth - fileSettings['marginRight'] - fileSettings['marginLeft'] ) * 4 * n;
77 // let rgb;
78 // let lastPx = imgData.data.length;
79 // while ( j != lastPx ) {
80 // rgb = 'rgb(' + imgData.data[j] + ', ' + imgData.data[j + 1] + ', ' + imgData.data[j + 2] + ')';
81 // j += 4;
82 // if ( rgb != theColorBody ) {
83 // n++;
84 // j = imgData.data.length - ( contentWidth - fileSettings['marginRight'] - fileSettings['marginLeft'] ) * 4 * n;
85 // lastPx = imgData.data.length - ( contentWidth - fileSettings['marginRight'] - fileSettings['marginLeft'] ) * 4 * ( n - 1 );
86 // } else if ( n == 50 ) {
87 // break;
88 // }
89 // }
90 canvasNew.height = pageHeight - n;
91 context = canvasNew.getContext('2d');
92 context.drawImage( canvas, 0, i * -pageHeight + row );
93 addImageCustom( canvasNew, imgWidth, imgHeight );
94 leftHeight -= pageHeight;
95 pdf.addPage();
96 row = row + n;
97 }
98
99 leftHeightAfter += row;
100 var countPagesAfter = Math.ceil( leftHeightAfter / pageHeight );
101 if ( countPagesAfter != countPages ) {
102
103 canvasNew.height = pageHeight - imgHeaderSize;
104 context = canvasNew.getContext('2d');
105 context.drawImage( canvas, 0, i * -pageHeight + row );
106 addImageCustom( canvasNew, imgWidth, imgHeight );
107
108 pdf.addPage();
109 i++;
110 leftHeight -= pageHeight;
111 row += imgHeaderSize;
112 }
113
114 canvasNew.height = leftHeight + row;
115 context.drawImage( canvas, 0, i * -pageHeight + row );
116 imgHeightLast = width / contentWidth * canvasNew.height;
117 addImageCustom( canvasNew, imgWidth, imgHeightLast );
118
119 }
120 deferred.resolve();
121 });
122 }
123
124 if ( 'undefined' != typeof afterImageToPdf ) {
125 afterImageToPdf();
126 }
127 /* show default wp panel */
128 document.getElementById("wpadminbar") ? document.getElementById("wpadminbar").style.display = "block" : '';
129
130 // executes after adding all images
131 $.when.apply( $, deferreds ).then( function() {
132 if ( 'open' == fileSettings['fileAction'] ) {
133 pdf.setProperties({
134 title: fileSettings['fileName']
135 });
136 //pdf.autoPrint();
137 window.open( pdf.output('bloburl') );
138 /* IFRAME
139 var string = pdf.output('datauristring');
140 var iframe = "<head><style>*{margin: 0; padding: 0;}</style><title>" + fileSettings['fileName'] + "</title></head>" +
141 "<body><iframe width='100%' height='1000px' src='" + string + "'></iframe></body>"
142
143 var x = window.open();
144 x.document.open();
145 x.document.write( iframe );
146 x.document.close();*/
147
148 //pdf.output( 'dataurlnewwindow' );
149 } else {
150 pdf.save( fileSettings['fileName'] );
151 }
152 });
153
154 } )( jQuery );
155 }