dart pdf flutter // image attach不工作

v8wbuo2f  于 2023-05-11  发布在  Flutter
关注(0)|答案(1)|浏览(289)

我试过很多方法...我不知道如何从pdf中的资产文件夹中附加一张图像。
如果我在第30行添加这个:

pw.MemoryImage(
(await rootBundle.load('assets/img/your_image.jpg')).buffer.asUint8List(),
);

程序崩溃了...
PDF文件应包含徽标的图像。它是一个Web应用程序,所以当用户点击按钮时,它应该下载PDF。这是工作。但我无法添加图像...
主代码:

import 'package:flutter/material.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:pdf/pdf.dart';
import 'dart:typed_data';
import 'dart:html' as html;
import 'dart:typed_data';
import 'package:flutter/services.dart';

class PDFSave extends StatefulWidget {
  @override
  _PDFSaveState createState() => _PDFSaveState();
}

class _PDFSaveState extends State<PDFSave> {
  final pdf = pw.Document();
  var anchor;

  savePDF() async {
    Uint8List pdfInBytes = await pdf.save();
    final blob = html.Blob([pdfInBytes], 'application/pdf');
    final url = html.Url.createObjectUrlFromBlob(blob);
    anchor = html.document.createElement('a') as html.AnchorElement
      ..href = url
      ..style.display = 'none'
      ..download = 'pdf.pdf';
    html.document.body?.children.add(anchor);
  }

  createPDF() async {
    
    pdf.addPage(
      pw.Page(
        pageFormat: PdfPageFormat.a4,
        build: (pw.Context context) => pw.Column(
          children: [
            pw.Column(
              children: [
                pw.Row(
                  mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
                  children: [
                    pw.Text('Rechnung', style: pw.TextStyle(fontSize: 40)),
                    pw.Container(
                      padding: pw.EdgeInsets.all(10),
                      margin: pw.EdgeInsets.all(10),
                      decoration:
                          pw.BoxDecoration(border: pw.Border.all(width: 2)),
                      child: pw.BarcodeWidget(
                        color: PdfColor.fromHex("#000000"),
                        barcode: pw.Barcode.qrCode(),
                        data: "My data",
                        width: 50,
                        height: 50,
                      ),
                    ),
                  ],
                ),
                pw.Divider(
                  height: 2,
                  thickness: 2,
                ),
              ],
            ),
          ],
        ),
      ),
    );
    savePDF();
  }

  @override
  void initState() {
    super.initState();
    createPDF();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      appBar: AppBar(
        title: Text('PDF Creator'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Press'),
          onPressed: () {
            anchor.click();
          },
        ),
      ),
    );
  }
}

我试着附加了很多代码...

gv8xihay

gv8xihay1#

答案很简单。
首先,检查assets ordner是否在pubspec中
使用此代码将图像添加到PDF并在Web浏览器中下载。
如果出现错误,请重新启动IDE!!!

import 'package:flutter/material.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:pdf/pdf.dart';
import 'dart:typed_data';
import 'dart:html' as html;
import 'dart:typed_data';
import 'package:flutter/services.dart';

class PDFSave extends StatefulWidget {
  @override
  _PDFSaveState createState() => _PDFSaveState();
}

class _PDFSaveState extends State<PDFSave> {
  final pdf = pw.Document();
  var anchor;

  savePDF() async {
    Uint8List pdfInBytes = await pdf.save();
    final blob = html.Blob([pdfInBytes], 'application/pdf');
    final url = html.Url.createObjectUrlFromBlob(blob);
    anchor = html.document.createElement('a') as html.AnchorElement
      ..href = url
      ..style.display = 'none'
      ..download = 'pdf.pdf';
    html.document.body?.children.add(anchor);
  }

  createPDF() async {
    final ByteData image = await rootBundle.load('assets/logo.PNG');
    Uint8List imageData = (image).buffer.asUint8List();
    pdf.addPage(
      pw.Page(
        pageFormat: PdfPageFormat.a4,
        build: (pw.Context context) => pw.Column(
          children: [
            pw.Column(
              children: [
                pw.Row(
                  mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
                  children: [
                    pw.Text('Rechnung', style: pw.TextStyle(fontSize: 40)),
                    pw.Container(
                      padding: pw.EdgeInsets.all(10),
                      margin: pw.EdgeInsets.all(10),
                      decoration:
                          pw.BoxDecoration(border: pw.Border.all(width: 2)),
                      child: pw.BarcodeWidget(
                        color: PdfColor.fromHex("#000000"),
                        barcode: pw.Barcode.qrCode(),
                        data: "My data",
                        width: 50,
                        height: 50,
                      ),
                    ),
                  ],
                ),
                pw.Divider(
                  height: 2,
                  thickness: 2,
                ),
                pw.Container(
                  width: 50.0,
                  height: 50.0,
                  child: pw.Image(
                    pw.MemoryImage(imageData),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
    savePDF();
  }

  @override
  void initState() {
    super.initState();
    createPDF();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      appBar: AppBar(
        title: Text('PDF Creator'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Press'),
          onPressed: () {
            anchor.click();
          },
        ),
      ),
    );
  }
}

Finished pdf with image

相关问题