gpt4 book ai didi

flutter - 将 Flutter 应用程序屏幕导出为 PDF

转载 作者:行者123 更新时间:2023-12-03 03:55:19 26 4
gpt4 key购买 nike

是否有任何可能的方法将 flutter app screen/s 导出为 PDF,我正在寻找类似于屏幕截图功能,但它应该生成 PDF 而不是图片。

提前致谢

最佳答案

您可以通过以下方式使用 pdf/printing:

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

void main() {
runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatelessWidget {
final GlobalKey<State<StatefulWidget>> _printKey = GlobalKey();

void _printScreen() {
Printing.layoutPdf(onLayout: (PdfPageFormat format) async {
final doc = pw.Document();

final image = await WidgetWraper.fromKey(
key: _printKey,
pixelRatio: 2.0,
);

doc.addPage(pw.Page(
pageFormat: format,
build: (pw.Context context) {
return pw.Center(
child: pw.Expanded(
child: pw.Image(image),
),
);
}));

return doc.save();
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RepaintBoundary(
key: _printKey,
child:
// This is the widget that will be printed.
const FlutterLogo(
size: 300,
),
),
],
),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.print),
onPressed: _printScreen,
),
);
}
}

关于flutter - 将 Flutter 应用程序屏幕导出为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61563860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com