Create PDF Forms in Node.js
PDF Generator for SurveyJS allows you to generate interactive PDF forms on a Node.js server. This tutorial describes how to configure PDF form creation in a Node.js application.
Install the survey-pdf npm package
PDF Generator for SurveyJS is built upon the jsPDF library and is distributed as a survey-pdf npm package. Run the following command to install the package and its dependencies, including jsPDF:
npm install survey-pdf --save
If your survey contains HTML or Signature Pad questions, install the jsdom package to create a simulated web environment in a Node.js application. Create a JSDOM instance and reference the window and document objects from the JSDOM instance in a global scope:
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const SurveyPDF = require("survey-pdf");
const { window } = new JSDOM(`...`);
global.window = window;
global.document = window.document;
Export the PDF Form
To export a PDF form, you need to create a SurveyPDF instance. Its constructor accepts two parameters: a survey JSON schema and optional PDF document settings.
To save a PDF document with the exported survey, call the save(fileName) method on the SurveyPDF instance. If you omit the fileName parameter, the document uses the default name ("survey_result.pdf").
const surveyJson = { /* ... */ };
const pdfDocOptions: IDocOptions = { /* ... */ };
const surveyPdf = new SurveyPDF.SurveyPDF(surveyJson, pdfDocOptions);
surveyPdf.save("My PDF Form.pdf");
Populate the PDF Form with Data
Specify the data property of a SurveyPDF instance to define question answers. If a survey contains default values, and you wish to preserve them, call the mergeData(newObj) method instead.
surveyPdf.data = {
// ...
// An object with question answers
// ...
};
// ----- or -----
surveyPdf.mergeData({
// ...
// An object with question answers
// ...
});
For more information on how to programmatically define question answers, refer to the following help topic: Populate Form Fields.
Customize the PDF Form
If the default appearance of the exported form does not meet your requirements, use the following customization APIs to tailor the generated PDF document:
PDF Form Settings
Configure page orientation, fonts, compression, read-only mode, and other document-level settings.PDF Appearance Customization
Customize themes, layouts, and styles.Question Rendering
Customize the rendering behavior of specific question types.
In this tutorial, the exported PDF form uses the print-optimized Monochrome Light theme:
import { MonochromeLight } from "survey-core/themes";
const surveyPdf = new SurveyPDF({ /* ... */ });
surveyPdf.applyTheme(MonochromeLight);
Activate a SurveyJS License
SurveyJS PDF Generator is not available for free commercial use. To integrate it into your application, you must purchase a commercial license for the software developer(s) who will be working with the PDF Generator APIs and implementing the integration. If you use SurveyJS PDF Generator without a license, an alert banner will appear at the top of each page in an exported PDF document:
After purchasing a license, follow the steps below to activate it and remove the alert banner:
- Log in to the SurveyJS website using your email address and password. If you've forgotten your password, request a reset and check your inbox for the reset link.
- Open the following page: How to Remove the Alert Banner. You can also access it by clicking Set up your license key in the alert banner itself.
- Follow the instructions on that page.
Once you've completed the setup correctly, the alert banner will no longer appear.
Send feedback to the SurveyJS team
Need help? Visit our support page