Extract Structured Data from PDFs Using AI
This tutorial demonstrates how to build an AI-powered document extraction workflow that converts PDF content into structured application data, including:
- Loading and processing PDF documents for AI extraction,
- Converting PDF pages into images for processing,
- Extracting structured JSON data from unstructured document content,
- Validating AI-generated output using a schema, and
- Making extracted values available to workflows, services, or application logic.
Prerequisites
Section titled “Prerequisites”Before starting this tutorial, ensure the following resources are available:
- A configured AI connection,
- A sample PDF document, and
- Access to the file system (Administer -> File System) to upload or verify the sample PDF file used in this tutorial.
Extraction workflow
Section titled “Extraction workflow”The following steps demonstrate how to configure and implement the document extraction workflow.
This document extraction workflow can also be adapted to process other types of PDF documents and extract different kinds of structured application data.
-
Open the console by clicking the console icon in the top-right nav bar, or press
F8. -
Set the Module context to the correct application module (e.g.,
Orders), or use the wildcard option.
1. Initialize the AI model
Section titled “1. Initialize the AI model”Initialize a new AI model object.
const ai = new Sys.Ai.SimpleAi(Common.$Config.AiConnection)2. Load the PDF document
Section titled “2. Load the PDF document”Load the PDF from the working file area and read it.
const drawingFile = await Sys.Fs.readFile( 'working', 'drawings/drawing04.pdf', //Relative path to the PDF { format: 'binary' });This code snippet returns a FileAndContent object with the properties:
fileName, andcontent(contains the PDF file as a binary buffer before it is converted into a series of images).
Troubleshooting Tip
File names and file paths are case-sensitive. For example, drawings/drawing04.pdf and Drawings/drawing04.pdf are treated as different file paths by the Lowgile platform.
3. Convert the PDF file into images
Section titled “3. Convert the PDF file into images”Convert the PDF file into PNG images before sending the it to the AI model.
const images = await Sys.Pdf.toPngImages(drawingFile.content, { scale: 2 });Insight
Most multimodal AI providers process document pages as image input, allowing the AI model to inspect visible document content such as text, labels, tables, and drawing annotations.
The next line of code adds the images to the call to the AI model.
await ai.addImageDocuments(images);4. Add the reference data
Section titled “4. Add the reference data”Provide the AI model with a list of known materials (stored in a static entity) so it can correctly identify and extract them in the technical document.
const materials = this.Materials.getEntryList().map(e => ({ ...e, id: undefined }))Note
Refer to the Create a Materials Static Entity document for more information on creating the Materials static entity.
5. Extract the list of materials
Section titled “5. Extract the list of materials”Send the prompt, reference data, and schema definition to the AI model.
return ai.queryAndReturnStructuredObject ( 'What material(s) are used in the attached drawing? The list of known materials is ${JSON.stringify(materials)}', z => z.object ({ materialsAsMentionedInDrawing: z.string(), materials: z.array(z.object({ name: z.enum(materials.map(m => m.name)), nameAsMentionedInDrawing: z.string(), whereMaterialWasMentioned: z.string(), confidencePercentage: z.number() })) }))Tip
See the arrow functions reference for more information on the arrow function pattern.
Structured object
Section titled “Structured object”This object defines the schema the AI model must return.
z => z.object ({ materialsAsMentionedInDrawing: z.string(), materials: z.array(z.object({ name: z.enum(materials.map(m => m.name)), nameAsMentionedInDrawing: z.string(), whereMaterialWasMentioned: z.string(), confidencePercentage: z.number() })) })The schema requires the AI model to return:
- The materials mentioned in the drawing,
- A structured array of extracted material objects,
- The original material names found in the document,
- Where each material was identified, and
- A confidence score for each extracted value.
Principle
In AI systems, a confidence percentage (or confidence score/rating) is an estimate of how certain the model is that its output is correct.
Response
Section titled “Response”When executed, the AI provider returns structured data that matches the schema.
For example:
{ "materialsAsMentionedInDrawing": "PMMA or PETG", "materials": [ { "name": "PMMA", "nameAsMentionedInDrawing": "PMMA", "whereMentioned": "Material field in the drawing title block", "confidence": 0.95 }, { "name": "PETG", "nameAsMentionedInDrawing": "PETG", "whereMentioned": "Alternative material note in the drawing", "confidence": 0.91 } ]}Key Takeaway
This workflow demonstrates how Lowgile applications can convert unstructured PDF documents into structured application data. These extracted values can then be:
- Stored in the database,
- Used to update workflow variables,
- Used to drive process decisions,
- Used to pre-fill task screens, or
- Passed to another service or integration.