我使用这个库https://pub.dev/packages/pdf来生成 PDF。问题是我无法\xe2\x80\x99 设法将照片添加到我的 PDF 中。每张照片的路径都保存在List对象中。这就是我尝试实现的方式:
import \'package:pdf/widgets.dart\' as pw;\n\xe2\x80\x8b\n\xe2\x80\x8b\nFuture<void> generatePDF() async {\n\xe2\x80\x8b\npdf = pw.Document();\n\xe2\x80\x8b\n pdf.addPage(\n pw.MultiPage(\n pageFormat: PdfPageFormat.a4,\n build: (pw.Context context) => [\n pw.Partitions(\n children: [\n pw.Partition(\n child: pw.Column(\n crossAxisAlignment: pw.CrossAxisAlignment.start,\n children: <pw.Widget>[\n\xe2\x80\x8b\n /// Photos list\n pw.ListView.builder(\n itemCount: 1,\n itemBuilder: (pw.Context context, int index) {\n return pw.Column(\n children: _photosList\n ?.map(\n (photo) async => pw.Column(\n children: <pw.Widget>[\n pw.Text(\n "Photo id: ${photo.photoId} - Photo type: ${photo.photoType}"), // This is displayed correctly for each photo\n pw.Image(\n pw.MemoryImage(\n (await rootBundle\n .load(\'${photo.photoPath}\')) // This is not working because the photos are not in rootBundle and I can\'t find a solution to replace this line\n .buffer\n .asUint8List(),\n ),\n ),\n ],\n ),\n )\n ?.toList() ??\n [],\n );\n },\n ),\n ],\n ),\n ),\n ],\n ),\n ],\n ),\n );\n print(\'PDF Generated\');\n }\nRun Code Online (Sandbox Code Playgroud)\n输出:
\n\n导入飞镖:io
import 'dart:io';
Run Code Online (Sandbox Code Playgroud)
如果你有路径,你可以用它创建一个文件。
File photo1 = File(photo.photoPath);
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用以下语句将该文件加载到 PDF:
MemoryImage memoryImage = pw.MemoryImage(photo1.readAsBytesSync()); //you could use async as well
Run Code Online (Sandbox Code Playgroud)
这将为您留下来自 photoPath 的 MemoryImage。然后你可以简单地添加它:
pw.Image(memoryImage)
Run Code Online (Sandbox Code Playgroud)