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 · Endpoints/ServerPdf.cs · 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
app.MapGet("/api/claims/{id:long}/pdf", async (long id, SurveyDb db, SurveyJsService service, IWebHostEnvironment env, HttpResponse response) =>
{
var claim = await db.Responses.SingleOrDefaultAsync(r => r.FormId == "claim" && r.Id == id);
if (claim is null) return Results.Json(new { error = "Unknown claim" }, statusCode: 404);
// Use the schema version recorded with the response
if (claim.DefinitionVersion is not (null or "v1" or "v2"))
return Results.Json(new { error = "The schema version for this claim is unavailable" }, statusCode: 409);
var definition = claim.DefinitionVersion is null
? await db.Forms.Where(f => f.Key == "claim").Select(f => f.Json).SingleAsync()
: await File.ReadAllTextAsync(Path.Combine(env.ContentRootPath, "shared", "definitions", $"relational-storage.{claim.DefinitionVersion}.json"));
var pdf = await service.PostAsync("/pdf", new { schema = JsonNode.Parse(definition), response = JsonNode.Parse(claim.Data) }, TimeSpan.FromSeconds(60));
if (pdf.Status != 200 || !pdf.HasContentType("application/pdf") || !pdf.BodyStartsWith("%PDF-"))
throw service.Unexpected("/pdf", pdf);
response.Headers.ContentDisposition = $"inline; filename=\"claim-{id}.pdf\"";
response.Headers.CacheControl = "private, no-store";
return Results.Bytes(pdf.Body, "application/pdf");
});