如何将整个flutter Screen转换为pdf?

xesrikrc  于 2023-05-18  发布在  Flutter
关注(0)|答案(5)|浏览(155)

我有一个Flutter的电子商务应用程序,其中有一个orderDetails页面,我想转换整个页面为PDF使用FlutterPDF包,因为它会更方便,因为数据是从FirebaseFirerestore检索。有没有办法让我做到这一点?
sub:我想使用this library将整个Flutter屏幕转换为PDF

yqkkidmi

yqkkidmi1#

您可以使用此软件包screenshot捕获屏幕
然后将该图像添加到PDF文档并保存

import 'dart:io';
import 'dart:typed_data';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart ' as pw;

Future getPdf(Uint8List screenShot) async {
  pw.Document pdf = pw.Document();
  pdf.addPage(
    pw.Page(
      pageFormat: PdfPageFormat.a4,
      build: (context) {
        return pw.Expanded(
          child: pw.Image(PdfImage.file(pdf.document, bytes: screenShot), fit: pw.BoxFit.contain)
        );
      },
    ),
  );
  File pdfFile = File('Your path + File name');
  pdfFile.writeAsBytesSync(pdf.save());
}
5q4ezhmt

5q4ezhmt2#

详细信息:在pubspec.yaml中加载依赖项:

screenshot: 
  share_plus: 
  path_provider:
  permission_handler:

在dart文件中导入包:

import 'package:screenshot/screenshot.dart';
import 'package:share_plus/share_plus.dart';
import 'package:permission_handler/permission_handler.dart';

  shareImage() async {

    final uint8List = await screenshotController.capture();
    String tempPath = (await getTemporaryDirectory()).path;
    String fileName ="myFile";
    if (await Permission.storage.request().isGranted) {
      File file = await File('$tempPath/$fileName.png').create();
      file.writeAsBytesSync(uint8List);
      await Share.shareFiles([file.path]);
    }
  }

并在任何地方调用shareImage():

GestureDetector(
   onTap: (){ shareImage();},
   child: ........,
  ),

记得把你的愿望小部件 Package 在:

Screenshot(
    controller: screenshotController,
    child: Text("This text will be captured as image"),
),
20jt8wwn

20jt8wwn3#

newest solution will be
    shareImage() async {
    final uint8List = await screenshotController.capture();
        String tempPath = (await getTemporaryDirectory()).path;
    String fileName =" ";>>>>>your file name between ""
    File file = await File('$tempPath/$fileName" }.png').create();
    file.writeAsBytesSync(uint8List!);
     await Share.shareFiles([file.path]);
    }
4c8rllxm

4c8rllxm4#

import 'dart:io';
import 'dart:typed_data';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart ' as pw;
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';

Future screenToPdf(String fileName,Uint8List screenShot) async {
  pw.Document pdf = pw.Document();
  pdf.addPage(
    pw.Page(
      pageFormat: PdfPageFormat.a4,
      build: (context) {
        return pw.Expanded(
          child: pw.Image(pw.MemoryImage(screenShot), fit: pw.BoxFit.contain),
        );
      },
    ),
  );
  String path = (await getTemporaryDirectory()).path;
  File pdfFile = await File('$path/$fileName.pdf').create();

  pdfFile.writeAsBytesSync(await pdf.save());
  await Share.shareFiles([pdfFile.path]);
}
bfhwhh0e

bfhwhh0e5#

你需要

1. flutter pub add pdf
 2. flutter pub add screenshot

然后

Future<void> getPdf(Uint8List screenShot) async {
      final pdf = pw.Document();
      pdf.addPage(
        pw.Page(
          pageFormat: PdfPageFormat.a4,
          build: (context) {
            return pw.Center(
              child: pw.Image(pw.MemoryImage(screenShot), fit: pw.BoxFit.contain),
            );
          },
        ),
      );
      final output = File('Your path + File name');
      await output.writeAsBytes(await pdf.save());
    }

相关问题