Skip to content

Allow limiting the image resolution #2554

Open

Description

@Beiri22

Description

When using large images that are scaled down, the pdf size will not decrease by downscaling. There should be an option to limit the resolution of pixel images in the resulting PDF.

  • This could be an option to image which could be set globally or locally like set image(max-res: 300)
  • or some kind of pdf export setting for the cli.

When talking about that topic, we could also think of other related options like image compression settings in pdf.

Use Case

It might be useful for everyone who uses high-res images in their typst document, especially when they face pdf file size restrictions (maybe for submission).

Activity

  1. added
    feature request
    on Nov 1, 2023
  2. lumpiluk commented on Nov 2, 2023

    @lumpiluk

    I read this in #404 a couple of days ago:

    We plan to add support for instructing Typst to recompress images at a specific DPI level as an export option in the future.

  3. added
    pdf
    on Nov 2, 2023
  4. Beiri22 commented on Jul 9, 2024

    @Beiri22
    ContributorAuthor

    Nice to read that, but is there any progress on this topic, since?

  5. Ownezx commented on Jan 6, 2026

    @Ownezx

    For now there is a workaround using ghost script

    gs -sDEVICE=pdfwrite -dPDFSETTINGS=/printer -dNOPAUSE -dBATCH -sOutputFile=compressed.pdf main.pdf
    
  6. c-harding commented on Sep 18, 2026

    @c-harding

    @Ownezx That workaround doesn't seem to support the document metadata though, which means that text is copied with hard wrapping, and screen readers lose the reading order.

    Typst emits a tagged PDF — on 0.15.1 my document has a /StructTreeRoot and 132 /S/P elements. After gs -sDEVICE=pdfwrite (Ghostscript 10.06.0) there is no /StructTreeRoot and no /Marked entry at all. -dPreserveMarkedContent=true makes no difference, with or without -dPDFUACompatibilityPolicy=1. It isn't a missing flag: pdfwrite re-interprets each page into fresh content streams, and the structure tree refers into the old ones by MCID, so by the time it writes there is nothing left to point at.

    Two other things worth knowing if you do use it:

    • Every -dPDFSETTINGS preset downsamples with /ColorImageDownsampleType /Average, a box filter. Asking for /Bicubic explicitly looks better and comes out smaller — on a 76 MB document (ten camera-resolution photos and twelve rendered PNG maps), /printer gives 6.1 MB and this gives 3.6 MB:

      gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dQUIET \
         -dDownsampleColorImages=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution=300 \
         -dDownsampleGrayImages=true  -dGrayImageDownsampleType=/Bicubic  -dGrayImageResolution=300 \
         -sOutputFile=compressed.pdf main.pdf
      
    • It re-encodes lossless images as JPEG. For photographs that's the point; for screenshots, diagrams or rendered maps it isn't. -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode keeps them lossless, but applies to every image, so you can't have JPEG photos and lossless line art in one pass — in my case that's the difference between 3.6 MB and 24 MB.

  7. wojpawlik commented on Nov 6, 2026

    @wojpawlik
    mutool clean -gzZ \
      --color-image-recompress-method=same \
      --color-image-subsample-method=bicubic \
      --color-image-subsample-dpi=300 \
      main.pdf compressed.pdf
    • -g is required to reduce file size.
    • --color-image-recompress-method is required to subsample.
      same keeps lossless images lossless.
    • --color-image-subsample-method=bicubic indeed reduces file size.
      Requires specifying --color-image-subsample-dpi.
    • There are equivalent options for gray and bitonal images.
    • --structure does nothing. It's always kept as of 1.28.6.

    Docs: https://mupdf.readthedocs.io/en/latest/tools/mutool-clean.html

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

    feature requestpdf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions