fix(core): Improve PDF XML extraction, embedding, and format detection; update loadPdf/exportPdf error handling; add new validator implementations and enhance IPdf metadata.
This commit is contained in:
@ -189,34 +189,38 @@ export class XInvoice {
|
||||
public async loadPdf(pdfBuffer: Uint8Array | Buffer, validate: boolean = false): Promise<XInvoice> {
|
||||
try {
|
||||
// Extract XML from PDF using the consolidated extractor
|
||||
// which tries multiple extraction methods in sequence
|
||||
const xmlContent = await this.pdfExtractor.extractXml(pdfBuffer);
|
||||
|
||||
const extractResult = await this.pdfExtractor.extractXml(pdfBuffer);
|
||||
|
||||
// Store the PDF buffer
|
||||
this.pdf = {
|
||||
name: 'invoice.pdf',
|
||||
id: `invoice-${Date.now()}`,
|
||||
metadata: {
|
||||
textExtraction: ''
|
||||
textExtraction: '',
|
||||
format: extractResult.success ? extractResult.format?.toString() : undefined
|
||||
},
|
||||
buffer: pdfBuffer instanceof Buffer ? new Uint8Array(pdfBuffer) : pdfBuffer
|
||||
};
|
||||
|
||||
if (!xmlContent) {
|
||||
// No XML found in PDF
|
||||
console.warn('No XML found in PDF');
|
||||
throw new Error('No XML found in PDF');
|
||||
|
||||
// Handle extraction result
|
||||
if (!extractResult.success || !extractResult.xml) {
|
||||
const errorMessage = extractResult.error ? extractResult.error.message : 'Unknown error extracting XML from PDF';
|
||||
console.warn('XML extraction failed:', errorMessage);
|
||||
throw new Error(`No XML found in PDF: ${errorMessage}`);
|
||||
}
|
||||
|
||||
|
||||
// Load the extracted XML
|
||||
await this.loadXml(xmlContent, validate);
|
||||
|
||||
await this.loadXml(extractResult.xml, validate);
|
||||
|
||||
// Store the detected format
|
||||
this.detectedFormat = extractResult.format || InvoiceFormat.UNKNOWN;
|
||||
|
||||
return this;
|
||||
} catch (error) {
|
||||
console.error('Error loading PDF:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies data from a TInvoice object
|
||||
@ -281,7 +285,7 @@ export class XInvoice {
|
||||
valid: false,
|
||||
errors: [{
|
||||
code: 'VAL-ERROR',
|
||||
message: `Validation error: ${error.message}`
|
||||
message: `Validation error: ${error instanceof Error ? error.message : String(error)}`
|
||||
}],
|
||||
level
|
||||
};
|
||||
@ -356,7 +360,7 @@ export class XInvoice {
|
||||
}
|
||||
|
||||
// Embed XML into PDF
|
||||
const modifiedPdf = await this.pdfEmbedder.createPdfWithXml(
|
||||
const result = await this.pdfEmbedder.createPdfWithXml(
|
||||
this.pdf.buffer,
|
||||
xmlContent,
|
||||
filename,
|
||||
@ -365,7 +369,14 @@ export class XInvoice {
|
||||
this.pdf.id
|
||||
);
|
||||
|
||||
return modifiedPdf;
|
||||
// Handle potential errors
|
||||
if (!result.success || !result.pdf) {
|
||||
const errorMessage = result.error ? result.error.message : 'Unknown error embedding XML into PDF';
|
||||
console.error('Error exporting PDF:', errorMessage);
|
||||
throw new Error(`Failed to export PDF: ${errorMessage}`);
|
||||
}
|
||||
|
||||
return result.pdf;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -392,4 +403,4 @@ export class XInvoice {
|
||||
public isFormat(format: InvoiceFormat): boolean {
|
||||
return this.detectedFormat === format;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user