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

Flutter中如何将screenshot获取的Uint8List转为PdfImage用于打印?

实现方法
  • 把screenshot获取到的Uint8List数据,通过printing包的PdfImage.file方法,结合PDF上下文对象转换成PdfImage实例。
  • 在PDF页面的构建逻辑中,用这个PdfImage渲染图片即可。

完整代码示例:

// 假设你通过screenshot获取到的Uint8List数据变量名为screenshotBytes
doc.addPage(pw.Page(
  build: (pw.Context context) {
    // 将Uint8List转换为PdfImage
    final pdfImage = pw.PdfImage.file(
      context.pdf,
      bytes: screenshotBytes,
    );
    
    return pw.Center(
      child: pw.Image(pdfImage),
    );
  },
));

注意事项

  • 确保screenshotBytes不为空,建议转换前增加非空判断,避免打印报错。
  • 若需要调整图片尺寸,可给pw.Image添加width或height参数,比如pw.Image(pdfImage, width: 200)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 14:17:00