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

App crash when using pageTheme and device goes from portrait to landscape #1012

Closed Arcttyx closed 1 year ago

Arcttyx commented 2 years ago

Describe the bug When I create a PDF single page (in my case) its all ok, but when I turn the device to landscape orientation app crash with following error:

I/testpdf(11254): Starting a blocking GC Alloc
I/testpdf(11254): Alloc concurrent copying GC freed 742(40KB) AllocSpace objects, 0(0B) LOS objects, 14% free, 142MB/166MB, paused 135us total 30.453ms
W/testpdf(11254): Throwing OutOfMemoryError "Failed to allocate a 147141758 byte allocation with 25165824 free bytes and 113MB until OOM, target footprint 174243144, growth limit 268435456" (VmSize 1954228 kB)
I/testpdf(11254): Starting a blocking GC Alloc
I/testpdf(11254): Alloc young concurrent copying GC freed 4(31KB) AllocSpace objects, 0(0B) LOS objects, 14% free, 142MB/166MB, paused 139us total 17.765ms
I/testpdf(11254): Starting a blocking GC Alloc
I/testpdf(11254): Alloc concurrent copying GC freed 4(16KB) AllocSpace objects, 0(0B) LOS objects, 14% free, 142MB/166MB, paused 124us total 32.894ms
I/testpdf(11254): Forcing collection of SoftReferences for 140MB allocation
I/testpdf(11254): Starting a blocking GC Alloc
I/testpdf(11254): Alloc concurrent copying GC freed 5(16KB) AllocSpace objects, 0(0B) LOS objects, 14% free, 142MB/166MB, paused 235us total 28.761ms
W/testpdf(11254): Throwing OutOfMemoryError "Failed to allocate a 147141760 byte allocation with 25165824 free bytes and 113MB until OOM, target footprint 174243144, growth limit 268435456" (VmSize 1954228 kB)
D/AndroidRuntime(11254): Shutting down VM
E/AndroidRuntime(11254): FATAL EXCEPTION: main
E/AndroidRuntime(11254): Process: com.mx.testpdf, PID: 11254
E/AndroidRuntime(11254): java.lang.OutOfMemoryError: Failed to allocate a 147141760 byte allocation with 25165824 free bytes and 113MB until OOM, target footprint 174243144, growth limit 268435456
E/AndroidRuntime(11254):    at java.util.Arrays.copyOf(Arrays.java:3161)
E/AndroidRuntime(11254):    at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
E/AndroidRuntime(11254):    at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
E/AndroidRuntime(11254):    at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:135)
E/AndroidRuntime(11254):    at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:238)
E/AndroidRuntime(11254):    at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:279)
E/AndroidRuntime(11254):    at io.flutter.plugin.common.StandardMethodCodec.encodeMethodCall(StandardMethodCodec.java:38)
E/AndroidRuntime(11254):    at io.flutter.plugin.common.MethodChannel.invokeMethod(MethodChannel.java:120)
E/AndroidRuntime(11254):    at io.flutter.plugin.common.MethodChannel.invokeMethod(MethodChannel.java:104)
E/AndroidRuntime(11254):    at net.nfet.flutter.printing.PrintingHandler.onPageRasterized(PrintingHandler.java:192)
E/AndroidRuntime(11254):    at net.nfet.flutter.printing.PrintingJob$3$1.run(PrintingJob.java:486)
E/AndroidRuntime(11254):    at android.os.Handler.handleCallback(Handler.java:883)
E/AndroidRuntime(11254):    at android.os.Handler.dispatchMessage(Handler.java:100)
E/AndroidRuntime(11254):    at android.os.Looper.loop(Looper.java:214)
E/AndroidRuntime(11254):    at android.app.ActivityThread.main(ActivityThread.java:7499)
E/AndroidRuntime(11254):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(11254):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/AndroidRuntime(11254):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)

To Reproduce Code snippet to reproduce the behavior:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:printing/printing.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;

class PdfPage extends StatelessWidget {
  PdfPage({Key? key) : super(key: key) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight,
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: PdfPreview(
            build: (format) {
              final pdf = pw.Document();

              pdf.addPage(
                pw.Page(
                  pageTheme: const pw.PageTheme(pageFormat: PdfPageFormat.a3),   //THIS, HERE
                  build: (pw.Context context) => pw.Center(
                    child: pw.Text('Hello World!'),
                  ),
                ),
              );

              return pdf.save();
            },
          ),
        ),
      ),
    );
  }
}

Expected behavior App does not crash when device goes from portrait to landscape

Screenshots

Flutter Doctor Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 2.10.4, on macOS 12.2.1 21D62 darwin-x64, locale es-MX) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) [✓] Xcode - develop for iOS and macOS (Xcode 13.3.1) [✓] Android Studio (version 4.2) [✓] VS Code (version 1.66.2) [✓] Connected device (2 available) [✓] HTTP Host Availability

• No issues found!

Desktop (please complete the following information):

Smartphone (please complete the following information):

Its necessary to activate the automatic rotation function of the device to rotate the cellphone

DavBfr commented 2 years ago

It's an issue to render the PDF: not enough memory on the device. Try to set a maxWidth parameter to PdfPreview

Arcttyx commented 2 years ago

It's an issue to render the PDF: not enough memory on the device. Try to set a maxWidth parameter to PdfPreview

t does not help at all But if I change pageFormat: PdfPageFormat.a3 to pageFormat: PdfPageFormat.standard it works better. Could be something related with that specific format a3

For now I'll try changing and using all my template with standard

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] commented 1 year ago

Closing this stale issue because it has no activity.