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



有没有任何可能的方法将flutter应用程序屏幕导出为PDF,我想要的是类似于屏幕截图功能,但它应该生成PDF而不是图片。

提前感谢

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

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,
),
);
}
}

最新更新