@@ -48,7 +48,7 @@ import {
48
48
import PDFObject from 'src/core/objects/PDFObject' ;
49
49
import PDFRef from 'src/core/objects/PDFRef' ;
50
50
import { Fontkit } from 'src/types/fontkit' ;
51
- import { TransformationMatrix } from 'src/types/matrix ' ;
51
+ import { OnWarningHandler , TransformationMatrix } from 'src/types' ;
52
52
import {
53
53
assertIs ,
54
54
assertIsOneOfOrUndefined ,
@@ -131,6 +131,7 @@ export default class PDFDocument {
131
131
ignoreEncryption = false ,
132
132
parseSpeed = ParseSpeeds . Slow ,
133
133
throwOnInvalidObject = false ,
134
+ onWarning,
134
135
updateMetadata = true ,
135
136
capNumbers = false ,
136
137
} = options ;
@@ -146,24 +147,30 @@ export default class PDFDocument {
146
147
parseSpeed ,
147
148
throwOnInvalidObject ,
148
149
capNumbers ,
150
+ onWarning ,
149
151
) . parseDocument ( ) ;
150
- return new PDFDocument ( context , ignoreEncryption , updateMetadata ) ;
152
+ return new PDFDocument (
153
+ context ,
154
+ ignoreEncryption ,
155
+ updateMetadata ,
156
+ onWarning ,
157
+ ) ;
151
158
}
152
159
153
160
/**
154
161
* Create a new [[PDFDocument]].
155
162
* @returns Resolves with the newly created document.
156
163
*/
157
164
static async create ( options : CreateOptions = { } ) {
158
- const { updateMetadata = true } = options ;
165
+ const { updateMetadata = true , onWarning } = options ;
159
166
160
167
const context = PDFContext . create ( ) ;
161
168
const pageTree = PDFPageTree . withContext ( context ) ;
162
169
const pageTreeRef = context . register ( pageTree ) ;
163
170
const catalog = PDFCatalog . withContextAndPages ( context , pageTreeRef ) ;
164
171
context . trailerInfo . Root = context . register ( catalog ) ;
165
172
166
- return new PDFDocument ( context , false , updateMetadata ) ;
173
+ return new PDFDocument ( context , false , updateMetadata , onWarning ) ;
167
174
}
168
175
169
176
/** The low-level context of this document. */
@@ -175,6 +182,9 @@ export default class PDFDocument {
175
182
/** Whether or not this document is encrypted. */
176
183
readonly isEncrypted : boolean ;
177
184
185
+ /** The function called with details of non-fatal issues. */
186
+ readonly onWarning : OnWarningHandler ;
187
+
178
188
/** The default word breaks used in PDFPage.drawText */
179
189
defaultWordBreaks : string [ ] = [ ' ' ] ;
180
190
@@ -193,13 +203,16 @@ export default class PDFDocument {
193
203
context : PDFContext ,
194
204
ignoreEncryption : boolean ,
195
205
updateMetadata : boolean ,
206
+ onWarning : OnWarningHandler = console . warn . bind ( console ) ,
196
207
) {
197
208
assertIs ( context , 'context' , [ [ PDFContext , 'PDFContext' ] ] ) ;
198
209
assertIs ( ignoreEncryption , 'ignoreEncryption' , [ 'boolean' ] ) ;
210
+ assertIs ( onWarning , 'onWarning' , [ 'function' ] ) ;
199
211
200
212
this . context = context ;
201
213
this . catalog = context . lookup ( context . trailerInfo . Root ) as PDFCatalog ;
202
214
this . isEncrypted = ! ! context . lookup ( context . trailerInfo . Encrypt ) ;
215
+ this . onWarning = onWarning ;
203
216
204
217
this . pageCache = Cache . populatedBy ( this . computePages ) ;
205
218
this . pageMap = new Map ( ) ;
@@ -254,7 +267,7 @@ export default class PDFDocument {
254
267
getForm ( ) : PDFForm {
255
268
const form = this . formCache . access ( ) ;
256
269
if ( form . hasXFA ( ) ) {
257
- console . warn (
270
+ this . onWarning (
258
271
'Removing XFA form data as pdf-lib does not support reading or writing XFA' ,
259
272
) ;
260
273
form . deleteXFA ( ) ;
0 commit comments