PluginProbe
PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin / 2.4.0
PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin v2.4.0
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.0, at js/front-script.js

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