- Source file
- Document ID
- Answers to review
Review the draft before submitting.
Source document · Open document
JSON definition · shared/definitions/extract-from-paper.json
View on GitHub
{
"title": "Work order",
"description": "Completed on paper by the technician and scanned at the office",
"elements": [
{
"type": "text",
"name": "customer_name",
"title": "Customer name"
},
{
"type": "text",
"name": "site_address",
"title": "Site address"
},
{
"type": "text",
"name": "visit_date",
"title": "Visit date",
"inputType": "date"
},
{
"type": "radiogroup",
"name": "work_type",
"title": "Type of work",
"choices": [
{
"value": "installation",
"text": "Installation"
},
{
"value": "repair",
"text": "Repair"
},
{
"value": "inspection",
"text": "Inspection"
}
]
},
{
"type": "checkbox",
"name": "parts",
"title": "Parts replaced",
"choices": [
{
"value": "filter",
"text": "Filter"
},
{
"value": "valve",
"text": "Valve"
},
{
"value": "pump",
"text": "Pump"
},
{
"value": "sensor",
"text": "Sensor"
}
]
},
{
"type": "text",
"name": "hours",
"title": "Hours on site",
"inputType": "number"
},
{
"type": "boolean",
"name": "resolved",
"title": "Issue resolved"
},
{
"type": "text",
"name": "technician",
"title": "Technician"
},
{
"type": "comment",
"name": "notes",
"title": "Notes"
}
]
}
Client · shared/client/extract-from-paper.js · sjs:extract-from-paper.client
View on GitHub
// Extract a draft without saving it and validate the response before replacing any answers
async function extract(file, signal) {
const body = new FormData();
body.append("scan", file);
const response = await fetch("/api/work-orders/extract", { method: "POST", body, signal });
if (!response.ok) throw await responseError(response, "Couldn't extract the answers. Try again or enter the answers directly in the form.");
const result = await response.json();
if (!result.answers || typeof result.answers !== "object" || Array.isArray(result.answers) || !Array.isArray(result.confidence)) {
throw new Error("The extraction result couldn't be read. Try again or enter the answers directly in the form.");
}
return result;
}
Client · shared/client/extract-from-paper.js · sjs:extract-from-paper.save
View on GitHub
// Save only after human review and confirm the response ID returned by the backend
survey.onComplete.add(async (sender, options) => {
lock(true, true);
sender.completedHtml = "<h3>Saving the reviewed response...</h3>";
options.showSaveInProgress();
try {
const data = Object.fromEntries(sender.getAllQuestions()
.filter(q => q !== reviewed).map(q => [q.valueName || q.name, q.isEmpty() ? null : q.value]));
const response = await fetch("/api/responses", {
method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ formId: "work-order", data })
});
if (!response.ok) throw await responseError(response, "Couldn't save the reviewed response. Please try again.");
const { id } = await response.json();
if (!Number.isInteger(id) || id < 1) throw new Error("The server didn't confirm a saved response. Please try again.");
setStoredQuery({ id });
sender.completedHtml = `<h3>The reviewed response has been saved.</h3><p>Response ID: ${id}</p>`;
showStatus(`Reviewed response #${id} saved.`);
options.showSaveSuccess();
} catch (error) {
sender.completedHtml = "<h3>The reviewed response hasn't been saved.</h3>";
options.showSaveError(error instanceof TypeError ? "Couldn't save the reviewed response. Please try again." : error.message);
} finally { lock(false); }
});
Server · src/main/java/io/surveyjs/examples/ExtractFromPaperController.java · sjs:extract-from-paper.template
View on GitHub
// Generate a blank, fillable PDF using the same schema as extraction
@GetMapping("/api/work-orders/blank.pdf")
public ResponseEntity<byte[]> blankWorkOrder() {
JsonNode form = workOrder().orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Unknown work order form"));
ServiceAnswer pdf = surveyjs.post("/pdf", Map.of("schema", form), 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.attachment().filename("work-order.pdf").build().toString())
.cacheControl(CacheControl.noStore().cachePrivate())
.body(pdf.bytes());
}
Server · src/main/java/io/surveyjs/examples/ExtractFromPaperController.java · sjs:extract-from-paper.server
View on GitHub
// POST /api/work-orders/extract — send the uploaded scan to the SurveyJS service; return a draft response
@PostMapping(path = "/api/work-orders/extract", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Draft extractWorkOrder(MultipartHttpServletRequest request) throws IOException {
MultipartFile scan = request.getFile("scan");
if (scan == null || scan.getSize() > MAX_BYTES) { // missing, a text field (not a file), or too large
throw scan == null && request.getParameter("scan") == null
? new ResponseStatusException(HttpStatus.BAD_REQUEST, "Upload the scan in a field named \"scan\"")
: new ResponseStatusException(HttpStatus.CONTENT_TOO_LARGE, "The scan must be a file of 5 MB or less");
}
if (FileType.detect(scan) == null) { // by the first bytes, never the client's Content-Type
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The file is not a PDF, PNG, JPEG, WebP or GIF document");
}
ServiceAnswer out = surveyjs.post("/extract", Map.of("schema", workOrder().orElseThrow(), "document", Base64.getEncoder().encodeToString(scan.getBytes())), Duration.ofSeconds(120));
JsonNode result = out.json(); // JSON with base64 went out, not multipart; an answer that isn't JSON fails closed
if (out.status() == 200 && result != null && result.path("data").isObject()) { // nothing is saved: a person reviews the draft
return new Draft(result.get("data"), result.has("confidence") ? result.get("confidence") : json.createArrayNode(), result.get("uniqueId"));
}
throw switch (out.status() + " " + (result == null ? "" : result.path("error").asString(""))) {
case "400 INVALID_DOCUMENT" -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "The file is not a PDF, PNG, JPEG, WebP or GIF document");
case "503 AI_NOT_CONFIGURED" -> new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "The SurveyJS service has no AI provider configured");
case "502 EXTRACTION_FAILED" -> new ResponseStatusException(HttpStatus.BAD_GATEWAY, "Could not extract the response from the document");
default -> surveyjs.unexpectedExtraction(out); // fail closed, logged by status only
};
}