Garrett
Committed by David PHAM-VAN

Wrapped cleanup of page and destory of document in try/finally blocks

@@ -290,7 +290,7 @@ class PrintingPlugin extends PrintingPlatform { @@ -290,7 +290,7 @@ class PrintingPlugin extends PrintingPlatform {
290 await _initPlugin(); 290 await _initPlugin();
291 291
292 final t = PdfJs.getDocument(Settings()..data = document); 292 final t = PdfJs.getDocument(Settings()..data = document);
293 - 293 + try {
294 final d = await promiseToFuture<PdfJsDoc>(t.promise); 294 final d = await promiseToFuture<PdfJsDoc>(t.promise);
295 final numPages = d.numPages; 295 final numPages = d.numPages;
296 296
@@ -298,10 +298,12 @@ class PrintingPlugin extends PrintingPlatform { @@ -298,10 +298,12 @@ class PrintingPlugin extends PrintingPlatform {
298 js.context['document'].createElement('canvas'); 298 js.context['document'].createElement('canvas');
299 299
300 final context = canvas.getContext('2d') as html.CanvasRenderingContext2D?; 300 final context = canvas.getContext('2d') as html.CanvasRenderingContext2D?;
301 - final _pages = pages ?? Iterable<int>.generate(numPages, (index) => index); 301 + final _pages =
  302 + pages ?? Iterable<int>.generate(numPages, (index) => index);
302 303
303 for (final i in _pages) { 304 for (final i in _pages) {
304 final page = await promiseToFuture<PdfJsPage>(d.getPage(i + 1)); 305 final page = await promiseToFuture<PdfJsPage>(d.getPage(i + 1));
  306 + try {
305 final viewport = 307 final viewport =
306 page.getViewport(Settings()..scale = dpi / PdfPageFormat.inch); 308 page.getViewport(Settings()..scale = dpi / PdfPageFormat.inch);
307 309
@@ -333,10 +335,14 @@ class PrintingPlugin extends PrintingPlatform { @@ -333,10 +335,14 @@ class PrintingPlugin extends PrintingPlatform {
333 canvas.height!, 335 canvas.height!,
334 data.toBytes(), 336 data.toBytes(),
335 ); 337 );
  338 + } finally {
336 page.cleanup(); 339 page.cleanup();
337 } 340 }
  341 + }
  342 + } finally {
338 t.destroy(); 343 t.destroy();
339 } 344 }
  345 + }
340 } 346 }
341 347
342 class _WebPdfRaster extends PdfRaster { 348 class _WebPdfRaster extends PdfRaster {