Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
dart_pdf
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Garrett
2022-05-16 14:20:46 -0700
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Committed by
David PHAM-VAN
2022-05-19 17:59:59 -0300
Commit
a48077ae2b5f272acd5e6719c0fdff0e8e126f59
a48077ae
1 parent
9ac0db40
Wrapped cleanup of page and destory of document in try/finally blocks
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
45 deletions
printing/lib/printing_web.dart
printing/lib/printing_web.dart
View file @
a48077a
...
...
@@ -290,52 +290,58 @@ class PrintingPlugin extends PrintingPlatform {
await
_initPlugin
();
final
t
=
PdfJs
.
getDocument
(
Settings
()..
data
=
document
);
final
d
=
await
promiseToFuture
<
PdfJsDoc
>(
t
.
promise
);
final
numPages
=
d
.
numPages
;
final
html
.
CanvasElement
canvas
=
js
.
context
[
'document'
].
createElement
(
'canvas'
);
final
context
=
canvas
.
getContext
(
'2d'
)
as
html
.
CanvasRenderingContext2D
?;
final
_pages
=
pages
??
Iterable
<
int
>.
generate
(
numPages
,
(
index
)
=>
index
);
for
(
final
i
in
_pages
)
{
final
page
=
await
promiseToFuture
<
PdfJsPage
>(
d
.
getPage
(
i
+
1
));
final
viewport
=
page
.
getViewport
(
Settings
()..
scale
=
dpi
/
PdfPageFormat
.
inch
);
canvas
.
height
=
viewport
.
height
.
toInt
();
canvas
.
width
=
viewport
.
width
.
toInt
();
final
renderContext
=
Settings
()
..
canvasContext
=
context
!
..
viewport
=
viewport
;
await
promiseToFuture
<
void
>(
page
.
render
(
renderContext
).
promise
);
// Convert the image to PNG
final
completer
=
Completer
<
void
>();
final
blob
=
await
canvas
.
toBlob
();
final
data
=
BytesBuilder
();
final
r
=
FileReader
();
r
.
readAsArrayBuffer
(
blob
);
r
.
onLoadEnd
.
listen
(
(
ProgressEvent
e
)
{
data
.
add
(
r
.
result
as
List
<
int
>);
completer
.
complete
();
},
);
await
completer
.
future
;
yield
_WebPdfRaster
(
canvas
.
width
!,
canvas
.
height
!,
data
.
toBytes
(),
);
page
.
cleanup
();
try
{
final
d
=
await
promiseToFuture
<
PdfJsDoc
>(
t
.
promise
);
final
numPages
=
d
.
numPages
;
final
html
.
CanvasElement
canvas
=
js
.
context
[
'document'
].
createElement
(
'canvas'
);
final
context
=
canvas
.
getContext
(
'2d'
)
as
html
.
CanvasRenderingContext2D
?;
final
_pages
=
pages
??
Iterable
<
int
>.
generate
(
numPages
,
(
index
)
=>
index
);
for
(
final
i
in
_pages
)
{
final
page
=
await
promiseToFuture
<
PdfJsPage
>(
d
.
getPage
(
i
+
1
));
try
{
final
viewport
=
page
.
getViewport
(
Settings
()..
scale
=
dpi
/
PdfPageFormat
.
inch
);
canvas
.
height
=
viewport
.
height
.
toInt
();
canvas
.
width
=
viewport
.
width
.
toInt
();
final
renderContext
=
Settings
()
..
canvasContext
=
context
!
..
viewport
=
viewport
;
await
promiseToFuture
<
void
>(
page
.
render
(
renderContext
).
promise
);
// Convert the image to PNG
final
completer
=
Completer
<
void
>();
final
blob
=
await
canvas
.
toBlob
();
final
data
=
BytesBuilder
();
final
r
=
FileReader
();
r
.
readAsArrayBuffer
(
blob
);
r
.
onLoadEnd
.
listen
(
(
ProgressEvent
e
)
{
data
.
add
(
r
.
result
as
List
<
int
>);
completer
.
complete
();
},
);
await
completer
.
future
;
yield
_WebPdfRaster
(
canvas
.
width
!,
canvas
.
height
!,
data
.
toBytes
(),
);
}
finally
{
page
.
cleanup
();
}
}
}
finally
{
t
.
destroy
();
}
t
.
destroy
();
}
}
...
...
Please
register
or
login
to post a comment