DavBfr / dart_pdf

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

App Crashing with big pdf on pdf.save() #432

Closed phcs971 closed 4 years ago

phcs971 commented 4 years ago

Describe the bug The app is crashes without any error when I have a big pdf and I'm saving it. The app needs to create a pdf for a photographic report, so it will have lots of photos.

To Reproduce Code snippet to reproduce the behavior:

final pdf = Document();
... // create a big pdf with big images
File f = File(path); //path generated with path provider
await f.create(recursive: true);
return await f.writeAsBytes(pdf.save());

Expected behavior The pdf saves to a file so the user can share it later

Flutter Doctor

[√] Flutter (Channel master, 1.22.0-10.0.pre.270, on Microsoft Windows [versão 10.0.19041.508], locale pt-BR)
    • Flutter version 1.22.0-10.0.pre.270 at C:\src\flutter
    • Framework revision 36a6ef6486 (19 hours ago), 2020-09-21 11:02:30 -0700
    • Engine revision 4b8477d115
    • Dart version 2.10.0 (build 2.10.0-142.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\Users\phcs9\AppData\Local\Android\sdk
    • Platform android-29, build-tools 29.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.0)
    • Visual Studio at D:\ProgramFiles\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.7.30330.147
    • Windows 10 SDK version 10.0.18362.0

[√] Android Studio (version 3.6)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 44.0.2
    • Dart plugin version 192.7761
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)

[√] VS Code (version 1.49.1)
    • VS Code at C:\Users\phcs9\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.14.1

[√] Connected device (5 available)
    • Moto Z3 Play (mobile) • 0048198433 • android-arm64  • Android 9 (API 28)
    • Windows (desktop)     • windows    • windows-x64    • Microsoft Windows [versão 10.0.19041.508]
    • Web Server (web)      • web-server • web-javascript • Flutter Tools
    • Chrome (web)          • chrome     • web-javascript • Google Chrome 85.0.4183.102
    • Edge (web)            • edge       • web-javascript • Microsoft Edge 85.0.564.51

• No issues found!

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context If there is a way to save the pdf by page it would help a lot

DavBfr commented 4 years ago

The full Pdf has to fit in memory to be processed.

phcs971 commented 4 years ago

The full Pdf has to fit in memory to be processed.

I see. Is there any way for me to save each page after the page is written? Like concatenating the bytes into the file?

DavBfr commented 4 years ago

No, it would require a major rewrite of the library to write it directly to the disk instead of memory.

fenchai23 commented 4 years ago

I see so not even isolates would solve this problem. OP have you found a workaround for this? maybe splitting the work?

DavBfr commented 4 years ago

If your images are too big, you can resize them first, and convert them to jpeg if not already using that.

fenchai23 commented 4 years ago

If your images are too big, you can resize them first, and convert them to jpeg if not already using that.

do you mean images inside the pdf? maybe that would work for OP, I am dealing with converting pdf into png files.

when I convert them to jpg instead of png every color is red-pinkish and the file size is increased by 500% and the speed is about the same as png.

and my pdf only contains texts and a few lines, and a logo, I doubt it uses that much memory, maybe it's the package algorithm itself, as you said it needs to load everything into memory first. I doubt I can fix that without splitting the work somehow, after 20 pages in a pdf the app will crash with oom error, worse when it also needs to convert into a PNG file.

Hopefully, there is a solution to this in the future. I'm hopeful and thanks for the package.

michhhh99 commented 2 years ago

pdf.save() is taking too long when build on web compared to mobile. Is there any way to resolve this?

DavBfr commented 2 years ago

@michelle010721 It's just Javascript, so yes it takes time. Until the Dart team implement a webassembly compilation we can't do much.

michhhh99 commented 2 years ago

Is there a way to prevent timeout, I’m encountering problem like “page not responding” when I run pdf.save() on web.

DavBfr commented 2 years ago

Run the PDF creation in a dart isolate.

michhhh99 commented 2 years ago

I ran through the isolate_test.dart but i don’t quite understand how it work. Any advice?

michhhh99 commented 2 years ago

Run the PDF creation in a dart isolate.

Try using dart isolate for pdf creation. but got throw an error. Error: Unsupported operation: dart:isolate is not supported on dart4web

Jeferson505 commented 2 years ago

Run the PDF creation in a dart isolate.

Try using dart isolate for pdf creation. but got throw an error. Error: Unsupported operation: dart:isolate is not supported on dart4web

Hello, @michhhh99 and @DavBfr.

I would like to know if any of you have managed a solution to this issue that does not give the last problem mentioned for web mode.

usamasindhu commented 4 weeks ago

Hi guys did anyone found any workaround this as isolates does not work in web and compute is supposed to work for non async. web page freezes for few seconds if there is any image in pdf. If anyone found any solution to this please mention

DavBfr commented 4 weeks ago

Use jpeg images, there is no processing and are embedded as is.