DavBfr / dart_pdf

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

Generated PDF content is broken and can no longer be displayed in the PDF preview #1314

Closed bummsa closed 1 year ago

bummsa commented 1 year ago

Describe the bug

When upgrading the printing + pdf library (and also image library to 4.0.15), I was not able to display pdf content anymore

To Reproduce Code snippet to reproduce the behavior: This is how I generated a dummy pdf content.

    final doc = pw.Document();
    doc.addPage(
      pw.Page(
        pageFormat: PdfPageFormat.a4,
        build: (pw.Context context) {
          return pw.Center(
            child: pw.Text('Title page'),
          ); // Center
        },
      ),
    );
    for (int i = 0; i < 2; i++) {
      doc.addPage(
        pw.Page(
          pageFormat: PdfPageFormat.a4,
          build: (pw.Context context) {
            return pw.Center(
              child: pw.Text('Content page ${i + 1}'),
            ); // Center
          },
        ),
      );
    }
    dev.log("dummy document is generated");

PdfViewer:

PdfPreview(
      maxPageWidth: MediaQuery.of(context).size.width * 0.8,
      build: (format) => provider.getDocumentBytes(),
      canDebug: false,
      useActions: false,
    ),

In provider.getDocumentBytes(), we do return the value of the function Document.save()

Expected behavior

Be able to open the generated PDF file with no errors in Acrobat and use PdfPreview to view the content inside the app.

Screenshots

This error occurs when I have saved the file and opened it in Acrobat: error in acrobat If you click OK, you can view the content. pdf content

PdfPreview: Pdfpreview

Flutter Doctor

[!] Flutter (Channel unknown, 3.7.1, on Microsoft Windows [Version 10.0.19045.2604], locale en-GB)
    ! Flutter version 3.7.1 on channel unknown at d:\install\software\flutter\3.7.1
      Currently on an unknown channel. Run `flutter channel` to switch to an official channel.
      If that doesn't fix the issue, reinstall Flutter by following instructions at
      https://flutter.dev/docs/get-started/install.
    ! Unknown upstream repository.
      Reinstall Flutter by following instructions at https://flutter.dev/docs/get-started/install.
    • Framework revision 7048ed95a5 (8 weeks ago), 2023-02-01 09:07:31 -0800
    • Engine revision 800594f1f4
    • Dart version 2.19.1
    • DevTools version 2.20.1
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly
      to perform update checks and upgrades.

[√] Windows Version (Installed version of Windows is version 10 or higher)

[!] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at E:\nonsense\AppData\AndroidSdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: D:\install\software\Java\jdk-11.0.17+8-original\bin\java
    • Java version OpenJDK Runtime Environment Temurin-11.0.17+8 (build 11.0.17+8)
    ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses

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

[√] Visual Studio - develop for Windows (Visual Studio Build Tools 2022 17.5.2)
    • Visual Studio at E:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools
    • Visual Studio Build Tools 2022 version 17.5.33502.453
    • Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2021.3)
    • Android Studio at E:\Program Files\Android\Android Studio
    • 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 11.0.13+0-b1751.21-8125866)

[!] Android Studio
    • Android Studio at E:\Program
    • 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
    X android-studio-dir = E:\Program
    X Android Studio not found at E:\Program
    • Try updating or re-installing Android Studio.
    • Consider removing your android-studio-dir setting by running:
      flutter config --android-studio-dir=

[√] IntelliJ IDEA Community Edition (version 2022.1)
    • IntelliJ at D:\install\software\IntelliJ IDEA Community Edition 2022.1
    • Flutter plugin version 70.0.4
    • Dart plugin version 221.5588

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19045.2604]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 111.0.5563.111
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 111.0.1661.54

[√] HTTP Host Availability
    • All required HTTP hosts are available

Desktop (please complete the following information):

The problem is reproducible on Android (Emulator) and Windows - before upgrading the libraries (including the image library) it works perfectly. Other platforms are not tested.

Arcttyx commented 1 year ago

I am also getting this error message "The font Helvetica-bold contains incorrect bad /widths" when I open my generated file in Acrobat program on Windows

pdf version 3.10.1 printing 5.10.2

pdf version 3.9.0 does not have this problem

DavBfr commented 1 year ago

I'll check that.

DavBfr commented 1 year ago

The master branch should fix it.