Skip to content

Commit 2b53fb4

Browse files
committed
Added per-page support for document width, height, format and orientation - #49
1 parent 3da4b59 commit 2b53fb4

File tree

2 files changed

+41
-43
lines changed

2 files changed

+41
-43
lines changed

examples/js/two-page.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var doc = new jsPDF();
22
doc.text(20, 20, 'Hello world!');
33
doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');
4-
doc.addPage();
4+
doc.addPage('a6','l');
55
doc.text(20, 20, 'Do you like that?');

jspdf.js

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ var jsPDF = (function(global) {
178178
tmp,
179179
page = 0,
180180
pages = [],
181+
pagedim = {},
181182
content = [],
182183
lineCapID = 0,
183184
lineJoinID = 0,
@@ -229,7 +230,7 @@ var jsPDF = (function(global) {
229230
out('endstream');
230231
},
231232
putPages = function() {
232-
var n,p,arr,i,deflater,adler32,wPt = pageWidth * k, hPt = pageHeight * k, adler32cs;
233+
var n,p,arr,i,deflater,adler32,adler32cs,wPt,hPt;
233234

234235
adler32cs = global.adler32cs || jsPDF.adler32cs;
235236
if (compress && typeof adler32cs === 'undefined') {
@@ -240,9 +241,12 @@ var jsPDF = (function(global) {
240241

241242
for (n = 1; n <= page; n++) {
242243
newObject();
244+
wPt = (pageWidth = pagedim[n].width) * k;
245+
hPt = (pageHeight = pagedim[n].height) * k;
243246
out('<</Type /Page');
244247
out('/Parent 1 0 R');
245248
out('/Resources 2 0 R');
249+
out('/MediaBox [0 0 ' + f2(wPt) + ' ' + f2(hPt) + ']');
246250
out('/Contents ' + (objectNumber + 1) + ' 0 R>>');
247251
out('endobj');
248252

@@ -280,7 +284,6 @@ var jsPDF = (function(global) {
280284
}
281285
out(kids + ']');
282286
out('/Count ' + page);
283-
out('/MediaBox [0 0 ' + f2(wPt) + ' ' + f2(hPt) + ']');
284287
out('>>');
285288
out('endobj');
286289
},
@@ -614,14 +617,36 @@ var jsPDF = (function(global) {
614617
out('/Root ' + objectNumber + ' 0 R');
615618
out('/Info ' + (objectNumber - 1) + ' 0 R');
616619
},
617-
beginPage = function() {
618-
page++;
619-
// Do dimension stuff
620+
beginPage = function(width,height) {
621+
// Dimensions are stored as user units and converted to points on output
622+
var orientation = typeof height === 'string' && height.toLowerCase();
623+
if (typeof width === 'string') {
624+
var format = width.toLowerCase();
625+
if (pageFormats.hasOwnProperty(format)) {
626+
width = pageFormats[format][0] / k;
627+
height = pageFormats[format][1] / k;
628+
}
629+
}
630+
if (Array.isArray(width)) {
631+
height = width[1];
632+
width = width[0];
633+
}
634+
if (orientation) {
635+
switch(orientation.substr(0,1)) {
636+
case 'l': if (height > width ) orientation = 's'; break;
637+
case 'p': if (width > height ) orientation = 's'; break;
638+
}
639+
if (orientation === 's') { tmp = width; width = height; height = tmp; }
640+
}
620641
outToPages = true;
621-
pages[page] = [];
642+
pages[++page] = [];
643+
pagedim[page] = {
644+
width : (pageWidth = Number(width) || pageWidth),
645+
height : (pageHeight = Number(height) || pageHeight)
646+
};
622647
},
623648
_addPage = function() {
624-
beginPage();
649+
beginPage.apply(this, arguments);
625650
// Set line width
626651
out(f2(lineWidth * k) + ' w');
627652
// Set draw color
@@ -813,37 +838,6 @@ var jsPDF = (function(global) {
813838
throw ('Invalid unit: ' + unit);
814839
}
815840

816-
// Dimensions are stored as user units and converted to points on output
817-
if (pageFormats.hasOwnProperty(format_as_string)) {
818-
pageHeight = pageFormats[format_as_string][1] / k;
819-
pageWidth = pageFormats[format_as_string][0] / k;
820-
} else {
821-
try {
822-
pageHeight = format[1];
823-
pageWidth = format[0];
824-
} catch (err) {
825-
throw new Error('Invalid format: ' + format);
826-
}
827-
}
828-
829-
if (orientation === 'p' || orientation === 'portrait') {
830-
orientation = 'p';
831-
if (pageWidth > pageHeight) {
832-
tmp = pageWidth;
833-
pageWidth = pageHeight;
834-
pageHeight = tmp;
835-
}
836-
} else if (orientation === 'l' || orientation === 'landscape') {
837-
orientation = 'l';
838-
if (pageHeight > pageWidth) {
839-
tmp = pageWidth;
840-
pageWidth = pageHeight;
841-
pageHeight = tmp;
842-
}
843-
} else {
844-
throw('Invalid orientation: ' + orientation);
845-
}
846-
847841
//---------------------------------------
848842
// Public API
849843

@@ -892,8 +886,12 @@ var jsPDF = (function(global) {
892886
// through multiplication.
893887
'scaleFactor' : k,
894888
'pageSize' : {
895-
'width' : pageWidth,
896-
'height' : pageHeight
889+
get width() {
890+
return pageWidth
891+
},
892+
get height() {
893+
return pageHeight
894+
}
897895
},
898896
'output' : function(type, options) {
899897
return output(type, options);
@@ -913,7 +911,7 @@ var jsPDF = (function(global) {
913911
* @name addPage
914912
*/
915913
API.addPage = function() {
916-
_addPage();
914+
_addPage.apply(this, arguments);
917915
return this;
918916
};
919917

@@ -1675,7 +1673,7 @@ var jsPDF = (function(global) {
16751673
// Add the first page automatically
16761674
addFonts();
16771675
activeFontKey = 'F1';
1678-
_addPage();
1676+
_addPage(format, orientation);
16791677

16801678
events.publish('initialized');
16811679
return API;

0 commit comments

Comments
 (0)