forked from wyeworks/nucore-open
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add form.io tips and tricks (wyeworks#2146)
# Release Notes Documentation from Aaron on how to best use form.io # Additional context * Form.io documentation from Aaron * Turn RTF into .js files and link from readme Co-authored-by: Jason Hanggi <jason@tablexi.com>
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Generate In-Line Custom PDF within Current Form | ||
// Replace [token_id] | ||
var pdfFileName = data.orderedForUsernname && data.nucoreOrderNumber ? data.orderedForUsernname + '-' + data.nucoreOrderNumber : 'document'; | ||
var fileToken = '[token_id]'; // PDF token | ||
|
||
instance.label = 'Print Tag'; // change label | ||
instance.redraw(); | ||
// Create instance using same pdf as nested form | ||
var pdf = new Formio([form_id]); | ||
|
||
// change submission state to save it as draft | ||
submission.state = 'draft'; | ||
pdf.loadForm().then(function(res) { | ||
// create pdf url download | ||
var parentProjectId = res.project | ||
var downloadUrl = 'https://files.form.io/pdf/' + | ||
parentProjectId + | ||
'/file/pdf/download?format=pdf'; | ||
var pdfSubmission = { | ||
data: {} | ||
}; | ||
res.components.forEach(function(component) { | ||
console.log(component.key, data[component.key]); | ||
if (component.input && data[component.key]) { | ||
pdfSubmission.data[component.key] = data[component.key]; | ||
} | ||
}); | ||
console.log(pdfSubmission); | ||
var requestJson = JSON.stringify({ | ||
form: res, | ||
submission: pdfSubmission | ||
}); | ||
// getting file | ||
fetch(downloadUrl, { | ||
method: 'POST', | ||
body: requestJson, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-file-token': fileToken | ||
} | ||
}) | ||
.then(function(res) { | ||
// creating blob | ||
return res.blob(); | ||
}) | ||
.then(function(blob) { | ||
// creating file | ||
var reader = new FileReader(); | ||
|
||
reader.onload = function() { | ||
//creating link | ||
var downloadLink = document.createElement('a'); | ||
downloadLink.href = reader.result; | ||
downloadLink.download = pdfFileName + '.pdf'; | ||
//simulating click to download | ||
downloadLink.click(); | ||
instance.label = instance.originalComponent.label; | ||
instance.redraw(); | ||
}; | ||
|
||
reader.readAsDataURL(blob); | ||
}) | ||
.catch(function(err) { | ||
// errors | ||
alert('Error while generating PDF'); | ||
instance.label = instance.originalComponent.label; | ||
instance.redraw(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Generate PDF of Current Form View | ||
// Replace `[token_id]` and `[form_id]` | ||
var pdfFileName = data.orderedForUsernname && data.nucoreOrderNumber ? data.orderedForUsernname + '-' + data.nucoreOrderNumber : 'document'; | ||
var fileToken = '[token_id]'; // PDF token | ||
var currentForm = '[form_id]'; | ||
|
||
instance.label = 'Generating Preview'; // change button label | ||
instance.redraw(); | ||
// change submission state to draft. | ||
submission.state = 'draft'; | ||
fetch(currentForm, { | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
.then(function(data) { | ||
//reciving data via Fetch | ||
return data.json(); | ||
}) | ||
.then(function(res) { | ||
// Create pdf file link | ||
var parentProjectId = res.project | ||
var downloadUrl = 'https://files.form.io/pdf/' + | ||
parentProjectId + | ||
'/file/pdf/download?format=pdf'; | ||
|
||
// Build the submission to preview | ||
var requestJson = JSON.stringify({ | ||
form: res, | ||
submission: { | ||
data: data | ||
} | ||
}); | ||
// Submit the form as draft | ||
fetch(downloadUrl, { | ||
method: 'POST', | ||
body: requestJson, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-file-token': fileToken | ||
} | ||
}) | ||
.then(function(response) { | ||
//creates a blob | ||
instance.label = 'Downloading PDF'; // change button label | ||
instance.redraw(); | ||
return response.blob(); | ||
|
||
}) | ||
.then(function(blob) { | ||
// Read the file | ||
var reader = new FileReader(); | ||
|
||
reader.onload = function() { | ||
// Create an Element to download pdf | ||
var downloadLink = document.createElement('a'); | ||
downloadLink.href = reader.result; | ||
downloadLink.download = pdfFileName + '.pdf'; | ||
downloadLink.click(); | ||
instance.label = instance.originalComponent.label; | ||
instance.redraw(); | ||
instance.label = 'Saving'; // change button label | ||
instance.redraw(); | ||
form.submit(); | ||
}; | ||
|
||
reader.readAsDataURL(blob); | ||
}) | ||
.catch(function(err) { | ||
alert('Error while generating PDF'); | ||
instance.label = instance.originalComponent.label; | ||
instance.redraw(); | ||
}); | ||
|
||
}); |
Binary file not shown.