Skip to content

Commit 083f2e6

Browse files
authored
Merge pull request #14 from surveyjs/create-nodejs-demo
Publish a demo: export surveys on Node.js
2 parents e67ef46 + aea1a61 commit 083f2e6

File tree

4 files changed

+300
-0
lines changed

4 files changed

+300
-0
lines changed

surveyjs-pdf-nodejs/.gitignore

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
.DS_Store
12+
dist
13+
dist-ssr
14+
coverage
15+
*.local
16+
17+
/cypress/videos/
18+
/cypress/screenshots/
19+
20+
# Editor directories and files
21+
.vscode/*
22+
!.vscode/extensions.json
23+
.idea
24+
*.suo
25+
*.ntvs*
26+
*.njsproj
27+
*.sln
28+
*.sw?

surveyjs-pdf-nodejs/README.MD

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Create PDF Forms in Node.js
2+
3+
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.
4+
5+
## Run the project
6+
7+
1. Install npm packages.
8+
9+
```
10+
npm install
11+
```
12+
13+
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.
14+
15+
```
16+
node index.js
17+
```
18+
19+
## How to Generate PDF Forms
20+
21+
To generate a PDF form in a Node.js environment, follow these steps:
22+
23+
1. Install the [`survey-pdf`](https://www.npmjs.com/package/survey-pdf) npm package.
24+
25+
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).
26+
27+
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).
28+
29+
30+
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.
31+
32+
## Limitations
33+
34+
The following question types are not supported when you generate PDF forms in Node.js:
35+
36+
* [HTML](https://surveyjs.io/form-library/documentation/api-reference/add-custom-html-to-survey)
37+
* [Image](https://surveyjs.io/form-library/documentation/api-reference/add-image-to-survey)
38+
* [Image Picker](https://surveyjs.io/form-library/documentation/api-reference/add-image-to-survey)
39+
* [Signature Pad](https://surveyjs.io/form-library/documentation/api-reference/signature-pad-model)
40+
41+
## SurveyJS PDF Generator Resources
42+
43+
- [Documentation](https://surveyjs.io/pdf-generator/documentation/overview)
44+
- [Live Examples](https://surveyjs.io/pdf-generator/examples/)

surveyjs-pdf-nodejs/index.js

+214
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
const SurveyPDF = require("survey-pdf");
2+
3+
const surveyPDF = new SurveyPDF.SurveyPDF({
4+
"title": "COVID-19 Screening Form",
5+
"description": "All fields with an asterisk (*) are required fields and must be filled out in order to process information in strict confidentiality.",
6+
"questionErrorLocation": "bottom",
7+
"pages": [
8+
{
9+
"name": "patient-info",
10+
"title": "Patient Information",
11+
"elements": [
12+
{
13+
"type": "panel",
14+
"name": "full-name",
15+
"title": "Full name",
16+
"elements": [
17+
{
18+
"type": "text",
19+
"name": "first-name",
20+
"title": "First name",
21+
"isRequired": true,
22+
"maxLength": 25
23+
},
24+
{
25+
"type": "text",
26+
"name": "last-name",
27+
"startWithNewLine": false,
28+
"title": "Last name",
29+
"isRequired": true,
30+
"maxLength": 25
31+
}
32+
]
33+
},
34+
{
35+
"type": "panel",
36+
"name": "personal-info",
37+
"elements": [
38+
{
39+
"type": "text",
40+
"name": "ssn",
41+
"title": "Social Security number",
42+
"isRequired": true,
43+
"maxLength": 9,
44+
"validators": [
45+
{
46+
"type": "regex",
47+
"text": "Your SSN must be a 9-digit number.",
48+
"regex": "^\\d{9}$"
49+
}
50+
]
51+
},
52+
{
53+
"type": "text",
54+
"name": "birthdate",
55+
"startWithNewLine": false,
56+
"title": "Date of Birth",
57+
"isRequired": true,
58+
"inputType": "date"
59+
}
60+
]
61+
}
62+
]
63+
},
64+
{
65+
"name": "symptoms",
66+
"title": "Current Symptoms",
67+
"elements": [
68+
{
69+
"type": "checkbox",
70+
"name": "symptoms",
71+
"title": "Have you experienced any of the following symptoms of COVID-19 within the last 48 hours?",
72+
"isRequired": true,
73+
"choices": [
74+
"Fever or chills",
75+
"New and persistent cough",
76+
"Shortness of breath or difficulty breathing",
77+
"Fatigue",
78+
"Muscle or body aches",
79+
"New loss of taste or smell",
80+
"Sore throat"
81+
],
82+
"showNoneItem": true,
83+
"noneText": "No symptoms"
84+
}
85+
]
86+
},
87+
{
88+
"name": "contacts",
89+
"title": "Contacts",
90+
"elements": [
91+
{
92+
"type": "boolean",
93+
"name": "contacted-person-with-symptoms",
94+
"title": "Have you been in contact with anyone in the last 14 days who is experiencing these symptoms?"
95+
},
96+
{
97+
"type": "radiogroup",
98+
"name": "contacted-covid-positive",
99+
"title": "Have you been in contact with anyone who has since tested positive for COVID-19?",
100+
"choices": [ "Yes", "No", "Not sure" ]
101+
}
102+
]
103+
},
104+
{
105+
"name": "travels",
106+
"title": "Travels",
107+
"elements": [
108+
{
109+
"type": "boolean",
110+
"name": "travelled",
111+
"title": "Have you travelled abroad in the last 14 days?"
112+
},
113+
{
114+
"type": "text",
115+
"name": "travel-destination",
116+
"visibleIf": "{travelled} = true",
117+
"title": "Where did you go?"
118+
}
119+
]
120+
},
121+
{
122+
"name": "tests",
123+
"title": "Tests",
124+
"elements": [
125+
{
126+
"type": "boolean",
127+
"name": "tested-covid-positive",
128+
"title": "Have you tested positive for COVID-19 in the past 10 days?"
129+
},
130+
{
131+
"type": "boolean",
132+
"name": "awaiting-covid-test",
133+
"title": "Are you currently awaiting results from a COVID-19 test?"
134+
},
135+
{
136+
"type": "paneldynamic",
137+
"name": "emergency-contacts",
138+
"title": "Emergency Contacts",
139+
"description": "If possible, it's best to specify at least TWO emergency contacts.",
140+
"panelsState": "firstExpanded",
141+
"confirmDelete": true,
142+
"panelAddText": "Add a new contact person",
143+
"visibleIf": "(({tested-covid-positive} = true or {contacted-covid-positive} = 'Yes') or ({symptoms} notempty and {symptoms} notcontains 'none'))",
144+
"isRequired": true,
145+
"templateElements": [
146+
{
147+
"type": "text",
148+
"name": "emergency-first-name",
149+
"title": "First name"
150+
},
151+
{
152+
"type": "text",
153+
"name": "emergency-last-name",
154+
"startWithNewLine": false,
155+
"title": "Last name"
156+
},
157+
{
158+
"type": "text",
159+
"name": "emergency-relationship",
160+
"title": "Relationship"
161+
},
162+
{
163+
"type": "text",
164+
"name": "emergency-phone",
165+
"startWithNewLine": false,
166+
"title": "Phone number",
167+
"inputType": "tel"
168+
}
169+
]
170+
}
171+
]
172+
},
173+
{
174+
"name": "finalization",
175+
"title": "Miscellaneous",
176+
"elements": [
177+
{
178+
"type": "comment",
179+
"name": "additional-info",
180+
"title": "Additional information"
181+
},
182+
{
183+
"type": "text",
184+
"name": "date",
185+
"title": "Date",
186+
"inputType": "date"
187+
},
188+
]
189+
}
190+
],
191+
"completeText": "Submit",
192+
"showPreviewBeforeComplete": "showAnsweredQuestions",
193+
"showQuestionNumbers": false,
194+
"focusFirstQuestionAutomatic": false,
195+
"widthMode": "static",
196+
"width": "1000px"
197+
});
198+
199+
surveyPDF.data = {
200+
"first-name": "Jane",
201+
"last-name": "Doe",
202+
"ssn": "937464652",
203+
"birthdate": "1990-09-21",
204+
"symptoms": [
205+
"none"
206+
],
207+
"contacted-person-with-symptoms": false,
208+
"contacted-covid-positive": "Not sure",
209+
"travelled": false,
210+
"tested-covid-positive": false,
211+
"awaiting-covid-test": false,
212+
"date": "2023-08-29"
213+
}
214+
surveyPDF.save("survey-result.pdf");

surveyjs-pdf-nodejs/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "survey-pdf-node",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"survey-pdf": "^1.9.104"
13+
}
14+
}

0 commit comments

Comments
 (0)