Flutter 生成PDF文件并下载至本地,通过手机自带分享栏分享

pdf文件中显示中文或特殊字符需要下载ttf文件,推荐下载地址(免费):

中国大陆-猫啃网-第4页 (maoken.com)

我选择的flutter_to_pdf插件:

flutter_to_pdf example | Flutter package (pub.dev)

1.dependencies中添加依赖:

2.导入下载好的字体文件:

别忘了pubspec.yaml中配置:

3.官方demo示例:

4.根据需要更换字体文件 :

5. 换了下载路径,可在本地文件中打开(path_provider插件获取路径):

6. 导出多页pdf文档,单页不需要addPage,分享文件Share.shareFiles(['文件地址',])

7.其中pdf页面示例,fontFamily设置导入的字体

From the Back Cover Explore what Flutter has to offer, where it came from, and where it’s going. Mobile development is progressing at a fast rate and with Flutter – an open-source mobile application development SDK created by Google – you can develop applications for Android and iOS, as well as Google Fuchsia. Learn to create three apps (a personal information manager, a chat system, and a game project) that you can install on your mobile devices and use for real. You will begin by getting a solid foundation of Flutter knowledge, and building on it immediately by constructing two more traditional productivity apps.. You will also learn to create a game, enabling you to see a whole other perspective on what Flutter can do. In addition to building these apps, you'll have the benefit of reviewing real-world issues you might encounter, along with ways to deal with them through tips and tricks, all designed to make your Flutter experience that much more productive and, frankly, fun! Practical Flutter will leave you with a solid grasp of how to build apps with Flutter, and springboard into creating more advanced apps on your own. By the time your journey through this material concludes, another larger one will begin as you springboard, well-prepared, into the larger world of Flutter development, tackling any project that comes your way with aplomb. Practical Flutter is a learning adventure you won't want to miss. About the Author Frank Zammetti is the author of 11 Apress titles on a variety of web and mobile development topics. He has over 25 years of experience as a developer. You can find him on Twitter @fzammetti.
要将Flutter中的图片保存到本地并分享到QQ,你可以按照以下步骤操作: 1. 将图片保存到本地存储,可以使用以下代码: ```dart import 'dart:io'; import 'package:flutter/material.dart'; import 'package:path_provider/path_provider.dart'; import 'package:image_gallery_saver/image_gallery_saver.dart'; Future<void> saveImageToGallery(BuildContext context, String imageUrl) async { try { // 获取应用程序目录 Directory appDocDir = await getApplicationDocumentsDirectory(); // 获取文件名 String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1); // 组合路径 String filePath = "${appDocDir.path}/$fileName"; // 下载图片 HttpClient httpClient = HttpClient(); HttpClientRequest request = await httpClient.getUrl(Uri.parse(imageUrl)); HttpClientResponse response = await request.close(); Uint8List bytes = await consolidateHttpClientResponseBytes(response); // 保存图片到本地 File file = File(filePath); await file.writeAsBytes(bytes); // 保存图片到相册 await ImageGallerySaver.saveImage(bytes); ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text("图片已保存到相册"), )); } catch (e) { ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text("保存图片失败: $e"), )); } } ``` 2. 调用QQ分享功能,可以使用以下代码: ```dart import 'package:flutter/material.dart'; import 'package:flutter_qq_share/flutter_qq_share.dart'; Future<void> shareImageToQQ(BuildContext context, String imageUrl) async { try { // 获取应用程序目录 Directory appDocDir = await getApplicationDocumentsDirectory(); // 获取文件名 String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1); // 组合路径 String filePath = "${appDocDir.path}/$fileName"; // 下载图片 HttpClient httpClient = HttpClient(); HttpClientRequest request = await httpClient.getUrl(Uri.parse(imageUrl)); HttpClientResponse response = await request.close(); Uint8List bytes = await consolidateHttpClientResponseBytes(response); // 保存图片到本地 File file = File(filePath); await file.writeAsBytes(bytes); // 分享图片到QQ await FlutterQQShare.shareImageToQQ(filePath); } catch (e) { ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text("分享图片失败: $e"), )); } } ``` 调用 `saveImageToGallery` 函数可以将图片保存到本地并保存到相册,调用 `shareImageToQQ` 函数可以将图片分享到QQ。你可以根据自己的需求修改和调整这些代码。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值