Skip to content

Add interactive forms - #8912

Draft
diogotcorreia wants to merge 7 commits into
typst:mainfrom
diogotcorreia:html-forms
Draft

diogotcorreia wants to merge 7 commits into
typst:mainfrom
diogotcorreia:html-forms

Conversation

@diogotcorreia

@diogotcorreia diogotcorreia commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Related to #1765 #2421 #4368

This PR only contains the library changes and the HTML export. The paged export will come in a separate PR. PR includes both HTML and paged export.

@diogotcorreia diogotcorreia added feature request New feature or request html Related to HTML export model Related to model category, which is all about structure and semantics. and removed feature request New feature or request labels Oct 1, 2026
@diogotcorreia diogotcorreia changed the title Add forms and its HTML export Add interactive forms Oct 9, 2026
@diogotcorreia diogotcorreia added layout Related to the layout category, which is about composing, positioning, etc. and removed layout Related to the layout category, which is about composing, positioning, etc. labels Oct 9, 2026
@diogotcorreia
diogotcorreia force-pushed the html-forms branch 3 times, most recently from d5346b7 to d99088b Compare October 15, 2026 15:08
@diogotcorreia
diogotcorreia force-pushed the html-forms branch 3 times, most recently from fb4293b to 85613d1 Compare October 26, 2026 10:17

@laurmaedje laurmaedje left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The code looks very solid overall, nice work!

The architecture was discussed ahead and seems to work well, so this review is mostly minor things, style nitpicks, and stuff like that.

I should note that I haven't looked much at the default show rules yet because they are WIP afaict. I'm happy to discuss how we can deal with the width / height issues the current ones have. I also haven't looked much at the PDF export code either, as I would defer that to @saecki. Finally, I should note that I mostly experimented with PDF, not with HTML because the latter is more straightforward.

Compatibility notes

  • Typing an "ä" does not work in Acrobat in a textbox, but in non-Typst forms I tested, it worked
  • A choice field with multiple: true does not display out of the box in Acrobat
  • A textbox does not display its default value in Acrobat

These were just a few things I noticed, I did not check comprehensively. I think we need a document that exercises all features and test it in all viewers on all platforms.

