Skip to content

Export target elements #4174

Closed
Closed

Description

@tingerrr

Description

I propose adding document-like set-rule only elements for export targets to control target specific settings:

  • png
    • controlling PPI from within Typst documents
      • although this may open a small DOS attack surface from template packages choosing astronomical values
  • pdf
    • PDF document metadata, which is currently set using document

Use document either for only the common settings or as a convenient default for various settings from multiple targets.

Alongside this, a variable such as target should be added, which is set depending on the current export target. This allows code to act according to the export target, preventing incorrect output or panics when a target specific feature is used, such as Forms #2421 or OCGs #2613.

The newly added elements and variable could be put into an export module, i.e. set export.pdf(...), if export.target == export.pdf { ... }, as they should not require easy access for the majority of users.

While I thought about the target variable being the element of the active export target for nice looking checks (target == pdf), this comes with some downsides:

  • elements with no fields must be available too (svg)
  • sub-targets, such as PDF/A-2 support #2942 are not easily accounted for unless they receive potentially useless sub-elements like pdf.a2 or pdf.x
  • if sub-elements are added, they would complicate the simple checks as the equality would no longer hold target == pdf would be false for target == pdf.a2, users would need to use target in (pdf, pdf.a2, pdf.x), listing all possible sub-targets

Using strings solves this more easily, as these sub-target checks could use string methods like target.starts-with("pdf") or even target.starts-with("pdf/a") for all versions of PDF/A.

Most of the sub-target specific settings will likely live on the "encompassing" element anyway (i.e. pdf for PDF/A).

Use Case

  1. Export target specific features which are likely to be more "low-level" than normal Typst features if they can't be easily generalized will likely need packages to be made usable for the average Typst user.

    Such packages need to be able to:

    • use export target specific features without panics (if they do when used on an unsupported target)
    • provide reasonable fallback behavior, when the target specific feature is not supported
      • this is especially important for preview of features which are not available for PNGs, such as forms
      • provide example images of target specific features without the need to take screenshots of PDF viewers to show of export target specific packages
  2. Export targets such as HTML HTML Export #721 likely need more export target specific code, as it is fundamentally different from how Typst currently works.

