简体   繁体   English

Flutter pdf 多张图片

[英]Flutter pdf multiple images

I'm trying to load multiple images (from assets or cache) into the pdf, but it does not work.我正在尝试将多个图像(来自资产或缓存)加载到 pdf 中,但它不起作用。 There are no errors, the pdf is created, but is corrupted somehow and does not want to open.没有错误,创建了 pdf,但不知何故已损坏并且不想打开。 Everything is 100% if there is only 1 image, or if all the images are the same image.如果只有一张图像,或者所有图像都是同一图像,则一切都是 100%。 What am I doing wrong?我究竟做错了什么?

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(backgroundColor: Colors.red, title: Text('Images')),
            body: Center(child: HomeScreen())));
  }
}

class HomeScreen extends StatefulWidget {
  HomeScreenState createState() => HomeScreenState();
}

class HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
      child: ListView(
        children: <Widget>[
          RaisedButton(
            color: Colors.red,
            onPressed: () async {
              writeOnPdf();
              await savePdf();
            },
            child: Text('Create Pdf'),
          ),
        ],
      ),
    ));
  }
}

final pdf = pw.Document();

writeOnPdf() async {
  var assetImage = PdfImage.file(
    pdf.document,
    bytes: (await rootBundle.load('assets/aa.png')).buffer.asUint8List(),
  );

  var otherImage = PdfImage.file(
    pdf.document,
    bytes: (await rootBundle.load('assets/out.png')).buffer.asUint8List(),
  );

  pdf.addPage(pw.MultiPage(
      pageFormat: PdfPageFormat.a4,
      margin: pw.EdgeInsets.all(32),
      build: (pw.Context context) {
        return <pw.Widget>[
          pw.Column(
              crossAxisAlignment: pw.CrossAxisAlignment.center,
              mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
              children: <pw.Widget>[
                pw.ClipRect(
                  child: pw.Container(
                    width: 550,
                    height: 250,
                    child: pw.Image(assetImage),
                  ),
                ),
              ]),
          pw.ClipRect(
            child: pw.Container(
              width: 100,
              height: 100,
              child: pw.Image(otherImage),
            ),
          )
        ];
      }));
}

final bool isHTML = false;

Future savePdf() async {
  Directory tempDir = await getTemporaryDirectory();
  String tempPath = tempDir.path;
  File file = File('$tempPath/1234.pdf');
  file.writeAsBytesSync(pdf.save());
  print(tempDir.listSync());
}

It would be ideal if I could load one image from the assets and one image from the cache.如果我可以从资产中加载一张图像并从缓存中加载一张图像,那将是理想的。 But at the moment nothing works.但目前没有任何效果。 Any advice would be appreciated.任何意见,将不胜感激。

DavBfr gave the answer. DavBfr给出了答案。 "It's an issue with async/await." “这是 async/await 的问题。”

Multiple Images #425] 1多个图像 #425] 1

              RaisedButton(
            color: Colors.red,
            onPressed: () async {
              final pdf = await writeOnPdf();
              await savePdf(pdf);
            },
            child: const Text('Create Pdf'),
          ),

 

try like that.像那样尝试。 its not influent way but it works for multiple images in same pdf page.它不是有影响力的方式,但它适用于同一 pdf 页面中的多个图像。

  final ByteData bytes1 = await rootBundle.load('assets/images/logo.png');
  final Uint8List img1 = bytes1.buffer.asUint8List();

  final ByteData bytes2 = await rootBundle.load('assets/images/1.jpg');
  final Uint8List img2 = bytes2.buffer.asUint8List();

  final ByteData bytes3 = await rootBundle.load('assets/images/2.png');
  final Uint8List img3 = bytes3.buffer.asUint8List();

  pw.Image imgty(Uint8List img) {
    return pw.Image(
        pw.MemoryImage(
          img,
        ),
        fit: pw.BoxFit.contain);
  }

and use them in pdf page并在 pdf 页面中使用它们

 Container(
        height: 111,
        width: 111,
        child: imgty(img1),)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM