Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can pdf_crypto load all kinds of pdf files? #1823

Open
kaciula opened this issue Mar 2, 2025 · 6 comments
Open

Can pdf_crypto load all kinds of pdf files? #1823

kaciula opened this issue Mar 2, 2025 · 6 comments
Labels
needs triage support Asking for help writing an application

Comments

@kaciula
Copy link

kaciula commented Mar 2, 2025

There are some PDF files that cannot be rendered at all on iOS but can be rendered on Android. I want to know if pdf_crypto can load these particular PDF files. I think it's a compression-related issue. If I read the pdf file with ghostscript and write it back, it successfully renders.

If pdf_crypto can read these PDF files, my client would like to purchase the licence. But first we need to know if it works. I can privately share some of these PDFs and you can confirm that it works. How should we proceed?

@kaciula kaciula added needs triage support Asking for help writing an application labels Mar 2, 2025
@DavBfr
Copy link
Owner

DavBfr commented Mar 2, 2025

pdf_crypto doesn't render pdf, it only allow to modify them and add some cryptography.

@kaciula
Copy link
Author

kaciula commented Mar 3, 2025

@DavBfr I misspoke. I don't need pdf_crypto to render the PDF. I just need to parse it so I can modify it so it can become renderable using other pdf rendering plugins out there. I just want to know if it can parse these PDFs.

@kaciula
Copy link
Author

kaciula commented Mar 5, 2025

@DavBfr Can I send you some pdfs that you can test if loading with pdf_crypto and then saving them with the pdf plugin works?

For example, the pdfs that don't render on iOS become renderable if I simply load and rewrite the pdf with Ghostscript.

gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf blank.pdf

https://teikningar.rnb.is/rnes_kort/Rnb----9721.pdf
https://teikningar.rnb.is/rnes_kort/Rnb-----9724.pdf
https://teikningar.rnb.is/rnes_kort/Rnb-----9730.pdf
https://teikningar.rnb.is/rnes_kort/Rnb-----9732.pdf

@DavBfr
Copy link
Owner

DavBfr commented Mar 5, 2025

I tested your files:

    final original = PdfDocumentParser(
        await File('Rnb-----9730.pdf').readAsBytes());

    final tools = PdfTools(
      compress: false,
      verbose: true,
    );
    tools.addPages(original);

    for (final o in tools.xref.objects) {
      final p = o.params;
      if (p is PdfDictStream) {
        if (p.values['/Subtype'] == PdfName('/Image') &&
            p.values['/Filter'] == PdfName('/DCTDecode')) {
          print(p.values);
          File('image_${o.objser}.jpg').writeAsBytesSync(p.decode().data);
          print('Saved image_${o.objser}.jpg');
        }
      }
    }

    final result = tools.save();
    final file = File('template.pdf');
    await file.writeAsBytes(result);

The DCTDecode filter represents an embedded Jpeg image. This particular image is 10012x7136 pixels, but the file seems corrupted. the iOS/MacOS jpeg decoder can't read it. Some software can open it, but shows an image of 10012x65280 pixels.

If you can find a library on iOS that can read this jpeg data, it should be possible to rewrite the PDF with some correct data.

Image

@DavBfr
Copy link
Owner

DavBfr commented Mar 5, 2025

Chrome / Safari can't display the attached image, but Firefox can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage support Asking for help writing an application
Projects
None yet
Development

No branches or pull requests

2 participants