简体   繁体   English

如何在 Dart / Flutter pdf 库中使用 drawBox、drawEllipse 或 drawLine 方法

[英]How to use drawBox, drawEllipse or drawLine methods within the Dart / Flutter pdf library

I want to generate PDF files including self drawn graphics within a Flutter App.我想在 Flutter 应用程序中生成 PDF 文件,包括自绘图形。 Of course with the pdf library provided it is quite simple to show a pdf preview containing for example two text lines, but i want to be able to insert some graphics that i want to draw myself, as i need to draw (myself)some very unconventional graphs.当然,如果使用 pdf 库,可以非常简单地显示包含例如两个文本行的 pdf 预览,但我希望能够插入一些我想自己绘制的图形,因为我需要绘制(我自己)一些非常非常规的图表。 In order to do that i need to be able to draw within a pdf widget (some lines, curves, points, of several colors, etc...).为了做到这一点,我需要能够在 pdf 小部件(一些线、曲线、点、几个 colors 等......)中进行绘制。 As per now i didn't manage to even draw a point,,,.按照现在,我什至没有设法画一个点,,,。 the pdf library of flutter dart describes dozens of methods. flutter dart 的 pdf 库描述了几十种方法。 but doesn't show any example, that's a pitty in fact.但没有显示任何例子,这实际上是一个遗憾。 Is there somebody who could help me in order to "draw" graphics within PDF Dart Flutter Object.是否有人可以帮助我在 PDF Dart Flutter 对象中“绘制”图形。 The PdfLibrary includes PdfGraphics class that is supposed to have the methods i try tu use without success !! PdfLibrary 包括 PdfGraphics class 应该有我尝试使用但没有成功的方法!

Many thank's in advance提前谢谢了

Please find my code:请找到我的代码:

import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';

void main() => runApp(const MyApp('Ceci est mon premier PDF'));

class MyApp extends StatelessWidget {
  const MyApp(this.title);

  final String title;

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text(title)),
        body: PdfPreview(
          build: (format) => _generatePdf(format, title),
        ),
      ),
    );
  }

  Future<Uint8List> _generatePdf(PdfPageFormat format, String title) async {
    final pdf = pw.Document();

    pdf.addPage(
      pw.Page(
        pageFormat: format,
        build: (context) {
          return pw.Center(
            child: pw.Column (
              children: [
                pw.Text(title),
                pw.Text(title),
                //pw.drawBox(10,10,100,100),     <---- if i remove the comment the app 
                                                       crashes saying "Method not found" 
                                                       otherwise i have a PDF generated with two 
                                                       lines of text, and i want that just under a 
                                                       self drawn graphic could be displayed 

              ],
            ),
          );//pw.Text(title),

        },
      ),
    );

    return pdf.save();
  }
}

You have to use the CustomPaint class instead of just drawing directly.您必须使用CustomPaint class 而不是直接绘图。

Try尝试

pw.CustomPaint(
  size: const PdfPoint(110, 110),
  painter: (PdfGraphics canvas, PdfPoint size) {
    canvas
      ..setColor(PdfColors.indigo)
      ..drawRect(10, 10, 100, 100)
      ..fillPath();
  },
);

instead of代替

pw.drawBox(10,10,100,100)

Check out the list of drawable shapes here: https://pub.dev/documentation/pdf/latest/pdf/PdfGraphics-class.html在此处查看可绘制形状的列表: https://pub.dev/documentation/pdf/latest/pdf/PdfGraphics-class.html


For those looking for more information对于那些寻找更多信息的人

(pdf's) CustomPaint expects a child and one or two painter functions. (pdf's) CustomPaint需要一个孩子和一两个画家函数。 Unlike Flutter's CustomPaint不像 Flutter 的CustomPaint

  • this uses a PdfGraphics instead of a Canvas这使用PdfGraphics而不是 Canvas
  • the painting functions are functions not CustomPainters绘画功能是不是 CustomPainters 的功能

I struggled for days to figure this out and found my answer on this comment: https://github.com/DavBfr/dart_pdf/issues/145#issuecomment-530798498我挣扎了好几天才弄清楚这一点,并在此评论中找到了答案: https://github.com/DavBfr/dart_pdf/issues/145#issuecomment-530798498

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

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