JSON definition · shared/definitions/relational-storage.v1.json
View on GitHub
{
"title": "Insurance claim",
"elements": [
{
"type": "text",
"name": "q_email",
"valueName": "customer_email",
"dbColumn": "claims.customer_email",
"title": "Email",
"inputType": "email",
"isRequired": true
},
{
"type": "text",
"name": "q_amount",
"valueName": "amount",
"dbColumn": "claims.amount",
"title": "Amount claimed",
"inputType": "number",
"min": 0
},
{
"type": "dropdown",
"name": "incident",
"title": "What happened?",
"choices": [
{
"value": "collision",
"text": "Collision"
},
{
"value": "theft",
"text": "Theft"
},
{
"value": "weather",
"text": "Storm or flood"
}
]
},
{
"type": "comment",
"name": "details",
"title": "Details"
},
{
"type": "boolean",
"name": "priority_handling",
"title": "Priority handling"
}
]
}
Client · shared/client/server-pdf.js · sjs:server-pdf.client
View on GitHub
// Open a tab during the click and navigate only after the server returns a PDF
async function generatePdf(id, action) {
if (urls.has(id)) return usePdfUrl(id, action, urls.get(id));
const tab = action === "open" ? window.open("/examples/server-pdf/loading", "_blank") : null;
try {
const response = await fetch(`/api/claims/${id}/pdf`);
if (!response.ok) throw await responseError(response, "Couldn't generate the PDF. Please try again.");
if (!response.headers.get("content-type")?.startsWith("application/pdf")) throw new Error("The server didn't return a PDF. Please try again.");
const blob = await response.blob();
if (!blob.size) throw new Error("The server returned an empty PDF. Please try again.");
const url = URL.createObjectURL(blob);
urls.set(id, url);
if (action === "download") return usePdfUrl(id, action, url);
const opened = tab && !tab.closed;
if (opened) tab.location = url;
return { message: opened ? "PDF opened in a new tab." : "PDF ready. Select Open PDF again or Download PDF.", needsAction: !opened };
} catch (error) { tab?.close(); throw error; }
}
Server · src/main/java/io/surveyjs/examples/ServerPdfController.java · sjs:server-pdf.server
View on GitHub
// GET /api/claims/{id}/pdf — ask the SurveyJS service for the filled form and pass the PDF through
@GetMapping("/api/claims/{id:\\d+}/pdf")
public ResponseEntity<byte[]> claimPdf(@PathVariable long id) throws IOException {
Claim claim = jdbc.sql("SELECT definition_version, data FROM responses WHERE form_id = 'claim' AND id = ?").param(id)
.query(Claim.class).optional().orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Unknown claim"));
String definition = switch (claim.definitionVersion()) { // use the schema version recorded with the response
case "v1", "v2" -> Files.readString(DEFINITIONS.resolve("relational-storage." + claim.definitionVersion() + ".json"));
case null -> jdbc.sql("SELECT json FROM forms WHERE key = 'claim'").query(String.class).single();
default -> throw new ResponseStatusException(HttpStatus.CONFLICT, "The schema version for this claim is unavailable");
};
ServiceAnswer pdf = surveyjs.post("/pdf", Map.of("schema", json.readTree(definition), "response", json.readTree(claim.data())), Duration.ofSeconds(60));
if (pdf.status() != 200 || !pdf.hasContentType("application/pdf") || !pdf.startsWith("%PDF-")) { // a 200 alone isn't a PDF
throw surveyjs.unexpected("/pdf", pdf);
}
return ResponseEntity.ok().contentType(MediaType.APPLICATION_PDF)
.header(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.inline().filename("claim-" + id + ".pdf").build().toString())
.cacheControl(CacheControl.noStore().cachePrivate())
.body(pdf.bytes());
}