@@ -6,8 +6,19 @@ export interface ParsedInvoice {
66 dueDate ?: string ;
77}
88
9+ /**
10+ * Service for parsing PDF invoice files using optical character recognition (OCR) patterns.
11+ * Extracts key financial metadata like total amounts and due dates.
12+ */
913@Injectable ( )
1014export class PdfService {
15+ /**
16+ * Parses a PDF buffer to extract invoice data.
17+ *
18+ * @param pdfBuffer - The raw binary buffer of the PDF file.
19+ * @returns A promise resolving to a ParsedInvoice object.
20+ * @throws Error if the PDF parsing fails.
21+ */
1122 async parseInvoicePdf ( pdfBuffer : Buffer ) : Promise < ParsedInvoice > {
1223 try {
1324 const data = await pdfParse ( pdfBuffer ) ;
@@ -25,6 +36,14 @@ export class PdfService {
2536 }
2637 }
2738
39+ /**
40+ * Searches the text content for potential currency amounts using regex patterns.
41+ * Identifies the largest amount found as the likely total.
42+ *
43+ * @param text - The raw text extracted from the PDF.
44+ * @returns The largest positive amount found, or undefined if none found.
45+ * @private
46+ */
2847 private extractTotalAmount ( text : string ) : number | undefined {
2948 // Regex patterns to find currency amounts
3049 const patterns = [
@@ -57,6 +76,13 @@ export class PdfService {
5776 return amounts . length > 0 ? Math . max ( ...amounts ) : undefined ;
5877 }
5978
79+ /**
80+ * Searches the text content for potential due dates using regex patterns.
81+ *
82+ * @param text - The raw text extracted from the PDF.
83+ * @returns The first valid date found in ISO format, or undefined.
84+ * @private
85+ */
6086 private extractDueDate ( text : string ) : string | undefined {
6187 // Regex patterns to find dates
6288 const patterns = [
@@ -80,6 +106,13 @@ export class PdfService {
80106 return undefined ;
81107 }
82108
109+ /**
110+ * Normalizes a date string into ISO 8601 format (YYYY-MM-DD).
111+ *
112+ * @param dateString - The raw date string to normalize.
113+ * @returns The normalized date string, or the original if invalid.
114+ * @private
115+ */
83116 private normalizeDate ( dateString : string ) : string {
84117 // Convert various date formats to ISO format (YYYY-MM-DD)
85118 const date = new Date ( dateString ) ;
0 commit comments