1
1
import zlib from 'zlib' ;
2
- import stream from 'stream' ;
3
2
import PDFObject from './object' ;
4
3
5
- class PDFReference extends stream . Writable {
6
- constructor ( document , id , data ) {
7
- super ( { decodeStrings : false } ) ;
8
-
9
- this . finalize = this . finalize . bind ( this ) ;
4
+ class PDFReference {
5
+ constructor ( document , id , data = { } ) {
10
6
this . document = document ;
11
7
this . id = id ;
12
- if ( data == null ) {
13
- data = { } ;
14
- }
15
8
this . data = data ;
16
-
17
9
this . gen = 0 ;
18
- this . deflate = null ;
19
10
this . compress = this . document . compress && ! this . data . Filter ;
20
11
this . uncompressedLength = 0 ;
21
- this . chunks = [ ] ;
12
+ this . buffer = [ ] ;
22
13
}
23
14
24
- initDeflate ( ) {
25
- this . data . Filter = 'FlateDecode' ;
26
-
27
- this . deflate = zlib . createDeflate ( ) ;
28
- this . deflate . on ( 'data' , ( chunk ) => {
29
- this . chunks . push ( chunk ) ;
30
- return ( this . data . Length += chunk . length ) ;
31
- } ) ;
32
-
33
- return this . deflate . on ( 'end' , this . finalize ) ;
34
- }
35
-
36
- _write ( chunk , encoding , callback ) {
37
- if ( ! ( chunk instanceof Uint8Array ) ) {
15
+ write ( chunk ) {
16
+ if ( ! Buffer . isBuffer ( chunk ) ) {
38
17
chunk = Buffer . from ( chunk + '\n' , 'binary' ) ;
39
18
}
40
19
41
20
this . uncompressedLength += chunk . length ;
42
21
if ( this . data . Length == null ) {
43
22
this . data . Length = 0 ;
44
23
}
45
-
24
+ this . buffer . push ( chunk ) ;
25
+ this . data . Length += chunk . length ;
46
26
if ( this . compress ) {
47
- if ( ! this . deflate ) {
48
- this . initDeflate ( ) ;
49
- }
50
- this . deflate . write ( chunk ) ;
51
- } else {
52
- this . chunks . push ( chunk ) ;
53
- this . data . Length += chunk . length ;
27
+ return ( this . data . Filter = 'FlateDecode' ) ;
54
28
}
55
-
56
- return callback ( ) ;
57
29
}
58
30
59
- end ( ) {
60
- super . end ( ...arguments ) ;
61
-
62
- if ( this . deflate ) {
63
- return this . deflate . end ( ) ;
31
+ end ( chunk ) {
32
+ if ( chunk ) {
33
+ this . write ( chunk ) ;
64
34
}
65
-
66
35
return this . finalize ( ) ;
67
36
}
68
37
@@ -72,23 +41,34 @@ class PDFReference extends stream.Writable {
72
41
const encryptFn = this . document . _security
73
42
? this . document . _security . getEncryptFn ( this . id , this . gen )
74
43
: null ;
44
+
45
+ if ( this . buffer . length ) {
46
+ this . buffer = Buffer . concat ( this . buffer ) ;
47
+ if ( this . compress ) {
48
+ this . buffer = zlib . deflateSync ( this . buffer ) ;
49
+ }
50
+
51
+ if ( encryptFn ) {
52
+ this . buffer = encryptFn ( this . buffer ) ;
53
+ }
54
+
55
+ this . data . Length = this . buffer . length ;
56
+ }
57
+
75
58
this . document . _write ( `${ this . id } ${ this . gen } obj` ) ;
76
59
this . document . _write ( PDFObject . convert ( this . data , encryptFn ) ) ;
77
60
78
- if ( this . chunks . length ) {
61
+ if ( this . buffer . length ) {
79
62
this . document . _write ( 'stream' ) ;
80
- for ( let chunk of Array . from ( this . chunks ) ) {
81
- this . document . _write ( chunk ) ;
82
- }
63
+ this . document . _write ( this . buffer ) ;
83
64
84
- this . chunks . length = 0 ; // free up memory
65
+ this . buffer = [ ] ; // free up memory
85
66
this . document . _write ( '\nendstream' ) ;
86
67
}
87
68
88
69
this . document . _write ( 'endobj' ) ;
89
- return this . document . _refEnd ( this ) ;
70
+ this . document . _refEnd ( this ) ;
90
71
}
91
-
92
72
toString ( ) {
93
73
return `${ this . id } ${ this . gen } R` ;
94
74
}
0 commit comments