Skip to content

Inclusion of PNG in PDF without reencoding #7668

Open

Description

@Enivex

Description

As evidenced by https://github.com/josch/img2pdf, it is possible to directly embed raw png data in pdf without decoding and re-encoding. This has some benefits

(Could also be relevant for some other formats if typst currently does not do so)

For JPEG, JPEG2000, non-interlaced PNG, TIFF images with CCITT Group 4 encoded data, and JBIG2 with single-page generic coding (e.g. using jbig2enc), img2pdf directly embeds the image data into the PDF without re-encoding it. It thus treats the PDF format merely as a container format for the image data. In these cases, img2pdf only increases the filesize by the size of the PDF container (typically around 500 to 700 bytes). Since data is only copied and not re-encoded, img2pdf is also typically faster than other solutions for these input formats.

Activity

  1. added
    feature request
    pdf
    change request
    perf
    and removed
    feature request
    on Jan 9, 2026
  2. jqwv commented on Jan 18, 2026

    @jqwv

    This was considered as not possible for JPEG because the file can be invalid. Without decoding it, it would not be possible to validate if "file.jpg" or "file.png" are expected image files.
    As mentioned in the linked issue, currently an inconsistent "hack" exists to already allow what you request: embed the file in a PDF, then all next times embed the PDF instead of the PNG/JPG. For PDF embedding, the file contents are currently not checked and just copied instead, even if the PDF is invalid.

    Related to this (because not many people know this): PNG and JPG have the same API (iteration by scanlines). A very efficient implementation could subsample an image (for cropped images, or even scaled images) without decoding it completely. This is done by skipping each Y scanlines and each X columns. The problem is that only the C implementations support this. The rust image people all have ignored this. As a result, decoding JPG and PNG in Rust always requires a full decompression, I think even to RGBA even if it's greyscale or monochrome, even if we could crop and/or scale an image from a big 50 MB image with only constant memory requirement.

  3. Enivex commented on Jan 18, 2026

    @Enivex
    CollaboratorAuthor

    Even if you need to verify that an image is valid by decoding, you can still embed after doing that instead of reencoding.

  4. jqwv commented on Jan 18, 2026

    @jqwv

    Yes, this is what is done with JPG. But decoding the image is required. Reencoding is not.
    Your issue asked for "without decoding".

  5. Enivex commented on Jan 18, 2026

    @Enivex
    CollaboratorAuthor

    Yes, this is what is done with JPG. But decoding the image is required. Reencoding is not. Your issue asked for "without decoding".

    I mentioned both, but the no reencoding would be by far the most important. Encoding is generally much slower than decoding. Also, crucially, we wouldn't undo the effort of png optimizers.

  6. changed the title [-]Inclusion of png in pdf without reencoding[/-] [+]Inclusion of PNG in PDF without reencoding[/+] on Jan 26, 2026
  7. andylizi commented on Feb 2, 2026

    @andylizi
    Contributor

    Most of the work would be done in krilla. I've made an attempt at LaurenzV/krilla#331

  8. TechPizzaDev commented on Sep 26, 2026

    @TechPizzaDev

    I'd like to move this along, especially with LaurenzV/krilla#415 merged, but krilla has not made a release with all the changes yet.
    @LaurenzV do you have any release plans to unblock this?

  9. LaurenzV commented on Sep 26, 2026

    @LaurenzV
    Collaborator

    I believe typst already uses a git revision that contains the change, so shouldn't be blocked on a new release.

  10. TechPizzaDev commented on Sep 26, 2026

    @TechPizzaDev

    Oh yeah, it's pointing at 7772dbe, from July 31st. That makes it easier, sorry about the ping!

  11. Enivex commented on Sep 26, 2026

    @Enivex
    CollaboratorAuthor

    Is the implementation in krilla complete?

    Ideally this should also happen for jpg if it doesn't already.

  12. LaurenzV commented on Sep 26, 2026

    @LaurenzV
    Collaborator

    Pretty sure JPEG already works this way.

    It is "complete" for opaque PNG images. You might be able to get some savings with certain types of transparent PNGs as well, but all the low-hanging fruits (and the most important cases) are already implemented.

  13. andylizi commented on Sep 26, 2026

    @andylizi
    Contributor

    @TechPizzaDev Thanks for the reminder, I didn't notice the version bump either! I already have some integration code from before, will put up a PR in a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    change requestpdfperf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions