You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter应用pdf包在Windows生成PDF时抛出FileSystemException异常

Flutter Windows平台PDF生成FileSystemException问题解决

问题原因

Windows系统文件名禁止包含:, *, ?, ", <, >, |等特殊字符,你当前生成的文件名sultanat_invoice${DateTime.now().toString()}.pdf中,DateTime.now().toString()会输出带冒号的时间格式(例如2024-05-20 14:25:30.456),导致文件路径非法,触发FileSystemException。

解决方案

1. 修复文件名中的非法字符

修改PdfInvoiceApi.generate方法里的文件名生成逻辑,替换时间字符串中的特殊字符:

return PdfApi.saveDocument(
  name: 'sultanat_invoice${DateTime.now().toString().replaceAll(RegExp(r'[:.]'), '-')}.pdf',
  pdf: pdf
);

此代码会将时间中的冒号和点替换为横杠,生成合法文件名(如sultanat_invoice2024-05-20 14-25-30-456.pdf)。

2. 验证path_provider的Windows支持

确保pubspec.yaml中path_provider依赖为最新版本,执行flutter pub get完成依赖同步。最新版path_provider已原生支持Windows平台,无需额外配置。

3. 可选:添加路径调试

在PdfApi.saveDocument中打印生成的文件路径,便于排查问题:

final file = File('${dir.path}/$name');
print('PDF保存路径:${file.path}'); // 新增调试打印
await file.writeAsBytes(bytes);

内容的提问来源于stack exchange,提问作者Freethinker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 20:18:20