Comments that aren't about a particular part of the code

  • It would be nice if set text(fill: ..) would configure the variable text appearance color (via krilla's VariableAppearance)

Comment thread crates/typst-library/src/model/form.rs Resolved
Comment thread crates/typst-library/src/model/form.rs Resolved
Comment thread crates/typst-library/src/model/form.rs Resolved
Comment thread crates/typst-library/src/model/form.rs Resolved
Comment thread crates/typst-library/src/model/form.rs Resolved
Comment thread crates/typst-library/src/model/form.rs Resolved
Comment thread crates/typst-pdf/src/tags/tree/mod.rs Resolved
Comment thread crates/typst-pdf/src/form.rs Resolved
Comment thread crates/typst-pdf/src/form.rs Resolved
Comment thread crates/typst-library/src/model/form.rs Resolved
@diogotcorreia
diogotcorreia force-pushed the html-forms branch 2 times, most recently from cdbc60b to 1caa177 Compare October 30, 2026 16:47

@diogotcorreia diogotcorreia left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think I addressed all or most of the comments. I haven't tried to fix compatibility with Acrobat yet, but will investigate that next.

Comment thread crates/typst-html/src/rules.rs Outdated

const FORM_BUTTON_RULE: ShowFn<FormButton> = |elem, engine, styles| {
let span = elem.span();
if styles.get_ref(FormElem::form).is_none() {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I named it FormElem::try_from_styles, but I'm not sure if it should be part of FormElem or just a standalone function. It's kinda weird that it's on FormElem but then returns a &Packed<FormElem>

Comment thread crates/typst-html/src/rules.rs Resolved
Comment thread crates/typst-html/src/rules.rs Resolved
Comment thread crates/typst-layout/src/form.rs Resolved
Comment thread crates/typst-layout/src/rules.rs Outdated
Comment on lines +644 to +647
if fixed_width && max_length.is_none() {
bail!(span, "`max-length` must be set when `fixed-width: true`");
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

HTML does not support fixed-width so this doesn't exist there?

Comment thread crates/typst-layout/src/introspect.rs Outdated
}
}
FrameItem::Text(..) | FrameItem::Shape(..) | FrameItem::Image(..) => {}
FrameItem::Text(..)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I gave my shot at this with the measure locator, but it doesn't seem to be working 100% yet. I think it's best we define a few test cases and the expected outcome.

Comment thread crates/typst-layout/src/rules.rs Resolved
/// An internal or external link to a destination.
Link(Destination, Size),
/// A representation of an interactive form field.
FormField(FieldWidget),

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I added Arc to FieldWidget::appearance, the struct no longer increases in size.


#[derive(Debug, Clone, Hash)]
pub struct FieldWidget {
pub span: Span,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Hopefully everything is documented now (except for the user-facing docs)

Comment thread crates/typst-library/src/model/form.rs Resolved
@diogotcorreia
diogotcorreia force-pushed the html-forms branch 4 times, most recently from 827e0ce to 2aa504e Compare November 6, 2026 13:28
@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Nov 6, 2026

@saecki saecki left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here's the review of the pdf export.
It's one question about the alt_name of form fields, on performance concern, otherwise mostly stylistic nits :)

Comment thread crates/typst-pdf/src/tags/groups.rs Resolved
Comment thread crates/typst-pdf/src/util.rs Resolved
Comment thread crates/typst-pdf/src/tags/tree/mod.rs Resolved
Comment thread crates/typst-pdf/src/tags/mod.rs Resolved
Comment thread crates/typst-pdf/src/convert.rs Resolved
Comment thread crates/typst-pdf/src/form.rs Resolved
Comment thread crates/typst-pdf/src/form.rs Resolved
Comment thread crates/typst-pdf/src/form.rs Resolved
Comment thread crates/typst-pdf/src/form.rs Resolved
Comment thread crates/typst-pdf/src/form.rs Resolved

@laurmaedje laurmaedje left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here's some more comments. :)

Comment thread crates/typst-layout/src/introspect.rs Resolved
Comment thread crates/typst-layout/src/form.rs Resolved
Comment thread crates/typst-library/src/model/form.rs Resolved
Comment thread crates/typst-library/src/model/form.rs Resolved
Comment thread crates/typst-library/src/model/form.rs Outdated
if disabled {
styles::set_disabled_styles(&mut out);
}
out.set(RectElem::inset, Sides::splat(Some(Abs::pt(5.0).into())));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The inset is set for the second time here, so the inner 0.5pt from set_rect_stroke are overridden.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It was intentional, but I can see that it can be confusing, especially because I'm setting it back to the default (5pt). It might make more sense to set the inner stroke without the inset, so I'll just inline the function, sadly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was a bit confused because I would think to get the inner stroke plus the standard padding, it should really be 5.5pt, shouldn't it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It's such a small difference anyway, but I'm setting the inset to 5pt + 0.05em now then.

Comment thread tests/suite/model/form.typ Resolved
Comment thread crates/typst-library/src/model/form.rs Resolved
Comment thread crates/typst-pdf/src/form.rs Resolved
Comment thread crates/typst-pdf/src/form.rs Outdated
if is_leaf {
let name = field.name.clone();
if fields.insert(path_segment.into(), Node::Leaf(field)).is_some() {
bail!(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed. And if we know about the field, we could also be smarter about the hint for dotted paths instead of saying generically that it might happen with {name}.something-else.

Comment thread crates/typst-pdf/src/convert.rs Resolved
@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Nov 17, 2026
@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Nov 17, 2026

@diogotcorreia diogotcorreia left a comment •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've resolved a bunch of comments and the commits I just pushed include a few other changes as well, most notably:

  • form.cursor is gone, as we discussed
  • form.signature has been added
  • WIP reference and guide docs have been added
  • replaced textbox's multiline and fixed-width with kind string enum
  • refactored crates/typst-layout/src/form.rs so a lot more code is reused between each field type
  • variable text field's quadding is now inferred from horizontal alignment

I've replied to all unresolved comments as well. Some I left unresolved because I wasn't sure my changes were the right ones, but for others I have not changed anything and I await further feedback/discussion.

Comment thread crates/typst-layout/src/form.rs Resolved
let widget = FieldWidget {
span,
field_location: elem.location().unwrap(),
size,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

How should we handle checkbox/radio? Expand + off state normal's size?

let size = region.expand.select(region.size, body_off.normal.size());

I'm going with this, let me know if you want to revert to region on these two

Comment thread crates/typst-layout/src/form.rs Resolved
Comment thread crates/typst-layout/src/introspect.rs Resolved
Comment thread crates/typst-layout/src/rules.rs Resolved
Comment thread crates/typst-pdf/src/form.rs Outdated
let styles = StyleChain::default();
let span = field.span();

let get_alt_text = || {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done. Also added .filter(|s| !s.trim().is_empty()) so that we don't set whitespace-only alt names.

#form.textbox()
]

--- form-hover-down-styles-pdf-a pdf ---

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Is turning the form-counter test into PDF A/4 enough? Seems to pass

Comment thread tests/suite/model/form.typ Resolved
Comment thread tests/suite/model/form.typ Resolved
Comment thread crates/typst-layout/src/form.rs Resolved
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Nov 24, 2026
#import "../../components/index.typ": docs-chapter, docs-table, short-or-long

#show: docs-chapter.with(
title: "Form Guide",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
title: "Form Guide",
title: "Forms Guide",


= #short-or-long[Form Fields][Which form fields does Typst support?] <form-fields>

Typst has support for five kinds of form fields where users can input information,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Typst has support for five kinds of form fields where users can input information,
Typst has support for five kinds of form fields where users can enter information,

Comment on lines +44 to +45
Each field kind is better suited for a different data type that you might want to collect
and the medium you want to distribute your form in.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Each field kind is better suited for a different data type that you might want to collect
and the medium you want to distribute your form in.
The suitability of each field kind depends on which type of data you want to collect and in which medium you want to distribute your form.

]
```

- *@form.radio-group[Radio]:* Select at most one option among multiple options.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
- *@form.radio-group[Radio]:* Select at most one option among multiple options.
- *@form.radio-group[Radio]:* Select at most one option among multiple.

When only a single choice is allowed, this field kind is displayed as
a dropdown. For multiple choices, it is displayed as a listbox instead.
Choice fields work great digitally, although with limited styling options in PDF.
However, they are not usable in print.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
However, they are not usable in print.
When printed, they do not work.

(especially dropdowns, I could see someone circling a listbox option)

use crate::text::TextElem;
use crate::visualize::{CircleElem, Paint, RectElem};

/// A form with interactive and fillable fields.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps this should describe that multiple forms are allowed, that forms must not nest (perhaps on the body argument), and that when submitting, all field values in the form submit?

Comment on lines +180 to +182
/// We highly recommend explicitly setting each form field's name when you
/// expect the form to be submitted. Otherwise, fields won't be submitted at
/// all in HTML and will have auto-generated names in PDF.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@laurmaedje should we document that auto-generated name changes will not be considered a breaking change or are non-breaking breaking changes a fanciful mirage anyways?

/// A textbox can be used when you want the user to input arbitrary text
/// into a field, and it comes in three variants:
///
/// - *single-line*, where the value cannot contain line breaks;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Capitalize at start of list, add following punctuation to boldface or italics

Comment on lines +1309 to +1311
/// When used within a PDF file, this field allows the user to cryptographically
/// sign the present document and optionally place a visual signature where
/// this field is.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

From a compat standpoint, more like the reverse

)
}

/// Fragment element to allow styling of form fields in a specific state.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it a fragment or a pseudo element (cc @laurmaedje)

@MDLC01 MDLC01 left a comment

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.

Just a few details I noticed

Comment on lines +1081 to +1082
/// - When `false`, this field is displayed as a dropdown.
/// - When `true`, this field is displayed as a listbox.

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.

Suggested change
/// - When `false`, this field is displayed as a dropdown.
/// - When `true`, this field is displayed as a listbox.
/// - When `{false}`, this field is displayed as a dropdown.
/// - When `{true}`, this field is displayed as a listbox.

/// set your own click listener that performs another action instead.
pub action: Option<ButtonAction>,

/// Whether this button is disabled. When true, interacting with it does not

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.

Suggested change
/// Whether this button is disabled. When true, interacting with it does not
/// Whether this button is disabled. When `{true}`, interacting with it does not

Same in other places below.

pub struct FormChoice {
/// The name of this field.
///
/// If `{auto}`, will be automatically generated as `field-<n>`

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.

Suggested change
/// If `{auto}`, will be automatically generated as `field-<n>`
/// If `{auto}`, will be automatically generated as `{"field-<n>"}`

Also in one other place below.

/// this field is.
///
/// This form field is not supported in HTML and some PDF readers. See the
/// @form:compatibility[compatibility section] for more details.

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.

Suggested change
/// @form:compatibility[compatibility section] for more details.
/// @form:compatibility[Compatibility section] for more details.

the document, attesting its validity.
Users of a supported PDF reader can sign the document using their digital signature.
Only certain PDF readers are supported – check the
@form:compatibility[compatibility table] for full details.

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 may be mistaken, but I believe the question is whether the signature functionality is supported by the PDF reader, whereas this is phrased in a way that makes it seems like something (Typst?) needs to support each individual PDF reader.

I am unsure how to phrase it better without making the text too cumbersome. Maybe something like this?

Users of a PDF reader that supports the feature can sign the document using...
Not all PDF readers support digital signatures...

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

Labels

html Related to HTML export model Related to model category, which is all about structure and semantics. waiting-on-review This PR is waiting to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants