Showing
4 changed files
with
57 additions
and
48 deletions
@@ -42,6 +42,10 @@ class PdfObjectBase<T extends PdfDataType> with PdfDiagnostic { | @@ -42,6 +42,10 @@ class PdfObjectBase<T extends PdfDataType> with PdfDiagnostic { | ||
42 | required this.objser, | 42 | required this.objser, |
43 | this.objgen = 0, | 43 | this.objgen = 0, |
44 | required this.params, | 44 | required this.params, |
45 | + this.deflate, | ||
46 | + this.encryptCallback, | ||
47 | + this.verbose = false, | ||
48 | + this.version = PdfVersion.pdf_1_5, | ||
45 | }); | 49 | }); |
46 | 50 | ||
47 | /// This is the unique serial number for this object. | 51 | /// This is the unique serial number for this object. |
@@ -53,23 +57,44 @@ class PdfObjectBase<T extends PdfDataType> with PdfDiagnostic { | @@ -53,23 +57,44 @@ class PdfObjectBase<T extends PdfDataType> with PdfDiagnostic { | ||
53 | final T params; | 57 | final T params; |
54 | 58 | ||
55 | /// Callback used to compress the data | 59 | /// Callback used to compress the data |
56 | - DeflateCallback? get deflate => null; | 60 | + final DeflateCallback? deflate; |
57 | 61 | ||
58 | /// Callback used to encrypt the value of a [PdfDictStream] or a [PdfEncStream] | 62 | /// Callback used to encrypt the value of a [PdfDictStream] or a [PdfEncStream] |
59 | - PdfEncryptCallback? get encryptCallback => null; | 63 | + final PdfEncryptCallback? encryptCallback; |
60 | 64 | ||
61 | /// Output a PDF document with comments and formatted data | 65 | /// Output a PDF document with comments and formatted data |
62 | - bool get verbose => false; | 66 | + final bool verbose; |
63 | 67 | ||
64 | - PdfVersion get version => PdfVersion.pdf_1_5; | 68 | + /// PDF version to generate |
69 | + final PdfVersion version; | ||
65 | 70 | ||
66 | /// Returns the unique serial number in Pdf format | 71 | /// Returns the unique serial number in Pdf format |
67 | PdfIndirect ref() => PdfIndirect(objser, objgen); | 72 | PdfIndirect ref() => PdfIndirect(objser, objgen); |
68 | 73 | ||
69 | - void output(PdfStream s) { | 74 | + int output(PdfStream s) { |
75 | + assert(() { | ||
76 | + if (verbose) { | ||
77 | + setInsertion(s, 160); | ||
78 | + startStopwatch(); | ||
79 | + } | ||
80 | + return true; | ||
81 | + }()); | ||
82 | + | ||
83 | + final offset = s.offset; | ||
70 | s.putString('$objser $objgen obj\n'); | 84 | s.putString('$objser $objgen obj\n'); |
71 | writeContent(s); | 85 | writeContent(s); |
72 | s.putString('endobj\n'); | 86 | s.putString('endobj\n'); |
87 | + | ||
88 | + assert(() { | ||
89 | + if (verbose) { | ||
90 | + stopStopwatch(); | ||
91 | + debugFill( | ||
92 | + 'Creation time: ${elapsedStopwatch / Duration.microsecondsPerSecond} seconds'); | ||
93 | + writeDebug(s); | ||
94 | + } | ||
95 | + return true; | ||
96 | + }()); | ||
97 | + return offset; | ||
73 | } | 98 | } |
74 | 99 | ||
75 | void writeContent(PdfStream s) { | 100 | void writeContent(PdfStream s) { |
@@ -144,24 +144,8 @@ class PdfXrefTable extends PdfDataType with PdfDiagnostic { | @@ -144,24 +144,8 @@ class PdfXrefTable extends PdfDataType with PdfDiagnostic { | ||
144 | }()); | 144 | }()); |
145 | 145 | ||
146 | for (final ob in objects) { | 146 | for (final ob in objects) { |
147 | - assert(() { | ||
148 | - if (ob.verbose) { | ||
149 | - ob.setInsertion(s, 150); | ||
150 | - ob.startStopwatch(); | ||
151 | - } | ||
152 | - return true; | ||
153 | - }()); | ||
154 | - _offsets.add(PdfXref(ob.objser, s.offset, generation: ob.objgen)); | ||
155 | - ob.output(s); | ||
156 | - assert(() { | ||
157 | - if (ob.verbose) { | ||
158 | - ob.stopStopwatch(); | ||
159 | - ob.debugFill( | ||
160 | - 'Creation time: ${ob.elapsedStopwatch / Duration.microsecondsPerSecond} seconds'); | ||
161 | - ob.writeDebug(s); | ||
162 | - } | ||
163 | - return true; | ||
164 | - }()); | 147 | + final offset = ob.output(s); |
148 | + _offsets.add(PdfXref(ob.objser, offset, generation: ob.objgen)); | ||
165 | } | 149 | } |
166 | 150 | ||
167 | final int xrefOffset; | 151 | final int xrefOffset; |
@@ -195,7 +179,6 @@ class PdfXrefTable extends PdfDataType with PdfDiagnostic { | @@ -195,7 +179,6 @@ class PdfXrefTable extends PdfDataType with PdfDiagnostic { | ||
195 | debugFill( | 179 | debugFill( |
196 | 'Creation time: ${elapsedStopwatch / Duration.microsecondsPerSecond} seconds'); | 180 | 'Creation time: ${elapsedStopwatch / Duration.microsecondsPerSecond} seconds'); |
197 | debugFill('File size: ${s.offset} bytes'); | 181 | debugFill('File size: ${s.offset} bytes'); |
198 | - // debugFill('Pages: ${rootID!.pdfDocument.pdfPageList.pages.length}'); | ||
199 | debugFill('Objects: ${objects.length}'); | 182 | debugFill('Objects: ${objects.length}'); |
200 | writeDebug(s); | 183 | writeDebug(s); |
201 | } | 184 | } |
@@ -337,16 +320,19 @@ class PdfXrefTable extends PdfDataType with PdfDiagnostic { | @@ -337,16 +320,19 @@ class PdfXrefTable extends PdfDataType with PdfDiagnostic { | ||
337 | 320 | ||
338 | final objOffset = s.offset; | 321 | final objOffset = s.offset; |
339 | 322 | ||
340 | - s.putString('$id 0 obj\n'); | ||
341 | - | ||
342 | - PdfDictStream( | 323 | + PdfObjectBase( |
324 | + objser: id, | ||
325 | + params: PdfDictStream( | ||
343 | data: binOffsets.buffer.asUint8List(), | 326 | data: binOffsets.buffer.asUint8List(), |
344 | isBinary: false, | 327 | isBinary: false, |
345 | encrypt: false, | 328 | encrypt: false, |
346 | values: params.values, | 329 | values: params.values, |
347 | - ).output(o, s, o.verbose ? 0 : null); | 330 | + ), |
331 | + deflate: o.deflate, | ||
332 | + verbose: o.verbose, | ||
333 | + version: o.version, | ||
334 | + ).output(s); | ||
348 | 335 | ||
349 | - s.putString('\nendobj\n'); | ||
350 | return objOffset; | 336 | return objOffset; |
351 | } | 337 | } |
352 | } | 338 | } |
@@ -82,12 +82,13 @@ class PdfSignature extends PdfObjectDict { | @@ -82,12 +82,13 @@ class PdfSignature extends PdfObjectDict { | ||
82 | int? _offsetEnd; | 82 | int? _offsetEnd; |
83 | 83 | ||
84 | @override | 84 | @override |
85 | - void output(PdfStream s) { | 85 | + int output(PdfStream s) { |
86 | value.preSign(this, params); | 86 | value.preSign(this, params); |
87 | 87 | ||
88 | _offsetStart = s.offset + '$objser $objgen obj\n'.length; | 88 | _offsetStart = s.offset + '$objser $objgen obj\n'.length; |
89 | - super.output(s); | 89 | + final offset = super.output(s); |
90 | _offsetEnd = s.offset; | 90 | _offsetEnd = s.offset; |
91 | + return offset; | ||
91 | } | 92 | } |
92 | 93 | ||
93 | Future<void> writeSignature(PdfStream os) async { | 94 | Future<void> writeSignature(PdfStream os) async { |
@@ -21,38 +21,33 @@ import 'package:pdf/pdf.dart'; | @@ -21,38 +21,33 @@ import 'package:pdf/pdf.dart'; | ||
21 | import 'package:pdf/src/priv.dart'; | 21 | import 'package:pdf/src/priv.dart'; |
22 | import 'package:test/test.dart'; | 22 | import 'package:test/test.dart'; |
23 | 23 | ||
24 | -class BasicObject<T extends PdfDataType> extends PdfObjectBase<T> { | ||
25 | - BasicObject({required super.objser, required super.params}); | ||
26 | - | ||
27 | - @override | ||
28 | - bool get verbose => true; | ||
29 | - | ||
30 | - @override | ||
31 | - PdfVersion get version => PdfVersion.pdf_1_4; | ||
32 | - | ||
33 | - @override | ||
34 | - DeflateCallback? get deflate => zlib.encode; | ||
35 | -} | ||
36 | - | ||
37 | void main() { | 24 | void main() { |
38 | test('Pdf Minimal', () async { | 25 | test('Pdf Minimal', () async { |
39 | var objser = 1; | 26 | var objser = 1; |
27 | + const verbose = true; | ||
28 | + const version = PdfVersion.pdf_1_4; | ||
40 | 29 | ||
41 | - final pages = BasicObject( | 30 | + final pages = PdfObjectBase( |
42 | objser: objser++, | 31 | objser: objser++, |
32 | + verbose: verbose, | ||
33 | + version: version, | ||
43 | params: PdfDict({ | 34 | params: PdfDict({ |
44 | '/Type': const PdfName('/Pages'), | 35 | '/Type': const PdfName('/Pages'), |
45 | '/Count': const PdfNum(1), | 36 | '/Count': const PdfNum(1), |
46 | })); | 37 | })); |
47 | 38 | ||
48 | - final content = BasicObject( | 39 | + final content = PdfObjectBase( |
49 | objser: objser++, | 40 | objser: objser++, |
41 | + verbose: verbose, | ||
42 | + version: version, | ||
50 | params: PdfDictStream( | 43 | params: PdfDictStream( |
51 | data: latin1.encode('30 811.88976 m 200 641.88976 l S'), | 44 | data: latin1.encode('30 811.88976 m 200 641.88976 l S'), |
52 | )); | 45 | )); |
53 | 46 | ||
54 | - final page = BasicObject( | 47 | + final page = PdfObjectBase( |
55 | objser: objser++, | 48 | objser: objser++, |
49 | + verbose: verbose, | ||
50 | + version: version, | ||
56 | params: PdfDict({ | 51 | params: PdfDict({ |
57 | '/Type': const PdfName('/Page'), | 52 | '/Type': const PdfName('/Page'), |
58 | '/Parent': pages.ref(), | 53 | '/Parent': pages.ref(), |
@@ -67,8 +62,10 @@ void main() { | @@ -67,8 +62,10 @@ void main() { | ||
67 | 62 | ||
68 | pages.params['/Kids'] = PdfArray([page.ref()]); | 63 | pages.params['/Kids'] = PdfArray([page.ref()]); |
69 | 64 | ||
70 | - final catalog = BasicObject( | 65 | + final catalog = PdfObjectBase( |
71 | objser: objser++, | 66 | objser: objser++, |
67 | + verbose: verbose, | ||
68 | + version: version, | ||
72 | params: PdfDict({ | 69 | params: PdfDict({ |
73 | '/Type': const PdfName('/Catalog'), | 70 | '/Type': const PdfName('/Catalog'), |
74 | '/Pages': pages.ref(), | 71 | '/Pages': pages.ref(), |
-
Please register or login to post a comment