Skip to content

RFC: PDF Form Elements #4368

Open
Open

Description

@tingerrr

Description

Proposal

Add PDF form elements, i.e. elements which when exported create form fields a user can interact with.

The following form elements are currently supported by pdf-writer 0.10.0:

  • text
    • fixed input size (comb)
    • redacted (password)
    • file selection
  • choice
    • list box (scrollable, single line)
    • combo box (dropdown)
    • custom input
    • multi select
  • buttons
    • radio buttons
    • push buttons
    • check boxes

API

First, I want to propose the API for the elements themselves, ignoring name conflicts for now:

Text elements (PDF 1.7, Section 12.7.4.3)

#let text(
  // whether this field is multiline or not 
  multiline: bool = false,
  // whether this field is a password (redacted) field or not
  passsword: bool = false,
  // whether the field should scroll (horizontally) to accomodate large inputs
  scroll: bool = true,
  // whether the input should be separated into the given number of "characters" (like a bank number input field)
  comb: int | none = none,
  // default body
  default: str | none = none,
) = ...

Choice elements (PDF 1.7, Section 12.7.4.4)

#let choice(
  // whether this field is a drop down or a list box
  dropdown: bool = true,
  // whether this field accepts custom choices
  edit: false,
  // whether more than one choice can be selected
  multiselect: bool = false,
  // the choices which are selected by default
  default: array<str> | none = none,
  // the choices the user is presented with
  ..choices: array<str>,
) = ...

Push button elements (PDF 1.7, Section 12.7.4.2.2)

I am unsure whether these should be added in the first iteration of this feature, as they don't retain any value and require the viewer to support built-in actions (which they rarely do) or JS actions (which is yet rarer than support for the built-in options).

Check box elements (PDF 1.7, Section 12.7.4.2.3)

#let checkbox(
  // the checkbox appearance when it is checked
  on: content | auto = auto,
  // the checkbox appearance when it is not checked
  off: content | auto = auto,
  // whether the checkbox is on or off by default
  default: bool = false,
  // positional + optional, the text to display with the checkbox
  value: str | none,
) = ...

Checkboxes display their text to the right of their box, I don't think this is configurable, but I could be wrong. Rendering it ourselves means we can support RTL layout, but lose interactivity for the displayed text, I believe.

The checkbox should create a block element which takes the size of the larger appearance.

Radio button elements (PDF 1.7, Section 12.7.4.2.4)

#let radio(
  // the radio button appearance when it is checked
  on: content | auto = auto,
  // the radio button appearance when it is not checked
  off: content | auto = auto,
  // whether the radio button is on or off by default
  default: bool = false,
  // positional, the key to associate the radio button to
  key: str,
  // positional + optional, the text to display with the radio button
  value: str | none,
) = ...

Radio buttons are tricky, my current design simply provides a key and should error when there is more than one set to enabled by default. Having more than one button enabled is valid for some cases, but these rely on a feature that most PDF viewers get wrong (RadiosInUnison).

I'm not sure if we have the infrastructure in place to throw an error when multiple elements use the same key and default: true, if not we may need export-errors first.

Name conflicts

There are some name conflicts to address, combined with the fact that we will either need a more restrictive interface to accommodate HTML forms in the future, or have these be PDF specific (which I think makes more sense, see additional notes be low), the PDF form fields should be placed in a module for PDF specific features and elements. See #4174 for a possible API related to this.

Assuming we chose implementing "low level" access for PDF forms here first and implementing a high level interface encompassing both PDF and HTML in the future, moving them into a pdf or pdf.form scope seems sensible to me. Users could still access them in a simpler fashion by importing from the scope if need be. We'd then have:

  • pdf.form.text
  • pdf.form.choice
  • pdf.form.checkbox
  • pdf.form.radio

Implementation Details

Because of how forms work in PDF and how we want to display forms ideally, we should strive to not use appearance streams in PDF at all and instead render form annotation fields on top of Typst native elements such as rect and block for styling. This has the benefit that they can be exported to all non-interactive export targets with the consistent styling and minimal effort. This also means that we lose functionality (e.g. switching appearances depending on hover) if we don't reimplement PDF viewer behavior for other export targets.

