Tech Tip: Processing PDF Documents in 4D AIKit
PRODUCT: 4D | VERSION: 20 R | PLATFORM: Mac & Win
Published On: December 4, 2025
When using 4D AIKit for document analysis, PDFs must first be converted into image formats before they can be processed. The pdfium plugin provides native PDF rendering inside 4D, enabling seamless conversion without external dependencies.
Implementation Notes
Plugin Setup
var $file : 4D.File var $images : Collection var $options : Object var $client:=cs.AIKit.OpenAI.new("your api key") $file := File("/RESOURCES/documents/invoice.pdf") // Convert PDF pages to images using the pdfium method : pdf to images $options:=New object("dpi"; 144) $images:=pdf to image($file; $options) // Process first page only If ($images # Null) & ($images.length > 0) var $blob : Blob var $outputFile : 4D.File // Convert picture to PNG blob PICTURE TO BLOB($images[0]; $blob; "image/png") // Save to temporary location $outputFile:=Folder(fk resources folder).file("temp/page_1.png") $outputFile.setContent($blob) // Use the image with 4D AIKit var $helper := $client.chat.vision.fromFile($outputFile) var $result := $helper.prompt("Extract invoice data as JSON") End if |
Implementation Notes
- Page selection: Extracting only the first page reduces processing time and API cost.
- File handling: Use File.setContent() to store the converted image before sending it to AIKit.
Plugin Setup
- Download the pdfium plugin from GitHub : https://github.com/miyako/4d-plugin-pdfium/releases
- Extract to your project’s Plugins folder
- Restart 4D to activate the plugin
- Confirm availability with the pdf to image() command