如何通过webview下载/创建pdf文件

问题描述 投票:6回答:1

我创建了一个webviewscaffold,但是在从webview浏览时无法下载任何内容,但是当我单击按钮时,它什么也没做。我不知道从哪里开始,就像其他可以下载和预览的浏览器一样,我试图在这里实现相同的目的。

class AppState extends State<App> {


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Align(
            alignment: Alignment(1, 1),
            child: Container(
              child: Web(), // it is a statefulwidget that have WebviewScaffold, i have created it on a new page and imported/used here
            ),
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(top: 20.0),
              )
            ],
          ),
          Align(
            alignment: Alignment(0.7, -0.93),
            child: FloatingActionButton(
                tooltip: "Share",
                child: Icon(
                  Icons.share,
                  color: Colors.amberAccent,
                ),
                onPressed: () {
                  _onShareTap();
                }),
          ),
        ],
      ),
    );
  }

我希望,当我单击Web视图中的打印或下载按钮时,它应该像其他浏览器一样工作。

dart flutter flutter-layout flutter-dependencies
1个回答
0
投票

如果您是html页面的所有者,则可以检查对我有用的解决方法。在html页面的源代码中,为特定资源链接添加onclick函数:

function downloadpdf() {
      var currentHref = window.location.href;
      window.history.pushState(null, null, '/app/somepdf.pdf');
      setTimeout(() => window.location.replace(currentHref), 1000);
}

在Flutter代码中添加侦听器:

StreamSubscription<String> _onWebViewUrlChanged;
_onWebViewUrlChanged =
    FlutterWebviewPlugin().onUrlChanged.listen((String url) {
      if (url.contains('.pdf')) {
        launchURL(url);
      }
});

launchURL将在外部浏览器窗口中打开预定义的URL。因此,您可以从flutter_webview_plugin下载pdf / etc。

您应该添加一些特定于应用的js / flutter魔术

© www.soinside.com 2019 - 2024. All rights reserved.