Skip to content

Web Forms JavaScript API

The Web Forms JavaScript library allows you to embed web forms on your website for collecting data from your users and filling out PDF templates.

Creates an embedded web form (or “simple form”) for a DocSpring template.

DocSpring.createSimpleForm(css_selector, template_id, options_and_callbacks);
ParameterTypeRequiredDescription
css_selectorstringYesCSS selector for the container element where the form will be rendered
template_idstringYesThe ID of your DocSpring template (e.g., 'tpl_123456')
options_and_callbacksobjectNoConfiguration options and event callbacks
OptionTypeDefaultDescription
defaultDataobject{}An object containing default values for form fields. Keys should match field names in your template.
metadataobject{}An object containing metadata for the submission. This data is included in API responses and webhooks but not in the PDF.
showClearButtonbooleantrueSet to false to hide the “Clear” button
clearButtonLabelstring"Clear"Text for the clear button
submitButtonLabelstring"Submit Form"Text for the submit button
submitButtonSavingLabelstring"Saving..."Text shown on submit button while saving
submitButtonProcessingLabelstring"Generating PDF..."Text shown on submit button while waiting for PDF processing
redirectURLstringnullURL to redirect to after form submission. Overrides the template’s default redirect URL.
waitForPDFbooleantrueIf redirectURL is provided, set to false to redirect immediately instead of waiting for PDF processing. Has no effect if “Submission Privacy” is “Private”.
CallbackParametersDescription
processTemplateSchemajsonSchema (mutable)Called before the form is rendered. Modify the template schema to customize form behavior. You can hide fields by removing them from the schema.
onClearFormNoneCalled after the clear button is pressed
onSubmitformData (object)Called when the submit button is pressed. Parameter contains all form field values.
onSavesubmission (object)Called when the form has been saved. Parameter contains submission attributes including id.
onProcessedsubmission (object)Called when the PDF has been processed. Only called if “Submission Privacy” is “Public”. Parameter contains submission attributes including id, download_url, and expires_at.
onErrorresponse (object)Called if there is an error with the request. Parameter is the response from the AJAX request.

Returns a form instance object with methods for programmatic control (currently undocumented).

DocSpring.createSimpleForm(".dsp-form", "tpl_123456");
DocSpring.createSimpleForm(".dsp-form", "tpl_123456", {
defaultData: {
name: "John Smith",
date: "2024-01-15",
},
});
DocSpring.createSimpleForm(".dsp-form", "tpl_123456", {
// Options
defaultData: {
name: "John Smith",
},
metadata: {
userId: "12345",
source: "website",
},
showClearButton: true,
clearButtonLabel: "Reset",
submitButtonLabel: "Generate My PDF",
submitButtonSavingLabel: "Please wait...",
submitButtonProcessingLabel: "Creating your PDF...",
redirectURL: "https://example.com/success",
waitForPDF: false,
// Callbacks
processTemplateSchema: function (jsonSchema) {
// Hide the email field
delete jsonSchema.properties.email;
},
onClearForm: function () {
console.log("Form cleared");
},
onSubmit: function (formData) {
console.log("Form submitted:", formData);
},
onSave: function (submission) {
console.log("Submission saved:", submission.id);
},
onProcessed: function (submission) {
console.log("PDF ready:", submission.download_url);
},
onError: function (response) {
console.error("Error:", response);
},
});

The submission object passed to onSave and onProcessed callbacks contains:

PropertyTypeAvailable InDescription
idstringBothUnique identifier for the submission
statestringBothCurrent state: "pending", "processed", or "error"
created_atstringBothISO 8601 timestamp when submission was created
download_urlstringonProcessed onlyURL to download the generated PDF
expires_atstringonProcessed onlyISO 8601 timestamp when the download URL expires

The onError callback receives a response object with:

PropertyTypeDescription
statusnumberHTTP status code
statusTextstringHTTP status text
responseJSONobjectParsed JSON error response (if available)
responseTextstringRaw response text
onError: function(response) {
if (response.status === 422) {
// Validation error
const errors = response.responseJSON.errors;
console.error('Validation failed:', errors);
} else if (response.status === 401) {
// Authentication error
console.error('Template not configured for public access');
} else {
// Other error
console.error('Error:', response.statusText);
}
}

The embedded forms library supports all modern browsers:

  • Chrome 60+
  • Firefox 55+
  • Safari 11+
  • Edge 79+

Older versions of Internet Explorer are not supported.