@@ -178,6 +178,7 @@ var jsPDF = (function(global) {
178
178
tmp ,
179
179
page = 0 ,
180
180
pages = [ ] ,
181
+ pagedim = { } ,
181
182
content = [ ] ,
182
183
lineCapID = 0 ,
183
184
lineJoinID = 0 ,
@@ -229,7 +230,7 @@ var jsPDF = (function(global) {
229
230
out ( 'endstream' ) ;
230
231
} ,
231
232
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 ;
233
234
234
235
adler32cs = global . adler32cs || jsPDF . adler32cs ;
235
236
if ( compress && typeof adler32cs === 'undefined' ) {
@@ -240,9 +241,12 @@ var jsPDF = (function(global) {
240
241
241
242
for ( n = 1 ; n <= page ; n ++ ) {
242
243
newObject ( ) ;
244
+ wPt = ( pageWidth = pagedim [ n ] . width ) * k ;
245
+ hPt = ( pageHeight = pagedim [ n ] . height ) * k ;
243
246
out ( '<</Type /Page' ) ;
244
247
out ( '/Parent 1 0 R' ) ;
245
248
out ( '/Resources 2 0 R' ) ;
249
+ out ( '/MediaBox [0 0 ' + f2 ( wPt ) + ' ' + f2 ( hPt ) + ']' ) ;
246
250
out ( '/Contents ' + ( objectNumber + 1 ) + ' 0 R>>' ) ;
247
251
out ( 'endobj' ) ;
248
252
@@ -280,7 +284,6 @@ var jsPDF = (function(global) {
280
284
}
281
285
out ( kids + ']' ) ;
282
286
out ( '/Count ' + page ) ;
283
- out ( '/MediaBox [0 0 ' + f2 ( wPt ) + ' ' + f2 ( hPt ) + ']' ) ;
284
287
out ( '>>' ) ;
285
288
out ( 'endobj' ) ;
286
289
} ,
@@ -614,14 +617,36 @@ var jsPDF = (function(global) {
614
617
out ( '/Root ' + objectNumber + ' 0 R' ) ;
615
618
out ( '/Info ' + ( objectNumber - 1 ) + ' 0 R' ) ;
616
619
} ,
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
+ }
620
641
outToPages = true ;
621
- pages [ page ] = [ ] ;
642
+ pages [ ++ page ] = [ ] ;
643
+ pagedim [ page ] = {
644
+ width : ( pageWidth = Number ( width ) || pageWidth ) ,
645
+ height : ( pageHeight = Number ( height ) || pageHeight )
646
+ } ;
622
647
} ,
623
648
_addPage = function ( ) {
624
- beginPage ( ) ;
649
+ beginPage . apply ( this , arguments ) ;
625
650
// Set line width
626
651
out ( f2 ( lineWidth * k ) + ' w' ) ;
627
652
// Set draw color
@@ -813,37 +838,6 @@ var jsPDF = (function(global) {
813
838
throw ( 'Invalid unit: ' + unit ) ;
814
839
}
815
840
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
-
847
841
//---------------------------------------
848
842
// Public API
849
843
@@ -892,8 +886,12 @@ var jsPDF = (function(global) {
892
886
// through multiplication.
893
887
'scaleFactor' : k ,
894
888
'pageSize' : {
895
- 'width' : pageWidth ,
896
- 'height' : pageHeight
889
+ get width ( ) {
890
+ return pageWidth
891
+ } ,
892
+ get height ( ) {
893
+ return pageHeight
894
+ }
897
895
} ,
898
896
'output' : function ( type , options ) {
899
897
return output ( type , options ) ;
@@ -913,7 +911,7 @@ var jsPDF = (function(global) {
913
911
* @name addPage
914
912
*/
915
913
API . addPage = function ( ) {
916
- _addPage ( ) ;
914
+ _addPage . apply ( this , arguments ) ;
917
915
return this ;
918
916
} ;
919
917
@@ -1675,7 +1673,7 @@ var jsPDF = (function(global) {
1675
1673
// Add the first page automatically
1676
1674
addFonts ( ) ;
1677
1675
activeFontKey = 'F1' ;
1678
- _addPage ( ) ;
1676
+ _addPage ( format , orientation ) ;
1679
1677
1680
1678
events . publish ( 'initialized' ) ;
1681
1679
return API ;
0 commit comments