Skip to content

Commit

Permalink
Add form.io tips and tricks (wyeworks#2146)
Browse files Browse the repository at this point in the history
# 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
2 people authored and joshea0 committed Oct 23, 2020
1 parent 0216763 commit 73825b0
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,5 @@ There are valuable resources in the NUcore's doc directory.
* Need to use a 3rd party service with your NUcore? [**See HOWTO_external_services.txt**](doc/HOWTO_external_services.md)
* Need to asynchronously monitor some aspect of NUcore? [**See HOWTO_daemons.txt**](doc/HOWTO_daemons.txt)
* Want to integrate with Form.io? [**See form.io_tips_and_tricks**](vendor/engines/form.io_tips_and_tricks.docx)
69 changes: 69 additions & 0 deletions doc/form.io/generate_inline_custom_pdf.js
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();
});
});
76 changes: 76 additions & 0 deletions doc/form.io/generate_pdf_of_current_form.js
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 added doc/form.io_tips_and_tricks.docx
Binary file not shown.

0 comments on commit 73825b0

Please sign in to comment.