DavBfr / dart_pdf

Pdf creation module for dart/flutter
https://pub.dev/packages/pdf
Apache License 2.0
1.41k stars 630 forks source link

Updating PDF page #1657

Open LuisHer27 opened 6 months ago

LuisHer27 commented 6 months ago

Describe the bug

To Reproduce Code snippet to reproduce the behavior:

Future<File> getFile() async {
  final data = await rootBundle.load('assets/participant_agreement.pdf');
  final bytes = data.buffer.asUint8List();
  final dir = await getApplicationDocumentsDirectory();
  final file = File('${dir.path}/form.pdf');

  final assetFile = await file.writeAsBytes(bytes);

  return assetFile;
}

class PdfEditorService {
  const PdfEditorService._();

  static Future<File> addSignatureToPDF(Uint8List signature) async {
    try {
      final font = await PdfGoogleFonts.nunitoExtraLight();

      final file = await getFile();
      final form = file.readAsBytesSync();

      final image = MemoryImage(signature);

      final document = PdfDocumentParser(
        form,
      );

      final lastPage = document.pages.length - 1;

      final themePage = PageTheme(
        pageFormat: PdfPageFormat.a4,
        buildBackground: (context) {
          return Container(color: PdfColors.blue);
        },
      );

      final pdf = Document.load(document)
        ..editPage(
          lastPage,
          Page(
            pageTheme: themePage,
            build: (context) {
              return Column(
                children: [
                  Text(
                    'Participant Agreement Form',
                    style: TextStyle(
                      font: font,
                      fontSize: 32,
                    ),
                  ),
                  SizedBox(height: 12),
                  Text(
                    'Name: Anakin Skywalker',
                    style: TextStyle(
                      font: font,
                      fontSize: 32,
                    ),
                  ),
                  Positioned(
                    top: 0,
                    left: 0,
                    child: Column(
                      children: [
                        Text(
                          'Signature',
                          style: TextStyle(
                            font: font,
                            fontSize: 32,
                          ),
                        ),
                        SizedBox(height: 20),
                        Image(image),
                        SizedBox(height: 8),
                        Container(
                          height: 1,
                          width: 200,
                          decoration: BoxDecoration(
                            border: Border.all(),
                          ),
                        ),
                        SizedBox(height: 10),
                        Text(
                          'Date: 2021-09-01',
                          style: TextStyle(
                            font: font,
                            fontSize: 32,
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              );
            },
          ),
        );

      final dir = await getApplicationDocumentsDirectory();
      final signedForm = File('${dir.path}/form.pdf');
      return await signedForm.writeAsBytes(await pdf.save());
    } catch (e) {
      log(e.toString());
      throw Exception('Error adding signature to PDF');
    }
  }
}

Expected expected_behavior.pdf behavior

Screenshots

Screenshot 2024-05-02 at 10 45 43 a m

Flutter Doctor

[✓] Flutter (Channel stable, 3.19.6, on macOS 14.2.1 23C71 darwin-arm64, locale en-MX)
    • Flutter version 3.19.6 on channel stable at /Users/huicho/Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 54e66469a9 (2 weeks ago), 2024-04-17 13:08:03 -0700
    • Engine revision c4cd48e186
    • Dart version 3.3.4
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/huicho/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    ✗ Unable to get list of installed Simulator runtimes.
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)

[✓] VS Code (version 1.86.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.84.0

[✓] Connected device (5 available)
    • sdk gphone64 arm64 (mobile) • emulator-5556             • android-arm64  • Android 14 (API 34) (emulator)
    • Iphone Huicho (mobile)      • 00008120-00086C122120C01E • ios            • iOS 17.4.1 21E236
    • Iphone Luis (mobile)        • 00008101-001C48940C29001E • ios            • iOS 17.4.1 21E236
    • macOS (desktop)             • macos                     • darwin-arm64   • macOS 14.2.1 23C71 darwin-arm64
    • Chrome (web)                • chrome                    • web-javascript • Google Chrome 124.0.6367.94
    ! Device emulator-5554 is offline.

[✓] Network resources
    • All expected network resources are available.

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context

WANGNEWMAN commented 1 month ago

same error: pw.Document pdf = pw.Document.load(PdfDocumentParser(localSource!.readAsBytesSync()),); pdf.editPage(controller.index, pw.Page(build:(context) { // return pw.Image(pw.ImageProxy(img, dpi: dpi)); // return pw.FullPage(ignoreMargins: true, child: pw.Image(pw.ImageProxy(img, dpi: dpi))); return pw.Center( child: pw.Text('hello word', textAlign: pw.TextAlign.center, textDirection: pw.TextDirection.ltr) ); },)); when the width bigger than heigh. resault is normal,but width less than height is error