DocumentAnalysisService
- class DocumentAnalysisService
-
- Fully qualified name
-
\Netresearch\
Nr Llm\ Specialized\ Document\ Document Analysis Service
Stateless "understand this document" primitive (ADR-076).
When the resolved provider implements
Document(Gemini, Claude), the PDF is ingested natively as a Base64 document block in a single chat call — whole-document reasoning. Otherwise the document is rasterized page-by-page with poppler and each page is read by the vision model; the per-page answers are concatenated withCapable Interface [Page N]markers.- analyzeDocument ( string $pdf, string $prompt, ?ChatOptions $options = null) : DocumentAnalysisResult
-
Analyze a PDF with a custom prompt.
- param string $pdf
-
Raw PDF bytes (must start with the
%PDF-header) - param string $prompt
-
Analysis prompt, applied to the whole document on the native path and to each page on the fallback
- param ChatOptions|null $options
-
Chat options;
provider,model,maxTokens,temperatureand the budget attribution fields (beUserUid,plannedCost) are passed through to whichever path runs - throws
-
Unsupportedwhen the bytes are not a PDFFormat Exception - throws
-
Providerwhen no provider is resolvable (no explicit provider and no active default configuration) — the same error a plainException chatcall raises() - throws
-
Servicewhen the provider lacks native PDF support and poppler is not installedUnavailable Exception - throws
-
Pdfwhen rasterization itself failsRasterization Exception
- Returns
-
Documentwith the answer text, the model/provider that produced it, whether the native document path was used, and the rasterized page countAnalysis Result
Usage
use Netresearch\NrLlm\Service\Option\ChatOptions;
use Netresearch\NrLlm\Specialized\Document\DocumentAnalysisService;
public function __construct(
private readonly DocumentAnalysisService $documentAnalysis,
) {}
$result = $this->documentAnalysis->analyzeDocument(
$pdfBytes,
'List the key obligations defined in this contract.',
new ChatOptions(maxTokens: 1024),
);
$result->text; // the answer
$result->usedNativeDocumentPath; // true: one whole-document call
$result->rasterizedPageCount; // pages read on the fallback path
Optional system dependency: poppler
The rasterization fallback shells out to the poppler binaries
pdftoppm and pdfimages (Debian/Ubuntu package
poppler-utils; declared in composer.json suggest). They
are only needed when the resolved provider has no native PDF
support. Without them, the fallback fails with the typed
Service (code 1784211009) naming
both remedies: install poppler-utils or configure a
document-capable provider. The native path never touches poppler.
Degradation policy is the caller's: on the fallback path a failed page fails the call — catch and retry (or degrade) in the consumer.
PdfRasterizerInterface
- class PdfRasterizerInterface
-
- Fully qualified name
-
\Netresearch\
Nr Llm\ Specialized\ Document\ Pdf Rasterizer Interface
Rasterizes PDF pages to PNG blobs. The default implementation is the poppler-backed
Poppler; substitute it by aliasing the interface to another class in yourPdf Renderer Services..yaml - imagePages ( string $absolutePath) : array
-
1-based page numbers carrying at least one embedded raster image.