我想要生成PDF文件,包括自行绘制的图形在颤振应用程序。当然,在pdf库中,显示包含两行文本的pdf预览非常简单,但我希望能够插入一些我想自己绘制的图形,因为我需要(自己)绘制一些非常非常规的图形。为了做到这一点,我需要能够在pdf小部件中绘制(一些线条、曲线、点、几种颜色等)。到现在为止,我甚至没有画出一点!,pdf的颤振飞镖库描述了几十种方法,但没有显示任何例子,这实际上是一个小问题。有没有人能帮我“画”PDF飞镖内的图形。PdfLibrary包括PdfGraphics类,它应该有我尝试使用的方法,但没有成功!!
许多人提前感谢
请找到我的密码:
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();
}
}发布于 2022-06-27 16:00:19
您必须使用CustomPaint类,而不是直接绘图。
试一试
pw.CustomPaint(
size: const PdfPoint(110, 110),
painter: (PdfGraphics canvas, PdfPoint size) {
canvas
..setColor(PdfColors.indigo)
..drawRect(10, 10, 100, 100)
..fillPath();
},
);而不是
pw.drawBox(10,10,100,100)查看此处可绘制形状的列表:https://pub.dev/documentation/pdf/latest/pdf/PdfGraphics-class.html
为那些寻找更多信息的人
CustomPaint期望一个孩子和一两个画家函数。不像颤振的CustomPaint
我挣扎了几天才弄明白这一点,并在以下评论中找到了答案:pdf/issues/145#发行-530798498
https://stackoverflow.com/questions/67776282
复制相似问题