@@ -22,7 +22,6 @@ import AttachmentsMixin from './mixins/attachments';
2222import LineWrapper from './line_wrapper' ;
2323import SubsetMixin from './mixins/subsets' ;
2424import MetadataMixin from './mixins/metadata' ;
25- import capitalize from './utils/capitalize' ;
2625
2726class PDFDocument extends stream . Readable {
2827 constructor ( options = { } ) {
@@ -61,7 +60,6 @@ class PDFDocument extends stream.Readable {
6160 this . _waiting = 0 ;
6261 this . _ended = false ;
6362 this . _offset = 0 ;
64-
6563 const Pages = this . ref ( {
6664 Type : 'Pages' ,
6765 Count : 0 ,
@@ -82,26 +80,19 @@ class PDFDocument extends stream.Readable {
8280 this . _root . data . Lang = new String ( this . options . lang ) ;
8381 }
8482
85- if ( this . options . pageLayout ) {
86- this . _root . data . PageLayout = capitalize ( this . options . pageLayout ) ;
87- }
88-
89- if ( this . options . pageMode ) {
90- this . _root . data . PageMode = capitalize ( this . options . pageMode ) ;
91- }
92-
9383 // The current page
9484 this . page = null ;
9585
9686 // Initialize mixins
87+ this . initMetadata ( ) ;
9788 this . initColor ( ) ;
9889 this . initVector ( ) ;
99- this . initFonts ( ) ;
90+ this . initFonts ( options . font ) ;
10091 this . initText ( ) ;
10192 this . initImages ( ) ;
10293 this . initOutline ( ) ;
103- this . initSubset ( options ) ;
10494 this . initMarkings ( options ) ;
95+ this . initSubset ( options ) ;
10596
10697 // Initialize the metadata
10798 this . info = {
@@ -129,7 +120,8 @@ class PDFDocument extends stream.Readable {
129120 // Initialize security settings
130121 // this._security = PDFSecurity.create(this, options);
131122
132- // Write the header PDF version
123+ // Write the header
124+ // PDF version
133125 this . _write ( `%PDF-${ this . version } ` ) ;
134126
135127 // 4 binary chars, as recommended by the spec
@@ -142,7 +134,6 @@ class PDFDocument extends stream.Readable {
142134 }
143135
144136 addPage ( options ) {
145- // end the current page if needed
146137 if ( options == null ) {
147138 ( { options } = this ) ;
148139 }
@@ -161,12 +152,16 @@ class PDFDocument extends stream.Readable {
161152 pages . Kids . push ( this . page . dictionary ) ;
162153 pages . Count ++ ;
163154
155+ // reset x and y coordinates
156+ this . x = this . page . margins . left ;
157+ this . y = this . page . margins . top ;
158+
164159 // flip PDF coordinate system so that the origin is in
165160 // the top left rather than the bottom left
166161 this . _ctm = [ 1 , 0 , 0 , 1 , 0 , 0 ] ;
167162 this . transform ( 1 , 0 , 0 , - 1 , 0 , this . page . height ) ;
168163
169- // this.emit('pageAdded');
164+ this . emit ( 'pageAdded' ) ;
170165
171166 return this ;
172167 }
@@ -181,13 +176,30 @@ class PDFDocument extends stream.Readable {
181176 return this ;
182177 }
183178
179+ bufferedPageRange ( ) {
180+ return { start : this . _pageBufferStart , count : this . _pageBuffer . length } ;
181+ }
182+
183+ switchToPage ( n ) {
184+ let page ;
185+ if ( ! ( page = this . _pageBuffer [ n - this . _pageBufferStart ] ) ) {
186+ throw new Error (
187+ `switchToPage(${ n } ) out of bounds, current buffer covers pages ${
188+ this . _pageBufferStart
189+ } to ${ this . _pageBufferStart + this . _pageBuffer . length - 1 } `
190+ ) ;
191+ }
192+
193+ return ( this . page = page ) ;
194+ }
195+
184196 flushPages ( ) {
185197 // this local variable exists so we're future-proof against
186198 // reentrant calls to flushPages.
187199 const pages = this . _pageBuffer ;
188200 this . _pageBuffer = [ ] ;
189201 this . _pageBufferStart += pages . length ;
190- for ( let page of Array . from ( pages ) ) {
202+ for ( let page of pages ) {
191203 this . endPageMarkings ( page ) ;
192204 page . end ( ) ;
193205 }
@@ -234,9 +246,8 @@ class PDFDocument extends stream.Readable {
234246 return ref ;
235247 }
236248
237- _read ( ) {
238- // do nothing, but this method is required by node
239- }
249+ _read ( ) { }
250+ // do nothing, but this method is required by node
240251
241252 _write ( data ) {
242253 if ( ! Buffer . isBuffer ( data ) ) {
@@ -262,6 +273,7 @@ class PDFDocument extends stream.Readable {
262273
263274 end ( ) {
264275 this . flushPages ( ) ;
276+
265277 this . _info = this . ref ( ) ;
266278 for ( let key in this . info ) {
267279 let val = this . info [ key ] ;
@@ -289,6 +301,8 @@ class PDFDocument extends stream.Readable {
289301 this . endSubset ( ) ;
290302 }
291303
304+ this . endMetadata ( ) ;
305+
292306 this . _root . end ( ) ;
293307 this . _root . data . Pages . end ( ) ;
294308 this . _root . data . Names . end ( ) ;
@@ -298,15 +312,15 @@ class PDFDocument extends stream.Readable {
298312 this . _root . data . ViewerPreferences . end ( ) ;
299313 }
300314
301- // if (this._security) {
302- // this._security.end();
303- // }
315+ if ( this . _security ) {
316+ this . _security . end ( ) ;
317+ }
304318
305319 if ( this . _waiting === 0 ) {
306320 return this . _finalize ( ) ;
321+ } else {
322+ return ( this . _ended = true ) ;
307323 }
308-
309- this . _ended = true ;
310324 }
311325
312326 _finalize ( ) {
@@ -316,7 +330,7 @@ class PDFDocument extends stream.Readable {
316330 this . _write ( `0 ${ this . _offsets . length + 1 } ` ) ;
317331 this . _write ( '0000000000 65535 f ' ) ;
318332
319- for ( let offset of Array . from ( this . _offsets ) ) {
333+ for ( let offset of this . _offsets ) {
320334 offset = `0000000000${ offset } ` . slice ( - 10 ) ;
321335 this . _write ( offset + ' 00000 n ' ) ;
322336 }
@@ -328,10 +342,9 @@ class PDFDocument extends stream.Readable {
328342 Info : this . _info ,
329343 ID : [ this . _id , this . _id ]
330344 } ;
331-
332- // if (this._security) {
333- // trailer.Encrypt = this._security.dictionary;
334- // }
345+ if ( this . _security ) {
346+ trailer . Encrypt = this . _security . dictionary ;
347+ }
335348
336349 this . _write ( 'trailer' ) ;
337350 this . _write ( PDFObject . convert ( trailer ) ) ;
@@ -353,7 +366,6 @@ const mixin = (methods) => {
353366 Object . assign ( PDFDocument . prototype , methods ) ;
354367} ;
355368
356- // Load mixins
357369mixin ( MetadataMixin ) ;
358370mixin ( ColorMixin ) ;
359371mixin ( VectorMixin ) ;
0 commit comments