How to embed fillable ONLYOFFICE forms into a web page
You can easily add an online form to your website, making it available for filling in and downloading as PDF. Read on this post and learn how to do it.
A brief intro
We guess you are already familiar with our forms. If not, here is a quick reminder. Starting from version 7.0 of ONLYOFFICE Docs, you can create, edit and collaborate on online forms, let other users fill them out.
Starting from version 8.0, we switched to PDF as the industry standard while taking all the advantages from our native format. So, in ONLYOFFICE forms, you work with two main formats. DOCXF is intended for creating form templates from blank or any existing DOCX file. Ready forms are saved and distributed in PDF.
How to open DOCXF for editing from website
Find and open the index.html
file of your ONLYOFFICE Document Server. Then, connect it to the Document Server API. You need to specify configuration parameters for opening a form template.
<script type="text/javascript"
src="http://0.0.0.0:8080/web-apps/apps/api/documents/api.js"></script>
// Specify the path to the API JavaScript file.
<button onclick="open_form_template()">Open Form Template</button>
// Add a button to open the form.
<div id="placeholder"></div>
// Add the element where the editor will be opened.
<script>
function open_form_template() {
// Close the editor in case it is open.
if (this.docEditor) {
this.docEditor.destroyEditor()
}
// Create the full URL address to the form template you need to open.
const url = window.location.protocol + "//" +
window.location.hostname + “:” + window.location.port + ”/” + filename + ”.docxf”;
// Add the key to identify the file for co-editing.
const key = filename + ”.docxf”;
// Create DocsAPI object with the document config and open the editor in the placeholder element.
this.docEditor = new DocsAPI.DocEditor("placeholder",
{
“document”: {
“fileType”: “docxf”,
“key”: key,
“title”: “Form Template”,
“url”: url
},
“documentType”: “word”
});
}
</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 pdf” button.
How to open PDF for filling from website
You need to add a button which opens the form for filling. Then, add the open_form_template()
function.
<script type="text/javascript"
src="http://0.0.0.0:8080/web-apps/apps/api/documents/api.js"></script>
// Specify the path to the API JavaScript file.
<button onclick="open_form()">Open Form</button>
// Add a button to open the form.
<div id="placeholder"></div>
// Add the element where the editor will be opened.
<script>
function open_form_template() {
// Close the editor in case it is open.
if (this.docEditor) {
this.docEditor.destroyEditor()
}
// Create the full URL address to the form you need to open.
const url = window.location.protocol + "//" +
window.location.hostname + ”:” + window.location.port + ”/” + filename + ”.pdf”;
const key = filename + ”.pdf”;
// Create DocsAPI object with the document config and open the editor in the placeholder element.
this.docEditor = new DocsAPI.DocEditor("placeholder",
{
“document”: {
“fileType”: “pdf”,
“title”: “Form”,
“url”: url
},
“documentType”: “pdf”
});
}
</script>
Take into consideration 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 PDF file is disabled. That’s why anyone can open the form and fill it out without disturbing others.
Once done, a form can be opened for filling. After filling in the fields (the required ones are highlighted with the red border), it’s possible to print or download the PDF form.