Activity

  1. added
    feature request
    on May 17, 2024
  2. emilyyyylime commented on May 18, 2024

    @emilyyyylime
    Contributor

    target sounds like it'd fit inside the sys module, but I'm not too against export either

  3. added
    change request
    scripting
    and removed
    feature request
    on May 24, 2024
  4. laurmaedje commented on Jun 11, 2024

    @laurmaedje
    Member

    I think the question of a target variable and elements for setting target-specific metadata is fairly separate.

    I want to have a target variable (and I agree that sys would be a good place for it), but I'm not sure how fine-grained it should be, e.g. whether we should make it possible to distinguish SVG and PNG. Potentially, it could be a type with a few methods like is-html() and is-pdf() rather than a enum or a string. However, it also feels a bit over-engineered and unnecessarily complex. Maybe there isn't much extra harm in allowing SVG and PNG to be distinguished as well.

    As for the target-specific metadata: I see how adding it all to document is not great, but I'm not sure configuring the DPI for PNG from within Typst is really necessary. To me, it seems just like the other command line flags, which are also not configurable from within Typst (which is a separate story and maybe not for all of them great, but allowing specifically DPI just because it is target-specific feels strange).

    I would prefer to get some experience with target-specific things with target and forms before settling on a design for target-specific metadata.

  5. tingerrr commented on Jun 11, 2024

    @tingerrr
    ContributorAuthor

    To me, it seems just like the other command line flags, which are also not configurable from within Typst (which is a separate story and maybe not for all of them great, but allowing specifically DPI just because it is target-specific feels strange).

    Yeah, to be completely honest, it probably only serves as an example of things that one may want to control for other output formats. Since I haven't really touched SVG at all, yet I wouldn't know if there is anything for that and for PNG this is the only thing that seemed similar to PDF specific metadata. It will probably be more relevant for HTML.

    I would prefer to get some experience with target-specific things with target and forms before settling on a design for target-specific metadata.

    Somewhat understandable, but it does impose something on PDF forms, namely that they must always be exported at this moment if there is no way to turn them off conditionally. But maybe their implementation will help in exploring the right API.

  6. laurmaedje commented on Jun 11, 2024

    @laurmaedje
    Member

    Somewhat understandable, but it does impose something on PDF forms, namely that they must always be exported at this moment if there is no way to turn them off conditionally.

    I don't follow. Wouldn't a sys.target allow us to do that? My comment was only about the set pdf(..).

  7. tingerrr commented on Jun 11, 2024

    @tingerrr
    ContributorAuthor

    I don't follow. Wouldn't a sys.target allow us to do that? My comment was only about the set pdf(..).

    Oh, pardon me, I misunderstood.

  8. laurmaedje commented on Jun 13, 2024

    @laurmaedje
    Member

    I had a thought regarding sys.target: We might need the mechanism to allow for local changes. E.g. in HTML export we could need a way to layout some piece of content (e.g. a figure) with Typst's layout and produce an SVG. So maybe we need some contextual mechanism.

  9. tingerrr commented on Jun 13, 2024

    @tingerrr
    ContributorAuthor

    Would this need another mechanism to expose the value then? Since it can't just be a regular value on a module if it's contextual I believe.

  10. laurmaedje commented on Jun 13, 2024

    @laurmaedje
    Member

    It would need to be a function.

  11. laurmaedje commented on Jun 13, 2024

    @laurmaedje
    Member

    Or a style property I guess

  12. tingerrr commented on Jun 16, 2024

    @tingerrr
    ContributorAuthor

    One problem with it being contextual is that anything else it depends on ends up being contextual too. I wonder if this can actually cause any deeper problems or not, ideally this property should only influence small portions of a document so it would probably be fine.

    Style properties for modules seem more idiomatic for this. If you don't mind, I would go ahead and implement style properties for modules first, as this is currently an element function only thing. And then rebase #4385 on top of that.

  13. laurmaedje commented on Jun 16, 2024

    @laurmaedje
    Member

    Wait, style properties on modules seem like an entirely new proposal. Have we discussed that before?

  14. tingerrr commented on Jun 16, 2024

    @tingerrr
    ContributorAuthor

    Well a sys.target property can only be contextual if it's a style property, no? Isn't this what you meant with "Or a style property I guess"?

  15. laurmaedje commented on Jun 16, 2024

    @laurmaedje
    Member

    With "or a style property" I meant that it that it uses the style chain like fields, but not necessarily as sys.target but rather on some element. Adding style properties on anything but elements (/ types in the future) would be a very large change that doesn't seem sufficiently motivated to me by just this. It opens up many questions.

  16. laurmaedje commented on Jun 16, 2024

    @laurmaedje
    Member

    I think a contextual function would be simpler.

  17. PgBiel commented on Jun 17, 2024

    @PgBiel
    Contributor

    I'd like to add that my main concern regarding this feature is ensuring it isn't "abused" to the point of causing a significant distinction between PDF and PNG exports, mostly because Typst's instant preview relies heavily on PNG export (or on SVG export for typst-preview), and ensuring PNG export looks as close as possible to the final PDF is very important.

    I'm not sure how we could best mitigate this. Documentation is one way, but not very effective.

    Regardless, this feature will certainly be necessary (most importantly for HTML export), so I'm not proposing we drop it. I just wanted to ensure this is considered for the final design.

  18. laurmaedje commented on Jun 17, 2024

    @laurmaedje
    Member

    I totally agree. For HTML export alone, it would be sufficient to provide the distinction between HTML and layouted targets (or perhaps modes or some other name for the concept since we can embed layouted in HTML). What's not yet 100% clear to me is what concretely we need for PDF specific things: Do we just want extra stuff to be ignored (doesn't necessarily needed target-specific code) or do we really need flow control?

  19. tingerrr commented on Jun 17, 2024

    @tingerrr
    ContributorAuthor

    I think a contextual function would be simpler.

    Fine with me, I'd go with that then.

    I'd like to add that my main concern regarding this feature is ensuring it isn't "abused" to the point of causing a significant distinction between PDF and PNG exports, mostly because Typst's instant preview relies heavily on PNG export (or on SVG export for typst-preview), and ensuring PNG export looks as close as possible to the final PDF is very important.

    Especially regarding the last part of this, take a look at #4368 and see what you think, this is my main concern with styling of PDF forms which is implemented purely by the PDF reader.

    I totally agree. For HTML export alone, it would be sufficient to provide the distinction between HTML and layouted targets (or perhaps modes or some other name for the concept since we can embed layouted in HTML)

    I would say that PDF is sufficiently different from other "layouted" targets, not in layout, but in functionality. Perhaps all future features can be neatly implemented in a way that they always appear the same on all non-HTML targets, but I think that's maybe a little too optimistic.

    In the end, I don't think this is something we should or would ever see in user code, but strictly in package code (I don't think this should be enforced, but it could technically). Once Typst starts supporting features that heavily rely on more fine grained control on the export target we'd take away the possibility of creating novel solutions out of fear of misuse.

    People will always misuse features, that shouldn't mean we take away othere's ability to use it for what it was made for.

    We could block the design here for now and see how we go on with PDF forms to see if distinction with PDF is required now. If we go with a more restrictive design (only exposing some targets), then we should go with a is-<target> approach to avoid excessive API breakage down the line when this changes.

  20. T0mstone commented on Oct 25, 2025

    @T0mstone
    Contributor

    I think a way to set the default target from within the document would also be very nice and fit with this proposal.

  21. lordkekz commented on Jan 7, 2026

    @lordkekz

    For HTML export alone, it would be sufficient to provide the distinction between HTML and layouted targets (or perhaps modes or some other name for the concept since we can embed layouted in HTML).

    I think it may be nice to also be able to differentiate between target() == "paged" being due to PDF/PNG/SVG and target() == "paged" inside a html.frame. This could be achieved using a global-target() function that always returns the outermost target (i.e. the kind of file that Typst is going to produce) and could be non-contextual.

    In my case, I am building a pure-Typst package for exporting either as PDF, or as multiple HTML documents. Of course, this means that when I want to link across documents, I need to use different link targets in HTML and PDF export, including inside html.frame. Currently the outermost target could be provided manually using sys.inputs or by storing it in a state. I read that there are plans for multi-file HTML export in Typst itself, which might also solve this particular challenge, but I do not know of any details.

  22. added
    designed
    feature request
    and removed
    change request
    on Jan 22, 2026
  23. laurmaedje commented on Aug 31, 2026

    @laurmaedje
    Member
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

    designedfeature requestscripting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions