forked from parallax/jsPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjspdf.plugin.addhtml.js
67 lines (56 loc) · 1.92 KB
/
jspdf.plugin.addhtml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* jsPDF addHTML PlugIn
* Copyright (c) 2014 Diego Casorran
*
* Licensed under the MIT License.
* http://opensource.org/licenses/mit-license
*/
(function (jsPDFAPI) {
'use strict';
/**
* Renders an HTML element to canvas object which added as an image to the PDF
*
* This PlugIn requires html2canvas: https://github.com/niklasvh/html2canvas
*
* @public
* @function
* @param element {Mixed} HTML Element, or anything supported by html2canvas.
* @param x {Number} starting X coordinate in jsPDF instance's declared units.
* @param y {Number} starting Y coordinate in jsPDF instance's declared units.
* @param options {Object} Additional options, check the code below.
* @param callback {Function} to call when the rendering has finished.
*
* NOTE: Every parameter is optional except 'element' and 'callback', in such
* case the image is positioned at 0x0 covering the whole PDF document
* size. Ie, to easily take screenshoots of webpages saving them to PDF.
*/
jsPDFAPI.addHTML = function (element, x, y, options, callback) {
'use strict';
if(typeof html2canvas === 'undefined')
throw new Error('You need this: https://github.com/niklasvh/html2canvas');
if(typeof x !== 'number') {
options = x;
callback = y;
}
if(typeof options === 'function') {
callback = options;
options = null;
}
options = options || {};
options.onrendered = function(canvas) {
x = parseInt(x) || 0;
y = parseInt(y) || 0;
var dim = options.dim || {};
var h = dim.h || 0;
var w = dim.w || Math.min(this.internal.pageSize.width,canvas.width/this.internal.scaleFactor) - x;
var format = 'JPEG';
if(options.format)
format = options.format;
var alias = Math.random().toString(35);
var args = [canvas, x,y,w,h, format,alias,'SLOW'];
this.addImage.apply(this, args);
callback(w,h,alias,args);
}.bind(this);
return html2canvas(element, options);
};
})(jsPDF.API);