diff --git a/surveyjs-pdf-nodejs/.gitignore b/surveyjs-pdf-nodejs/.gitignore new file mode 100644 index 0000000..38adffa --- /dev/null +++ b/surveyjs-pdf-nodejs/.gitignore @@ -0,0 +1,28 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +.DS_Store +dist +dist-ssr +coverage +*.local + +/cypress/videos/ +/cypress/screenshots/ + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/surveyjs-pdf-nodejs/README.MD b/surveyjs-pdf-nodejs/README.MD index 91d8763..39f4703 100644 --- a/surveyjs-pdf-nodejs/README.MD +++ b/surveyjs-pdf-nodejs/README.MD @@ -1,38 +1,43 @@ -# Create PDF Surveys on a Server-Side Node.js framework +# Create PDF Forms in Node.js -This example demonstrates how to export a survey to PDF on a server-side Node.js framework. +This example demonstrates how to generate a PDF form using the [SurveyJS](https://surveyjs.io/) form builder libraries and the Node.js server-side framework. -### Run the Demo -To run the demo, do the following. +## Run the project -* Install NPM packages. - Open a terminal and run the following command to install NPM packages. - ``` - npm install - ``` - * Run the `index.js` file to export a survey. - ``` - node index.js - ``` - * The exported PDF will appear within the application folder. Review the `survey-result.pdf` file. +1. Install npm packages.\ + + ``` + npm install + ``` -### How to Export Surveys +1. Execute the `index.js` file to generate a PDF form as a `survey-result.pdf` file. You will find this file in the project folder.\ -To export a survey on Node.js environment, follow these steps: + ``` + node index.js + ``` -* Install the [survey-pdf](https://www.npmjs.com/package/survey-pdf) package. -* Create a [SurveyPDF](https://surveyjs.io/pdf-generator/documentation/api-reference/surveypdf) instance. Its constructor accepts two parameters: a survey JSON schema and export properties. +## How to Generate PDF Forms -* Define question answers. -To specify question answers, define the `data` property of a SurveyJS PDF object. If a survey contains default values and you wish to preserve them, call the `mergeData` function. -> For more information on how to programmatically define question values, refer to the following help topic: [Populate Form Fields](https://surveyjs.io/form-library/documentation/design-survey/pre-populate-form-fields). +To generate a PDF form in a Node.js environment, follow these steps: -* To save a PDF document with the exported survey, call the [save(fileName)](https://surveyjs.io/pdf-generator/documentation/api-reference/surveypdf#save) method on the SurveyPDF instance. +1. Install the [`survey-pdf`](https://www.npmjs.com/package/survey-pdf) npm package. -### Limitations of Exporting Surveys on Node.js +2. Create a [`SurveyPDF`](https://surveyjs.io/pdf-generator/documentation/api-reference/surveypdf) instance. Its constructor accepts two parameters: a survey JSON schema and a [PDF document configuration](https://surveyjs.io/pdf-generator/documentation/api-reference/idocoptions). + +3. Specify the `data` property of a `SurveyPDF` instance to define question answers. If a survey contains default values, and you wish to preserve them, call the `mergeData(newObj)` method instead. For more information on how to programmatically define question answers, refer to the following help topic: [Populate Form Fields](https://surveyjs.io/form-library/documentation/design-survey/pre-populate-form-fields). + +4. Call the [save(fileName)](https://surveyjs.io/pdf-generator/documentation/api-reference/surveypdf#save) method on the `SurveyPDF` instance to save a PDF form. + +## Limitations + +The following question types are not supported when you generate PDF forms in Node.js: -When exporting surveys on Node.js environment, take note that the following question types are not supported: * [HTML](https://surveyjs.io/form-library/documentation/api-reference/add-custom-html-to-survey) -* [Image](https://surveyjs.io/form-library/documentation/api-reference/add-image-to-survey) +* [Image](https://surveyjs.io/form-library/documentation/api-reference/add-image-to-survey) * [Image Picker](https://surveyjs.io/form-library/documentation/api-reference/add-image-to-survey) -* [Signature Pad](https://surveyjs.io/form-library/documentation/api-reference/signature-pad-model) \ No newline at end of file +* [Signature Pad](https://surveyjs.io/form-library/documentation/api-reference/signature-pad-model) + +## SurveyJS PDF Generator Resources + +- [Documentation](https://surveyjs.io/pdf-generator/documentation/overview) +- [Live Examples](https://surveyjs.io/pdf-generator/examples/) \ No newline at end of file diff --git a/surveyjs-pdf-nodejs/index.js b/surveyjs-pdf-nodejs/index.js index 90bcdcf..7d47c30 100644 --- a/surveyjs-pdf-nodejs/index.js +++ b/surveyjs-pdf-nodejs/index.js @@ -1,199 +1,199 @@ const SurveyPDF = require("survey-pdf"); const surveyPDF = new SurveyPDF.SurveyPDF({ - "title": "COVID-19 Screening Form", - "description": "All fields with an asterisk (*) are required fields and must be filled out in order to process the information in strict confidentiality.", - "questionErrorLocation": "bottom", - "pages": [ - { - "name": "patient-info", - "title": "Patient Information", - "elements": [ - { - "type": "panel", - "name": "full-name", - "title": "Full name", - "elements": [ - { - "type": "text", - "name": "first-name", - "title": "First name", - "isRequired": true, - "maxLength": 25 - }, - { - "type": "text", - "name": "last-name", - "startWithNewLine": false, - "title": "Last name", - "isRequired": true, - "maxLength": 25 - } - ] - }, - { - "type": "panel", - "name": "personal-info", - "elements": [ - { - "type": "text", - "name": "ssn", - "title": "Social Security number", - "isRequired": true, - "maxLength": 9, - "validators": [ - { - "type": "regex", - "text": "Your SSN must be a 9-digit number.", - "regex": "^\\d{9}$" - } - ] - }, - { - "type": "text", - "name": "birthdate", - "startWithNewLine": false, - "title": "Date of Birth", - "isRequired": true, - "inputType": "date" - } - ] - } - ] - }, - { - "name": "symptoms", - "title": "Current Symptoms", - "elements": [ - { - "type": "checkbox", - "name": "symptoms", - "title": "Have you experienced any of the following symptoms of COVID-19 within the last 48 hours?", - "isRequired": true, - "choices": [ - "Fever or chills", - "New and persistent cough", - "Shortness of breath or difficulty breathing", - "Fatigue", - "Muscle or body aches", - "New loss of taste or smell", - "Sore throat" - ], - "showNoneItem": true, - "noneText": "No symptoms" - } - ] - }, - { - "name": "contacts", - "title": "Contacts", - "elements": [ - { - "type": "boolean", - "name": "contacted-person-with-symptoms", - "title": "Have you been in contact with anyone in the last 14 days who is experiencing these symptoms?" - }, - { - "type": "radiogroup", - "name": "contacted-covid-positive", - "title": "Have you been in contact with anyone who has since tested positive for COVID-19?", - "choices": [ "Yes", "No", "Not sure" ] - } - ] - }, - { - "name": "travels", - "title": "Travels", - "elements": [ - { - "type": "boolean", - "name": "travelled", - "title": "Have you travelled abroad in the last 14 days?" - }, - { - "type": "text", - "name": "travel-destination", - "visibleIf": "{travelled} = true", - "title": "Where did you go?" - } - ] - }, - { - "name": "tests", - "title": "Tests", - "elements": [ - { - "type": "boolean", - "name": "tested-covid-positive", - "title": "Have you tested positive for COVID-19 in the past 10 days?" - }, - { - "type": "boolean", - "name": "awaiting-covid-test", - "title": "Are you currently awaiting results from a COVID-19 test?" - }, - { - "type": "paneldynamic", - "name": "emergency-contacts", - "title": "Emergency Contacts", - "description": "If possible, it's best to specify at least TWO emergency contacts.", - "panelsState": "firstExpanded", - "confirmDelete": true, - "panelAddText": "Add a new contact person", - "visibleIf": "(({tested-covid-positive} = true or {contacted-covid-positive} = 'Yes') or ({symptoms} notempty and {symptoms} notcontains 'none'))", - "isRequired": true, - "templateElements": [ - { - "type": "text", - "name": "emergency-first-name", - "title": "First name" - }, - { - "type": "text", - "name": "emergency-last-name", - "startWithNewLine": false, - "title": "Last name" - }, - { - "type": "text", - "name": "emergency-relationship", - "title": "Relationship" - }, - { - "type": "text", - "name": "emergency-phone", - "startWithNewLine": false, - "title": "Phone number", - "inputType": "tel" - } - ] - } - ] - }, - { - "name": "finalization", - "title": "Miscellaneous", - "elements": [ - { - "type": "comment", - "name": "additional-info", - "title": "Additional information" - }, - { - "type": "text", - "name": "date", - "title": "Date", - "inputType": "date" - }, - ] - } - ], - "completeText": "Submit", - "showPreviewBeforeComplete": "showAnsweredQuestions", - "showQuestionNumbers": false, - "focusFirstQuestionAutomatic": false, - "widthMode": "static", - "width": "1000px" + "title": "COVID-19 Screening Form", + "description": "All fields with an asterisk (*) are required fields and must be filled out in order to process the information in strict confidentiality.", + "questionErrorLocation": "bottom", + "pages": [ + { + "name": "patient-info", + "title": "Patient Information", + "elements": [ + { + "type": "panel", + "name": "full-name", + "title": "Full name", + "elements": [ + { + "type": "text", + "name": "first-name", + "title": "First name", + "isRequired": true, + "maxLength": 25 + }, + { + "type": "text", + "name": "last-name", + "startWithNewLine": false, + "title": "Last name", + "isRequired": true, + "maxLength": 25 + } + ] + }, + { + "type": "panel", + "name": "personal-info", + "elements": [ + { + "type": "text", + "name": "ssn", + "title": "Social Security number", + "isRequired": true, + "maxLength": 9, + "validators": [ + { + "type": "regex", + "text": "Your SSN must be a 9-digit number.", + "regex": "^\\d{9}$" + } + ] + }, + { + "type": "text", + "name": "birthdate", + "startWithNewLine": false, + "title": "Date of Birth", + "isRequired": true, + "inputType": "date" + } + ] + } + ] + }, + { + "name": "symptoms", + "title": "Current Symptoms", + "elements": [ + { + "type": "checkbox", + "name": "symptoms", + "title": "Have you experienced any of the following symptoms of COVID-19 within the last 48 hours?", + "isRequired": true, + "choices": [ + "Fever or chills", + "New and persistent cough", + "Shortness of breath or difficulty breathing", + "Fatigue", + "Muscle or body aches", + "New loss of taste or smell", + "Sore throat" + ], + "showNoneItem": true, + "noneText": "No symptoms" + } + ] + }, + { + "name": "contacts", + "title": "Contacts", + "elements": [ + { + "type": "boolean", + "name": "contacted-person-with-symptoms", + "title": "Have you been in contact with anyone in the last 14 days who is experiencing these symptoms?" + }, + { + "type": "radiogroup", + "name": "contacted-covid-positive", + "title": "Have you been in contact with anyone who has since tested positive for COVID-19?", + "choices": [ "Yes", "No", "Not sure" ] + } + ] + }, + { + "name": "travels", + "title": "Travels", + "elements": [ + { + "type": "boolean", + "name": "travelled", + "title": "Have you travelled abroad in the last 14 days?" + }, + { + "type": "text", + "name": "travel-destination", + "visibleIf": "{travelled} = true", + "title": "Where did you go?" + } + ] + }, + { + "name": "tests", + "title": "Tests", + "elements": [ + { + "type": "boolean", + "name": "tested-covid-positive", + "title": "Have you tested positive for COVID-19 in the past 10 days?" + }, + { + "type": "boolean", + "name": "awaiting-covid-test", + "title": "Are you currently awaiting results from a COVID-19 test?" + }, + { + "type": "paneldynamic", + "name": "emergency-contacts", + "title": "Emergency Contacts", + "description": "If possible, it's best to specify at least TWO emergency contacts.", + "panelsState": "firstExpanded", + "confirmDelete": true, + "panelAddText": "Add a new contact person", + "visibleIf": "(({tested-covid-positive} = true or {contacted-covid-positive} = 'Yes') or ({symptoms} notempty and {symptoms} notcontains 'none'))", + "isRequired": true, + "templateElements": [ + { + "type": "text", + "name": "emergency-first-name", + "title": "First name" + }, + { + "type": "text", + "name": "emergency-last-name", + "startWithNewLine": false, + "title": "Last name" + }, + { + "type": "text", + "name": "emergency-relationship", + "title": "Relationship" + }, + { + "type": "text", + "name": "emergency-phone", + "startWithNewLine": false, + "title": "Phone number", + "inputType": "tel" + } + ] + } + ] + }, + { + "name": "finalization", + "title": "Miscellaneous", + "elements": [ + { + "type": "comment", + "name": "additional-info", + "title": "Additional information" + }, + { + "type": "text", + "name": "date", + "title": "Date", + "inputType": "date" + }, + ] + } + ], + "completeText": "Submit", + "showPreviewBeforeComplete": "showAnsweredQuestions", + "showQuestionNumbers": false, + "focusFirstQuestionAutomatic": false, + "widthMode": "static", + "width": "1000px" }); surveyPDF.data = {