Garrett
Committed by David PHAM-VAN

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

@@ -290,52 +290,58 @@ class PrintingPlugin extends PrintingPlatform { @@ -290,52 +290,58 @@ 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 -  
294 - final d = await promiseToFuture<PdfJsDoc>(t.promise);  
295 - final numPages = d.numPages;  
296 -  
297 - final html.CanvasElement canvas =  
298 - js.context['document'].createElement('canvas');  
299 -  
300 - final context = canvas.getContext('2d') as html.CanvasRenderingContext2D?;  
301 - final _pages = pages ?? Iterable<int>.generate(numPages, (index) => index);  
302 -  
303 - for (final i in _pages) {  
304 - final page = await promiseToFuture<PdfJsPage>(d.getPage(i + 1));  
305 - final viewport =  
306 - page.getViewport(Settings()..scale = dpi / PdfPageFormat.inch);  
307 -  
308 - canvas.height = viewport.height.toInt();  
309 - canvas.width = viewport.width.toInt();  
310 -  
311 - final renderContext = Settings()  
312 - ..canvasContext = context!  
313 - ..viewport = viewport;  
314 -  
315 - await promiseToFuture<void>(page.render(renderContext).promise);  
316 -  
317 - // Convert the image to PNG  
318 - final completer = Completer<void>();  
319 - final blob = await canvas.toBlob();  
320 - final data = BytesBuilder();  
321 - final r = FileReader();  
322 - r.readAsArrayBuffer(blob);  
323 - r.onLoadEnd.listen(  
324 - (ProgressEvent e) {  
325 - data.add(r.result as List<int>);  
326 - completer.complete();  
327 - },  
328 - );  
329 - await completer.future;  
330 -  
331 - yield _WebPdfRaster(  
332 - canvas.width!,  
333 - canvas.height!,  
334 - data.toBytes(),  
335 - );  
336 - page.cleanup(); 293 + try {
  294 + final d = await promiseToFuture<PdfJsDoc>(t.promise);
  295 + final numPages = d.numPages;
  296 +
  297 + final html.CanvasElement canvas =
  298 + js.context['document'].createElement('canvas');
  299 +
  300 + final context = canvas.getContext('2d') as html.CanvasRenderingContext2D?;
  301 + final _pages =
  302 + pages ?? Iterable<int>.generate(numPages, (index) => index);
  303 +
  304 + for (final i in _pages) {
  305 + final page = await promiseToFuture<PdfJsPage>(d.getPage(i + 1));
  306 + try {
  307 + final viewport =
  308 + page.getViewport(Settings()..scale = dpi / PdfPageFormat.inch);
  309 +
  310 + canvas.height = viewport.height.toInt();
  311 + canvas.width = viewport.width.toInt();
  312 +
  313 + final renderContext = Settings()
  314 + ..canvasContext = context!
  315 + ..viewport = viewport;
  316 +
  317 + await promiseToFuture<void>(page.render(renderContext).promise);
  318 +
  319 + // Convert the image to PNG
  320 + final completer = Completer<void>();
  321 + final blob = await canvas.toBlob();
  322 + final data = BytesBuilder();
  323 + final r = FileReader();
  324 + r.readAsArrayBuffer(blob);
  325 + r.onLoadEnd.listen(
  326 + (ProgressEvent e) {
  327 + data.add(r.result as List<int>);
  328 + completer.complete();
  329 + },
  330 + );
  331 + await completer.future;
  332 +
  333 + yield _WebPdfRaster(
  334 + canvas.width!,
  335 + canvas.height!,
  336 + data.toBytes(),
  337 + );
  338 + } finally {
  339 + page.cleanup();
  340 + }
  341 + }
  342 + } finally {
  343 + t.destroy();
337 } 344 }
338 - t.destroy();  
339 } 345 }
340 } 346 }
341 347