Integrate PDF Generator in Survey Creator

Edit in →

SurveyJS PDF Generator is a JavaScript UI component that enables you to render SurveyJS surveys and forms as PDF files directly in a browser. You can print these PDF forms for offline use, such as at conferences and workshops, or share editable forms with respondents to complete online. This example demonstrates how you can integrate PDF Generator into Survey Creator to generate and preview PDF forms as you configure them.

Install PDF Generator

PDF Generator is built upon the jsPDF library. Select and implement one of the following options to set up PDF Generator:

  • Option 1: Insert the jsPDF and PDF Generator scripts within the <head> tag on your HTML page.

    <head>
        <script src="https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js"></script>
        <script src="https://unpkg.com/survey-pdf/survey.pdf.min.js"></script>
        <!-- ... -->
    </head>
    
  • Option 2: Install the survey-pdf npm package.
    The jsPDF npm package will be installed automatically as a dependency.

    npm install survey-pdf --save
    

Add PDF Generator Settings to the Property Grid

PDF Generator settings allow you to customize page orientation, margins, font, and other document parameters (see IDocOptions). Survey Creator users can change these settings in the Property Grid. To make them available, add corresponding custom properties to the "survey" class:

Serializer.addProperty("survey", {
    name: "pdfOrientation",
    displayName: "Page orientation",
    type: "buttongroup",
    category: pdfCategoryName,
    categoryIndex: 800,
    default: "auto",
    choices: [ "auto", "p|Portrait", "l|Landscape" ],
    isSerializable: false
});

This demo also adds a custom pdfIsPageBreak property to the "question" class. When enabled for a question, this property inserts a page break before that question:

Serializer.addProperty("question", {
    name: "pdfIsPageBreak:boolean",
    displayName: "Insert a page break before this element",
    category: pdfCategoryName,
    categoryIndex: 800,
    default: false,
    isSerializable: false
});

Generate a PDF Form

To save a survey as a PDF document, follow the steps below:

  1. Prepare the PDF document.
    Use the PDF settings from the survey being configured to build an IDocOptions object and apply other customizations to the document.

  2. Instantiate SurveyPDF.
    Pass a survey JSON schema and the IDocOptions object to the SurveyPDF constructor.

  3. Call the save(fileName) method.
    This method saves a PDF document under a specified name on the user's device.

  4. Add a custom toolbar button.
    Combine steps 1 to 3 into a single function and call it by a click on a custom toolbar button.

Refer to the Code tab for a full code example.

Your cookie settings

We use cookies on our site to make your browsing experience more convenient and personal. In some cases, they are essential to making the site work properly. By clicking "Accept All", you consent to the use of all cookies in accordance with our Terms of Use & Privacy Statement. However, you may visit "Cookie settings" to provide a controlled consent.

Your renewal subscription expires soon.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.

Your renewal subscription has expired.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.