Skip to content

Support directly embedding PNGs in PDF without transcoding - #8898

Open
andylizi wants to merge 3 commits into
typst:mainfrom
andylizi:png-direct-embed
Open

andylizi wants to merge 3 commits into
typst:mainfrom
andylizi:png-direct-embed

Conversation

@andylizi

@andylizi andylizi commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

This PR hooks up the new krilla code path for directly embedding PNGs in PDF without transcoding (LaurenzV/krilla#331). Fixes #7668.

This change is expected to reduce PDF file sizes for documents containing PNGs, though files may end up larger if the input images are poorly compressed. Performance will also improve due to skipping the re-encoding step (see benchmark).

The following PNG types support direct embedding:

  • RGB without an alpha (transparency) channel
  • Grayscale
  • Indexed/palette color

The following are not supported due to PDF limitations:

  • RGBA (RGB with an alpha channel)
  • RGB images using tRNS chunk for transparency
  • Interlaced images of any kind

Unsupported images will fall back to transcoding as before.

Testing

  • The new image-png-indexed test has been manually inspected to confirm the embedding is working correctly.
  • In a real-world document with several PNGs, file size dropped from 480 KB to 304 KB (a ~36% reduction).

oxipng is a good tool for compressing PNGs ahead of time and is also available online via https://squoosh.app

// JPEG/PNG have special handing in PDF export (no recoding, so instead we use a
// transform to apply the orientation), so it's worth testing that separately.
`PNG`, ..rotations.map(v => with-rotation("/assets/images/f2t.png", 0x85, v)),
`JPEG`, ..rotations.map(v => with-rotation("/assets/images/f2t.jpg", 0x31, v)),

@andylizi andylizi Sep 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exif transform test previously covered both directly embedded JPEGs and transcoded PNGs to exercise both code paths. After this change, PNGs are also directly embedded. I suggest adding another image format (like webp) to this test to catch future regressions. Thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now after the #8898 (comment) fix, the specific PNG used in this test went back to skipping the transform, so it no longer covers the "directly embedding PNG" code path... We might need to add a version of f2t.png without alpha for better coverage.

@Enivex

Enivex commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

In a real-world document with several PNGs, file size dropped from 480 KB to 304 KB (a ~36% reduction).

How about compilation time? I'd expect it's faster as well

@andylizi

Copy link
Copy Markdown
Contributor Author

How about compilation time? I'd expect it's faster as well

Great question. I did some very unscientific measurements using a synthetic benchmark:

  • 50 photos download from Wikipedia, each around 3840px wide.
  • Converted to 8-bit RGB and compressed via oxipng -o max --fast -z --zi 2 --timeout 45
  • Total PNG file size: 893 MB
  • Script:
    #for i in range(1, 50, inclusive: true) {
      lorem(500)
      image("img_" + str(i) + ".png")
    }
  • On a 8-core 16-thread CPU after one round of warmup:
    • typst 0.15.1: took 10.4 seconds and produced 1297 MB pdf
    • this branch: took 5.9 seconds and produced 894 MB pdf (~43% decrease in time and ~31% decrease in size)

@andylizi andylizi changed the title Support embedding a number of PNGs directly in PDF Support directly embedding PNGs in PDF without transcoding Sep 27, 2026
Comment thread crates/typst-pdf/src/image.rs Resolved
Comment thread crates/typst-pdf/src/image.rs Outdated
)
} else if let RasterFormat::Exchange(ExchangeFormat::Png) = raster.format() {
let image_data = krilla::Data::from(raster.data().to_vec());
krilla::image::Image::from_png(image_data, interpolate)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a new from_png_native constructor: LaurenzV/krilla#435

We should use this, and in case it returns an error drop into the else path. This way, we can avoid decoding the image inside of krilla, which is wasteful since we already have a decoded representation of the image.

@andylizi andylizi Sep 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Do we want to bump krilla version as part of this PR, or leave this as future improvement for the next routine bump?

... I guess there would be a performance regression if we leave it as-is, that'd be unfortunate.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, up to @laurmaedje.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Krilla was bumped in #9022, I've applied the fix.

@Enivex

Enivex commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

How about compilation time? I'd expect it's faster as well

Great question. I did some very unscientific measurements using a synthetic benchmark:

  • 50 photos download from Wikipedia, each around 3840px wide.
  • Converted to 8-bit RGB and compressed via oxipng -o max --fast -z --zi 2 --timeout 45
  • Total PNG file size: 893 MB
  • Script:
    #for i in range(1, 50, inclusive: true) {
      lorem(500)
      image("img_" + str(i) + ".png")
    }
  • On a 8-core 16-thread CPU after one round of warmup:
    • typst 0.15.1: took 10.4 seconds and produced 1297 MB pdf
    • this branch: took 5.9 seconds and produced 894 MB pdf (~43% decrease in time and ~31% decrease in size)

Off-topic, but in my experience zopfli just isn't worth it, especially if you have to resort to lowering other settings (which you do for larger images since zopfli is dreadfully slow). Just oxipng -o max alone will get you like 99.5% there.

@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Sep 28, 2026
@LaurenzV

LaurenzV commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

I just realized, will PNG images with exif rotation be handled correctly in the new path?

@andylizi

andylizi commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

I just realized, will PNG images with exif rotation be handled correctly in the new path?

@LaurenzV Yes, there is an existing test for that! Which is awesome because I would not have remember otherwise 😂. See the comment here.

@LaurenzV

LaurenzV commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Ah whoops, I missed that!

@Enivex

Enivex commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

Will this also prevent the decoded image from sticking around in memory? It would certainly improve memory usage for documents with a lot of images.

@LaurenzV

Copy link
Copy Markdown
Collaborator

I think not, but this needs a different on the typst side, not in krilla.

@Enivex

Enivex commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

I think not, but this needs a different on the typst side, not in krilla.

This is in the typst repo 😅

@LaurenzV

Copy link
Copy Markdown
Collaborator

Ah yeah I know, just wanted to clarify 😄

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

Labels

pdf Related to PDF export or PDF embedding. waiting-on-review This PR is waiting to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inclusion of PNG in PDF without reencoding

5 participants