英文:
How to make pdf from widget fit to screen when widget is Scrollable?
问题
我使用这个示例代码成功地生成了一个PDF文件。它是从我的Desire小部件中生成的PDF。但是,如果小部件太长(可滚动),那么PDF中的图像就会变小,我必须放大它才能看到内容... 示例1 如果我使用 fit: pw.BoxFit.fill
,PDF中的小部件就会被挤压挤压。我可以让PDF适应A4大小的页面,或者只是适应屏幕或可滚动的PDF吗?
生成PDF的函数:
final GlobalKey<State<StatefulWidget>> printKey = GlobalKey();
final doc = pw.Document();
void _printScreen() async { //=============> PDF函数
final image = await WidgetWraper.fromKey(
key: printKey,
pixelRatio: 2.0,
);
final directory = await getTemporaryDirectory();
final file = File('${directory.path}/my_goal.pdf');
final pdf = pw.Document();
pdf.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return pw.FullPage(
ignoreMargins: true,
child: pw.Image(image, fit: pw.BoxFit.contain),
);
},
));
await file.writeAsBytes(await pdf.save());
await Share.shareFiles(
[file.path],
text: '查看我的目标详情',
subject: '我的目标详情',
);
}
在按钮按下时调用生成并共享PDF的函数:
IconButton(
onPressed: () async {
_printScreen();
},
icon: const Icon(Icons.share),
),
然后将我想要生成PDF的期望小部件包装起来:
return SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: RepaintBoundary(
key: printKey,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 这里是你的小部件内容
],
),
),
),
);
英文:
I'm successfully making a pdf with this sample code. It's making PDF from my Desire widget.
But if the widget is too long(Scrollable) then the image in pdf got small I have to zoom it to see the content...smaple1 If I do fit: pw.BoxFit.fill
the widget in PDF got squeeze.
Can I make the pdf fit to A4 size page, of just fit to screen or Scrollable pdf?
My function which making the pdf
final GlobalKey<State<StatefulWidget>> printKey = GlobalKey();
final doc = pw.Document();
void _printScreen() async { //=============> Pdf function
final image = await WidgetWraper.fromKey(
key: printKey,
pixelRatio: 2.0,
);
final directory = await getTemporaryDirectory();
final file = File('${directory.path}/my_goal.pdf');
final pdf = pw.Document();
pdf.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return pw.FullPage(
ignoreMargins: true,
child: pw.Image(image, fit: pw.BoxFit.contain),
);
},
));
await file.writeAsBytes(await pdf.save());
await Share.shareFiles(
[file.path],
text: 'Check out my goal details',
subject: 'My goal details',
);
}
calling the function to make and share pdf on button pressed
IconButton(
onPressed: () async {
_printScreen();
},
icon: const Icon(Icons.share),
),
Then wrap my desired widget from which I want pdf
return SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: RepaintBoundary( ||======>>>>>>>>>>>>>>>||
key: printKey,
child: Padding(
padding: const EdgeInsets.all(12.0),
child:
Column(crossAxisAlignment: CrossAxisAlignment.start,
children: [
答案1
得分: 1
你可以使用 MultiPage
doc.addPage(
pw.MultiPage(
build: (context) => [
_createTitleHeader(context),
//....
],
pageTheme: pageTheme,
footer: _buildFooter,
),
);
英文:
You can use MultiPage
doc.addPage(
pw.MultiPage(
build: (context) => [
_createTitleHeader(context),
//....
],
pageTheme: pageTheme,
footer: _buildFooter,
),
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论