Skip to content

Commit aed93c3

Browse files
committed
addHTML() can now split the canvas into multiple pages; Also, added rasterizeHTML support
1 parent 99b632e commit aed93c3

File tree

7 files changed

+132
-33
lines changed

7 files changed

+132
-33
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jspdf",
3-
"version": "1.0.133",
3+
"version": "1.0.138",
44
"homepage": "https://github.com/mrrio/jspdf",
55
"description": "PDF Document creation from JavaScript",
66
"main": "dist/jspdf.min.js",

build.sh

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ if [ "$git_push" = "1" ]; then
9090
fi
9191
if [ "$git_tag" = "1" ]; then
9292
commit=`git rev-parse --short=10 HEAD`
93+
git history
9394
git tag -m "${build} ${commit}" -s v${version}
9495
git push upstream v${version}
9596
fi

dist/jspdf.debug.js

+64-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @preserve
22
* jsPDF - PDF Document creation from JavaScript
3-
* Version 1.0.133-git Built on 2014-05-13T00:23
4-
* CommitID 4952bd270a
3+
* Version 1.0.138-git Built on 2014-05-18T18:42
4+
* CommitID 99b632ed34
55
*
66
* Copyright (c) 2010-2014 James Hall, https://github.com/MrRio/jsPDF
77
* 2010 Aaron Spike, https://github.com/acspike
@@ -1693,9 +1693,9 @@ var jsPDF = (function(global) {
16931693
* pdfdoc.mymethod() // <- !!!!!!
16941694
*/
16951695
jsPDF.API = {events:[]};
1696-
jsPDF.version = "1.0.133-debug 2014-05-13T00:23:diegocr";
1696+
jsPDF.version = "1.0.138-debug 2014-05-18T18:42:diegocr";
16971697

1698-
if (typeof define === 'function') {
1698+
if (typeof define === 'function' && define.amd) {
16991699
define(function() {
17001700
return jsPDF;
17011701
});
@@ -1719,6 +1719,7 @@ var jsPDF = (function(global) {
17191719
* Renders an HTML element to canvas object which added as an image to the PDF
17201720
*
17211721
* This PlugIn requires html2canvas: https://github.com/niklasvh/html2canvas
1722+
* OR rasterizeHTML: https://github.com/cburgmer/rasterizeHTML.js
17221723
*
17231724
* @public
17241725
* @function
@@ -1735,8 +1736,10 @@ var jsPDF = (function(global) {
17351736
jsPDFAPI.addHTML = function (element, x, y, options, callback) {
17361737
'use strict';
17371738

1738-
if(typeof html2canvas === 'undefined')
1739-
throw new Error('You need this: https://github.com/niklasvh/html2canvas');
1739+
if(typeof html2canvas === 'undefined' && typeof rasterizeHTML === 'undefined')
1740+
throw new Error('You need either '
1741+
+'https://github.com/niklasvh/html2canvas'
1742+
+' or https://github.com/cburgmer/rasterizeHTML.js');
17401743

17411744
if(typeof x !== 'number') {
17421745
options = x;
@@ -1748,27 +1751,73 @@ var jsPDF = (function(global) {
17481751
options = null;
17491752
}
17501753

1754+
var I = this.internal, K = I.scaleFactor, W = I.pageSize.width, H = I.pageSize.height;
1755+
17511756
options = options || {};
1752-
options.onrendered = function(canvas) {
1757+
options.onrendered = function(obj) {
17531758
x = parseInt(x) || 0;
17541759
y = parseInt(y) || 0;
17551760
var dim = options.dim || {};
17561761
var h = dim.h || 0;
1757-
var w = dim.w || Math.min(this.internal.pageSize.width,canvas.width/this.internal.scaleFactor) - x;
1762+
var w = dim.w || Math.min(W,obj.width/K) - x;
17581763

17591764
var format = 'JPEG';
17601765
if(options.format)
17611766
format = options.format;
17621767

1763-
var alias = Math.random().toString(35);
1764-
var args = [canvas, x,y,w,h, format,alias,'SLOW'];
1768+
if(obj.height > H && options.pagesplit) {
1769+
var crop = function() {
1770+
var cy = 0;
1771+
while(1) {
1772+
var canvas = document.createElement('canvas');
1773+
canvas.width = Math.min(W*K,obj.width);
1774+
canvas.height = Math.min(H*K,obj.height-cy);
1775+
var ctx = canvas.getContext('2d');
1776+
ctx.drawImage(obj,0,cy,obj.width,canvas.height,0,0,canvas.width,canvas.height);
1777+
var args = [canvas, x,cy?0:y,canvas.width/K,canvas.height/K, format,null,'SLOW'];
1778+
this.addImage.apply(this, args);
1779+
cy += canvas.height;
1780+
if(cy >= obj.height) break;
1781+
this.addPage();
1782+
}
1783+
callback(w,cy,null,args);
1784+
}.bind(this);
1785+
if(obj.nodeName === 'CANVAS') {
1786+
var img = new Image();
1787+
img.onload = crop;
1788+
img.src = obj.toDataURL("image/png");
1789+
obj = img;
1790+
} else {
1791+
crop();
1792+
}
1793+
} else {
1794+
var alias = Math.random().toString(35);
1795+
var args = [obj, x,y,w,h, format,alias,'SLOW'];
17651796

1766-
this.addImage.apply(this, args);
1797+
this.addImage.apply(this, args);
17671798

1768-
callback(w,h,alias,args);
1799+
callback(w,h,alias,args);
1800+
}
17691801
}.bind(this);
17701802

1771-
return html2canvas(element, options);
1803+
if(typeof html2canvas !== 'undefined' && !options.rstz) {
1804+
return html2canvas(element, options);
1805+
}
1806+
1807+
if(typeof rasterizeHTML !== 'undefined') {
1808+
var meth = 'drawDocument';
1809+
if(typeof element === 'string') {
1810+
meth = /^http/.test(element) ? 'drawURL' : 'drawHTML';
1811+
}
1812+
options.width = options.width || (W*K);
1813+
return rasterizeHTML[meth](element, void 0, options).then(function(r) {
1814+
options.onrendered(r.image);
1815+
}, function(e) {
1816+
callback(null,e);
1817+
});
1818+
}
1819+
1820+
return null;
17721821
};
17731822
})(jsPDF.API);
17741823
/** @preserve
@@ -5004,10 +5053,10 @@ jsPDFAPI.putTotalPages = function(pageExpression) {
50045053
/*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */
50055054

50065055
if (!(typeof Blob === "function" || typeof Blob === "object") || typeof URL === "undefined")
5007-
if ((typeof Blob === "function" || typeof Blob === "object") && typeof webkitURL !== "undefined") self.URL = webkitURL;
5008-
else var Blob = (function (view) {
5056+
window.Blob = (function (view) {
50095057
"use strict";
50105058

5059+
view.URL = view.URL || view.webkitURL;
50115060
var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || view.MSBlobBuilder || (function(view) {
50125061
var
50135062
get_class = function(object) {

dist/jspdf.min.js

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jspdf.plugin.addhtml.js

+58-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Renders an HTML element to canvas object which added as an image to the PDF
1414
*
1515
* This PlugIn requires html2canvas: https://github.com/niklasvh/html2canvas
16+
* OR rasterizeHTML: https://github.com/cburgmer/rasterizeHTML.js
1617
*
1718
* @public
1819
* @function
@@ -29,8 +30,10 @@
2930
jsPDFAPI.addHTML = function (element, x, y, options, callback) {
3031
'use strict';
3132

32-
if(typeof html2canvas === 'undefined')
33-
throw new Error('You need this: https://github.com/niklasvh/html2canvas');
33+
if(typeof html2canvas === 'undefined' && typeof rasterizeHTML === 'undefined')
34+
throw new Error('You need either '
35+
+'https://github.com/niklasvh/html2canvas'
36+
+' or https://github.com/cburgmer/rasterizeHTML.js');
3437

3538
if(typeof x !== 'number') {
3639
options = x;
@@ -42,26 +45,72 @@
4245
options = null;
4346
}
4447

48+
var I = this.internal, K = I.scaleFactor, W = I.pageSize.width, H = I.pageSize.height;
49+
4550
options = options || {};
46-
options.onrendered = function(canvas) {
51+
options.onrendered = function(obj) {
4752
x = parseInt(x) || 0;
4853
y = parseInt(y) || 0;
4954
var dim = options.dim || {};
5055
var h = dim.h || 0;
51-
var w = dim.w || Math.min(this.internal.pageSize.width,canvas.width/this.internal.scaleFactor) - x;
56+
var w = dim.w || Math.min(W,obj.width/K) - x;
5257

5358
var format = 'JPEG';
5459
if(options.format)
5560
format = options.format;
5661

57-
var alias = Math.random().toString(35);
58-
var args = [canvas, x,y,w,h, format,alias,'SLOW'];
62+
if(obj.height > H && options.pagesplit) {
63+
var crop = function() {
64+
var cy = 0;
65+
while(1) {
66+
var canvas = document.createElement('canvas');
67+
canvas.width = Math.min(W*K,obj.width);
68+
canvas.height = Math.min(H*K,obj.height-cy);
69+
var ctx = canvas.getContext('2d');
70+
ctx.drawImage(obj,0,cy,obj.width,canvas.height,0,0,canvas.width,canvas.height);
71+
var args = [canvas, x,cy?0:y,canvas.width/K,canvas.height/K, format,null,'SLOW'];
72+
this.addImage.apply(this, args);
73+
cy += canvas.height;
74+
if(cy >= obj.height) break;
75+
this.addPage();
76+
}
77+
callback(w,cy,null,args);
78+
}.bind(this);
79+
if(obj.nodeName === 'CANVAS') {
80+
var img = new Image();
81+
img.onload = crop;
82+
img.src = obj.toDataURL("image/png");
83+
obj = img;
84+
} else {
85+
crop();
86+
}
87+
} else {
88+
var alias = Math.random().toString(35);
89+
var args = [obj, x,y,w,h, format,alias,'SLOW'];
5990

60-
this.addImage.apply(this, args);
91+
this.addImage.apply(this, args);
6192

62-
callback(w,h,alias,args);
93+
callback(w,h,alias,args);
94+
}
6395
}.bind(this);
6496

65-
return html2canvas(element, options);
97+
if(typeof html2canvas !== 'undefined' && !options.rstz) {
98+
return html2canvas(element, options);
99+
}
100+
101+
if(typeof rasterizeHTML !== 'undefined') {
102+
var meth = 'drawDocument';
103+
if(typeof element === 'string') {
104+
meth = /^http/.test(element) ? 'drawURL' : 'drawHTML';
105+
}
106+
options.width = options.width || (W*K);
107+
return rasterizeHTML[meth](element, void 0, options).then(function(r) {
108+
options.onrendered(r.image);
109+
}, function(e) {
110+
callback(null,e);
111+
});
112+
}
113+
114+
return null;
66115
};
67116
})(jsPDF.API);

libs/Blob.js

Submodule Blob.js updated 1 file

libs/FileSaver.js

0 commit comments

Comments
 (0)