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
David PHAM-VAN
2021-06-24 08:37:13 -0300
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
2c75319580e9f8c0e861b9d6bed577e0279ecd9e
2c753195
1 parent
1c35ae7d
Use proper print dialog on Firefox
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
34 deletions
printing/CHANGELOG.md
printing/README.md
printing/lib/printing_web.dart
printing/CHANGELOG.md
View file @
2c75319
# Changelog
## 5.4.2
-
Use proper print dialog on Firefox
## 5.4.1
-
Always use HTTPS to download Google Fonts
...
...
printing/README.md
View file @
2c75319
...
...
@@ -37,8 +37,7 @@ for documentation.
4.
For MacOS add printing capability by opening macos directory in XCode
5.
For the web, a javascript library and a small script has to be added to
your
`web/index.html`
file, just before
`<script src="main.dart.js" type="application/javascript"></script>`
:
your
`web/index.html` file, just before `</head>`
:
```
html
<script
src=
"//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.8.335/pdf.min.js"
></script>
...
...
printing/lib/printing_web.dart
View file @
2c75319
...
...
@@ -43,6 +43,8 @@ class PrintingPlugin extends PrintingPlatform {
PrintingPlatform
.
instance
=
PrintingPlugin
();
}
static
const
String
_scriptId
=
'__net_nfet_printing_s__'
;
static
const
String
_frameId
=
'__net_nfet_printing__'
;
@override
...
...
@@ -99,59 +101,76 @@ class PrintingPlugin extends PrintingPlatform {
final
isChrome
=
js
.
context
[
'chrome'
]
!=
null
;
final
isSafari
=
js
.
context
[
'safari'
]
!=
null
;
final
isMobile
=
userAgent
.
contains
(
'Mobile'
);
// Maybe Firefox will support iframe printing
// https://bugzilla.mozilla.org/show_bug.cgi?id=911444
// final isFirefox = userAgent.contains('Firefox');
final
isFirefox
=
userAgent
.
contains
(
'Firefox'
);
// Chrome and Safari on a desktop computer
if
((
isChrome
||
isSafari
)
&&
!
isMobile
)
{
// Chrome, Safari, and Firefox on a desktop computer
if
((
isChrome
||
isSafari
||
isFirefox
)
&&
!
isMobile
)
{
final
completer
=
Completer
<
bool
>();
final
pdfFile
=
html
.
Blob
(
<
Uint8List
>[
Uint8List
.
fromList
(
result
)
],
<
Uint8List
>[
result
],
'application/pdf'
,
);
final
pdfUrl
=
html
.
Url
.
createObjectUrl
(
pdfFile
);
final
html
.
HtmlDocument
doc
=
js
.
context
[
'document'
];
final
script
=
doc
.
getElementById
(
_scriptId
)
??
doc
.
createElement
(
'script'
);
script
.
setAttribute
(
'id'
,
_scriptId
);
script
.
setAttribute
(
'type'
,
'text/javascript'
);
script
.
innerText
=
'''function
${_frameId}
_print(){var f=document.getElementById('
$_frameId
');f.focus();f.contentWindow.print();}'''
;
doc
.
body
!.
append
(
script
);
final
frame
=
doc
.
getElementById
(
_frameId
)
??
doc
.
createElement
(
'iframe'
);
frame
.
setAttribute
(
'style'
,
'visibility: hidden; height: 0; width: 0; position: absolute;'
,
// 'height: 400px; width: 600px; position: absolute; z-index: 1000',
);
if
(
isFirefox
)
{
// Set the iframe to be is visible on the page (guaranteed by fixed position) but hidden using opacity 0, because
// this works in Firefox. The height needs to be sufficient for some part of the document other than the PDF
// viewer's toolbar to be visible in the page
frame
.
setAttribute
(
'style'
,
'width: 1px; height: 100px; position: fixed; left: 0; top: 0; opacity: 0; border-width: 0; margin: 0; padding: 0'
);
}
else
{
// Hide the iframe in other browsers
frame
.
setAttribute
(
'style'
,
'visibility: hidden; height: 0; width: 0; position: absolute;'
,
// 'height: 400px; width: 600px; position: absolute; z-index: 1000',
);
}
frame
.
setAttribute
(
'id'
,
_frameId
);
frame
.
setAttribute
(
'src'
,
pdfUrl
);
final
stopWatch
=
Stopwatch
();
html
.
EventListener
?
load
;
load
=
(
html
.
Event
event
)
{
frame
.
removeEventListener
(
'load'
,
load
);
final
js
.
JsObject
win
=
js
.
JsObject
.
fromBrowserObject
(
frame
)[
'contentWindow'
];
frame
.
focus
();
win
.
callMethod
(
'print'
);
completer
.
complete
(
true
);
Timer
(
Duration
(
milliseconds:
isSafari
?
500
:
0
),
()
{
try
{
stopWatch
.
start
();
js
.
context
.
callMethod
(
'
${_frameId}
_print'
);
stopWatch
.
stop
();
completer
.
complete
(
true
);
}
catch
(
e
)
{
print
(
e
);
completer
.
complete
(
_getPdf
(
result
));
}
});
};
frame
.
addEventListener
(
'load'
,
load
);
doc
.
body
!.
append
(
frame
);
return
completer
.
future
;
final
res
=
await
completer
.
future
;
// If print() is synchronous
if
(
stopWatch
.
elapsedMilliseconds
>
1000
)
{
frame
.
remove
();
script
.
remove
();
}
return
res
;
}
// All the others
final
pdfFile
=
html
.
Blob
(
<
Uint8List
>[
Uint8List
.
fromList
(
result
)],
'application/pdf'
,
);
final
pdfUrl
=
html
.
Url
.
createObjectUrl
(
pdfFile
);
final
html
.
HtmlDocument
doc
=
js
.
context
[
'document'
];
final
link
=
html
.
AnchorElement
(
href:
pdfUrl
);
link
.
target
=
'_blank'
;
doc
.
body
?.
append
(
link
);
link
.
click
();
link
.
remove
();
return
true
;
return
_getPdf
(
result
);
}
@override
...
...
@@ -163,14 +182,22 @@ class PrintingPlugin extends PrintingPlatform {
String
?
body
,
List
<
String
>?
emails
,
)
async
{
return
_getPdf
(
bytes
,
filename:
filename
);
}
Future
<
bool
>
_getPdf
(
Uint8List
bytes
,
{
String
?
filename
})
async
{
final
pdfFile
=
html
.
Blob
(
<
Uint8List
>[
Uint8List
.
fromList
(
bytes
)
],
<
Uint8List
>[
bytes
],
'application/pdf'
,
);
final
pdfUrl
=
html
.
Url
.
createObjectUrl
(
pdfFile
);
final
html
.
HtmlDocument
doc
=
js
.
context
[
'document'
];
final
link
=
html
.
AnchorElement
(
href:
pdfUrl
);
link
.
download
=
filename
;
if
(
filename
!=
null
)
{
link
.
download
=
filename
;
}
else
{
link
.
target
=
'_blank'
;
}
doc
.
body
?.
append
(
link
);
link
.
click
();
link
.
remove
();
...
...
Please
register
or
login
to post a comment