如何使可滚动的部件在生成 PDF 时适应屏幕?

huangapple go评论66阅读模式
英文:

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&lt;State&lt;StatefulWidget&gt;&gt; printKey = GlobalKey();
final doc = pw.Document();

void _printScreen() async { //=============&gt; Pdf function
  final image = await WidgetWraper.fromKey(
    key: printKey,
    pixelRatio: 2.0,
  );

  final directory = await getTemporaryDirectory();
  final file = File(&#39;${directory.path}/my_goal.pdf&#39;);
  
  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: &#39;Check out my goal details&#39;,
    subject: &#39;My goal details&#39;,
  );
}

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( ||======&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;||
                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) =&gt; [
          _createTitleHeader(context),
          //....
        ],
        pageTheme: pageTheme,
        footer: _buildFooter,
      ),
    );
英文:

You can use MultiPage

 doc.addPage(
      pw.MultiPage(
        build: (context) =&gt; [
          _createTitleHeader(context),
          //....
        ],
        pageTheme: pageTheme,
        footer: _buildFooter,
      ),
    );

huangapple
  • 本文由 发表于 2023年3月9日 19:45:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定