At least this is my initial thought, some appearance streams are simply FormXObjects, which are basically whatever content you throw at them, so maybe we can still support the hover and input appearance changes anyway.

Additional Notes

Some of the features have been deliberately left out, as they are deemed too arcane for now. This issue should serve as a place to discuss whether they should be added.

Whether two named parameters can be used together and how will follow the spec (Section 12.7.4), if there are conflicting values for the given fields an error with a hint will be thrown.

Because Typst is working towards HTML support, eventually HTML forms will have to be supported too, these will necessarily have a different API and feature set, so this RFC does not attempt to create a unified API. #4174 may be used as a basis for a more restrictive common API between different export targets supporting similar features.

Use Case

Would be cool.

Activity

  1. added
    feature request
    on Jun 10, 2024
  2. tingerrr commented on Jun 10, 2024

    @tingerrr
    ContributorAuthor

    At this moment, these fields would be 100% sized, so putting a container around them would be required for both styling (consistent across export targets) and sizing by putting explicit constraints on those.

    I'm unsure whether this is a good idea, but otherwise we may need to duplicate loads of options from block and such on those elements.

  3. Enter-tainer commented on Jun 11, 2024

    @Enter-tainer
    Contributor

    What would they look like in png and svg?

  4. Enivex commented on Jun 11, 2024

    @Enivex
    Collaborator

    What would they look like in png and svg?

    Presumably the same, but without the actual ability to interact with the form.

  5. tingerrr commented on Jun 11, 2024

    @tingerrr
    ContributorAuthor

    Yeah, basically that, the assumption is that, if we don't use PDF specific styling, but instead use Typst content where ever possible they'd look the same everywhere and the PDF export only really differs in adding the interactive part of the form.

  6. reknih commented on Jun 11, 2024

    @reknih
    Member

    I have a few questions and remarks:

    • What is a form element on the inside? Is there a way to do #show checkbox: set rect(...) to quickly customize or do I always have to implement the show rule?
    • Wouldn't a simple width and height on every control be nicer than having to wrap it in a block?
    • How does the show rule expose interactive states (hover, active, disabled) of the control? With sub-elements like #show checkbox.active: (it) => {} or does it work like headers and footers in a page set rule?
    • Do we plan to eventually also offer sendable forms? If so, I think that the form elements all need to receive a name property, likewise the choices array could be required to contain dictionaries.
    • I think that a string is unidiomatic to group radio buttons. Two alternatives that come to mind would be using a label or forcing them to appear in the body of a radio.group or radiogroup function
    • Do I understand you correctly that we should write the appearance streams ourselves to guarantee consistent results instead of having the PDF reader do it? The sentence "we should strive to not use appearance streams in PDF at all" threw me off.
    • @PgBiel is currently working on diagnostics, that will enable export errors and warnings.
  7. wrzian commented on Jun 11, 2024

    @wrzian
    Contributor

    I know naming is subjective, but having text as the name feels confusing even if it is scoped due to how fundamental the current text function is to Typst. I'd argue for something like textbox instead.

  8. tingerrr commented on Jun 11, 2024

    @tingerrr
    ContributorAuthor
    • What is a form element on the inside? Is there a way to do #show checkbox: set rect(...) to quickly customize or do I always have to implement the show rule?

    It would be a simple block level element, not really reusing anything other than block at most.

    • Wouldn't a simple width and height on every control be nicer than having to wrap it in a block?

    Well maybe, but then we'd likely also need to add other possibly redundant appearance options.
    Perhaps we always use a block element with an auto default size and allow configuration through show-set. See below regarding appearance.

    • How does the show rule expose interactive states (hover, active, disabled) of the control? With sub-elements like #show checkbox.active: (it) => {} or does it work like headers and footers in a page set rule?

    At this moment not at all, this dies into how I want to avoid using appearance streams.
    Let's entertain the thought of exposing this to users, we'd expose either fields or sub elements to distinguish how an element is displayed, it's not important how this works, the problems I am concerned about exist in both cases.

    #show textbox.hover => block(width: 100%, it)
    #show textbox.normal => block(width: 50%, it)
    
    #textbox()

    If we want an API that allows a user to reuse primitive elements like above, we quickly run into the problem of how to translate this into appearance streams correctly and catching all the invalid edge cases like differing sizes of a text box block.

    A hover action would only switch things like stroke type, color and the like.

    If we instead faithfully expose appearance streams, we need to ensure that we correctly render something in non-PDF targets that will look like forms, which to me sounds like implementing parts of a PDF reader. This could quickly get out of hand, and I'm unsure how much of the whole PDF annotation transformation matrix shtick is required to correctly size and transform such appearances.

    We definitely want to faithfully display forms on PNG and SVG, simply because they are used as preview export targets. My key takeaway is that I want to reuse rendering of primitive elements where possible. Using appearance streams like those (or in general) makes this more brittle in its implementation and more restrictive in its user-facing API.

    • Do we plan to eventually also offer sendable forms? If so, I think that the form elements all need to receive a name property, likewise the choices array could be required to contain dictionaries.

    I left this out as sendable forms are a feature which basically only Acrobat implements and, at least from my subjective experience, no one really uses. This is also one of the reasons I'm unsure about push buttons, their only use cases are some a few arcane custom actions and JS actions.

    The keys in forms are also hierarchical, which is not easily done in Typst without having the user spell out the full path for the keys. We could add this in post and reserve a key field for them. It should be noted that hierarchical keys are also rarely used, so maybe this is not a big issue.

    • I think that a string is unidiomatic to group radio buttons. Two alternatives that come to mind would be using a label or forcing them to appear in the body of a radio.group or radiogroup function

    I considered this in earlier designs, but I'm not aware of any mechanism at this moment that supports allows forcing one element to only be created inside another. Consider #figure.caption, which can be created outside a figure, but arguably shouldn't. I'm unsure if this is due to this limitation, if it exists, or if it's simply an oversight.

    It should also be noted that such a group would not allow one to easily place connected radio buttons in completely different places, which PDF allows. This feature is rather arcane, though, and I've not seen it used much in forms I collected. I'm fine with not allowing this and going for a super/sub element design. This also avoids having a spurious key field which could later conflict with sendable forms.

    • Do I understand you correctly that we should write the appearance streams ourselves to guarantee consistent results instead of having the PDF reader do it? The sentence "we should strive to not use appearance streams in PDF at all" threw me off.

    See above regarding hover/normal etc.

    I know naming is subjective, but having text as the name feels confusing even if it is scoped due to how fundamental the current text function is to Typst. I'd argue for something like textbox instead.

    Sure, this makes sense to me.

  9. Enivex commented on Jun 11, 2024

    @Enivex
    Collaborator

    I know naming is subjective, but having text as the name feels confusing even if it is scoped due to how fundamental the current text function is to Typst. I'd argue for something like textbox instead.

    How about freeform?

  10. added
    pdf
    on Jun 21, 2024
  11. stanlrt commented on Nov 2, 2025

    @stanlrt
  12. whardier commented on Nov 2, 2025

    @whardier

    I am curious how font handling would happen.. as several fonts may be required for full embedding.

  13. Enivex commented on Nov 2, 2025

    @Enivex
  14. euberdeveloper commented on Feb 10, 2026

    @euberdeveloper
  15. dayala1 commented on Mar 5, 2026

    @dayala1
    Contributor

    This would be fundamental for adoption of Typst in many institutions that need forms for people to easily fill for many bureocratic processes (e.g. universities). Until it is added, people will just keep using Word.

  16. PavanSamyak commented on Jun 4, 2026

    @PavanSamyak

    I'm new to typst can anyone help me to get ineractive checkbox on to my pdfs please

  17. cady-b commented on Jun 4, 2026

    @cady-b
    Contributor

    @PavanSamyak Please direct support requests to the Forum or Discord; the issue tracker is not quite the place for such discussions.

    TLDR; Forms are not yet implemented, so there is no way you can currently construct interactive checkboxes within your documents. There is an effort behind changing that in the future (but no ETA, no promises): https://bsky.app/profile/typst.app/post/3mmrma5rjss2u

    You can otherwise use third-party solutions to create interactive overlays over any plain PDF. I assume Adobe & Co. have offerings here, but can't make general recommendations; https://github.com/carpe-diem/typst-fillable has been shared with the community, but I haven't looked over the code so run at your own risk.

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