Embedding forms into a web page

Starting from version 7.0, ONLYOFFICE Docs offers the possibility to create, edit and collaborate on online forms, fill them out, and save forms as PDF.

ONLYOFFICE forms are available in two main formats. DOCXF is intended for creating form templates from blank or any existing DOCX file. The OFORM format is used for filling out the ready forms.

These instructions help you add an online form to your website, making it available for filling in and downloading as PDF.

Please note that these instructions will only work when JWT is disabled. Starting from version 7.2, JWT is enabled by default, so you need to disable it. More information about token can be found here.

  • Editing forms
  • Filling forms

How to open DOCXF for editing from website

To open an online form in the DOCXF format for editing from your website, follow the steps below:

  1. Find and open the index.html file of your ONLYOFFICE Docs.
  2. Connect it to the Document Server API by specifying the path to the API JavaScript file:

    1. <script type="text/javascript" src="https://documentserver/web-apps/apps/api/documents/api.js"></script>
  3. Add the button element to open a form template:

    1. <button onclick="open_form_template()">Open Form Template</button>
  4. Add the div element where the editor will be opened:

    1. <div id="placeholder"></div>
  5. Add the script to close the editor in case it is open:

    1. if (this.docEditor) {
    2. this.docEditor.destroyEditor()
    3. }
  6. Create the full URL address to the form template you need to open:

    1. const url = "https://example.com/url-to-example-document.docxf";
  7. Create the key to identify the file for co-editing:

    1. const key = filename + ".docxf";
  8. Add the script initializing the Document Editor with the configuration for the document you want to open and open the editor in the placeholder element:

    1. this.docEditor = new DocsAPI.DocEditor("placeholder",
    2. {
    3. "document": {
    4. "fileType": "docxf",
    5. "key": key,
    6. "title": "Form Template",
    7. "url": url
    8. },
    9. "documentType": "word"
    10. });

The full code fragment looks like this:

  1. <script type="text/javascript" src="https://documentserver/web-apps/apps/api/documents/api.js"></script>
  2. <button onclick="open_form_template()">Open Form Template</button>
  3. <div id="placeholder"></div>
  4. <script>
  5. function open_form_template() {
  6. if (this.docEditor) {
  7. this.docEditor.destroyEditor()
  8. }
  9. const url = "https://example.com/url-to-example-document.docxf";
  10. const key = filename + ".docxf";
  11. this.docEditor = new DocsAPI.DocEditor("placeholder",
  12. {
  13. "document": {
  14. "fileType": "docxf",
  15. "key": key,
  16. "title": "Form Template",
  17. "url": url
  18. },
  19. "documentType": "word"
  20. });
  21. }
  22. </script>

Once done, a form template can be opened for editing. After editing this file, you can get the form itself. To do so, click the Save as oform button.

Embed docxf

How to open OFORM for filling from website

To make an online form in the OFORM format available for filling in and downloading as PDF from your website, follow the steps below:

  1. Find and open the index.html file of your ONLYOFFICE Docs.
  2. Connect it to the Document Server API by specifying the path to the API JavaScript file:

    1. <script type="text/javascript" src="https://documentserver/web-apps/apps/api/documents/api.js"></script>
  3. Add the button element to open the form:

    1. <button onclick="open_form()">Open Form</button>
  4. Add the div element where the editor will be opened:

    1. <div id="placeholder"></div>
  5. Add the script to close the editor in case it is open:

    1. if (this.docEditor) {
    2. this.docEditor.destroyEditor()
    3. }
  6. Create the full URL address to the form template you need to open:

    1. const url = "https://example.com/url-to-example-document.oform";
  7. Create the key to identify the file

    1. const key = filename + ".oform";

    Please note that the key field is not passed to the configuration of the editors. This field will be automatically generated as a random number. This allows making all sessions of opening the form independent. So, collaboration on the OFORM file is disabled. That’s why anyone can open the form and fill it out without disturbing others.

  8. Add the script initializing the Document Editor with the configuration for the document you want to open and open the editor in the placeholder element:

    1. this.docEditor = new DocsAPI.DocEditor("placeholder",
    2. {
    3. "document": {
    4. "fileType": "oform",
    5. "title": "Form",
    6. "url": url
    7. },
    8. "documentType": "word"
    9. });

The full code fragment looks like this:

  1. <script type="text/javascript" src="https://documentserver/web-apps/apps/api/documents/api.js"></script>
  2. <button onclick="open_form()">Open Form</button>
  3. <div id="placeholder"></div>
  4. <script>
  5. function open_form() {
  6. if (this.docEditor) {
  7. this.docEditor.destroyEditor()
  8. }
  9. const url = "https://example.com/url-to-example-document.oform";
  10. const key = filename + ".oform";
  11. this.docEditor = new DocsAPI.DocEditor("placeholder",
  12. {
  13. "document": {
  14. "fileType": "oform",
  15. "title": "Form",
  16. "url": url
  17. },
  18. "documentType": "word"
  19. });
  20. }
  21. </script>

Once done, a form can be opened for filling. After filling in the fields (the required ones are highlighted with the red border), you can get a PDF file. To do so, click the Save as PDF button.

Embed oform