Skip to content

Commit b2bcccb

Browse files
fix(ocr): adapt OfficeProcessor to officeparser v6.1.0 ESM changes
v6.1.0 added native ESM with Node16 resolution and a strict exports field, breaking deep subpath imports like officeparser/dist/parsers/ExcelParser.js. Switch to the main package entry and use parseOfficeAsync(), which accepts a Buffer and auto-detects the format via magic bytes. Co-authored-by: Elian Doran <eliandoran@users.noreply.github.com>
1 parent 189867c commit b2bcccb

1 file changed

Lines changed: 16 additions & 22 deletions

File tree

apps/server/src/services/ocr/processors/office_processor.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
import { parseExcel } from 'officeparser/dist/parsers/ExcelParser.js';
2-
import { parseOpenOffice } from 'officeparser/dist/parsers/OpenOfficeParser.js';
3-
import { parsePowerPoint } from 'officeparser/dist/parsers/PowerPointParser.js';
4-
import { parseWord } from 'officeparser/dist/parsers/WordParser.js';
5-
import type { OfficeParserConfig } from 'officeparser/dist/types.js';
1+
import officeparser from 'officeparser';
2+
import type { OfficeParserConfig } from 'officeparser';
63

74
import log from '../../log.js';
85
import { OCRProcessingOptions, OCRResult } from '../ocr_service.js';
96
import { FileProcessor } from './file_processor.js';
107

11-
type Parser = (buffer: Buffer, config: OfficeParserConfig) => Promise<{ toText(): string }>;
12-
13-
const PARSER_BY_MIME: Record<string, Parser> = {
8+
const SUPPORTED_MIME_TYPES = new Set([
149
// Office Open XML
15-
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': parseWord,
16-
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': parseExcel,
17-
'application/vnd.openxmlformats-officedocument.presentationml.presentation': parsePowerPoint,
10+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
11+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
12+
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
1813
// OpenDocument
19-
'application/vnd.oasis.opendocument.text': parseOpenOffice,
20-
'application/vnd.oasis.opendocument.spreadsheet': parseOpenOffice,
21-
'application/vnd.oasis.opendocument.presentation': parseOpenOffice
22-
};
14+
'application/vnd.oasis.opendocument.text',
15+
'application/vnd.oasis.opendocument.spreadsheet',
16+
'application/vnd.oasis.opendocument.presentation'
17+
]);
2318

2419
const PARSER_CONFIG: OfficeParserConfig = {
2520
outputErrorToConsole: false,
@@ -30,29 +25,28 @@ const PARSER_CONFIG: OfficeParserConfig = {
3025

3126
/**
3227
* Office document processor for extracting text from DOCX/XLSX/PPTX and ODT/ODS/ODP files.
33-
* Uses individual parsers from officeparser v6 to avoid pulling in pdfjs-dist.
28+
* Uses officeparser's main API, which auto-detects the format from the buffer's magic bytes.
3429
*/
3530
export class OfficeProcessor extends FileProcessor {
3631

3732
canProcess(mimeType: string): boolean {
38-
return mimeType in PARSER_BY_MIME;
33+
return SUPPORTED_MIME_TYPES.has(mimeType);
3934
}
4035

4136
getSupportedMimeTypes(): string[] {
42-
return Object.keys(PARSER_BY_MIME);
37+
return [...SUPPORTED_MIME_TYPES];
4338
}
4439

4540
async extractText(buffer: Buffer, options: OCRProcessingOptions = {}): Promise<OCRResult> {
4641
const mimeType = options.mimeType;
47-
if (!mimeType || !(mimeType in PARSER_BY_MIME)) {
42+
if (!mimeType || !SUPPORTED_MIME_TYPES.has(mimeType)) {
4843
throw new Error(`Unsupported MIME type for Office processor: ${mimeType}`);
4944
}
5045

5146
log.info(`Starting Office document text extraction for ${mimeType}...`);
5247

53-
const parse = PARSER_BY_MIME[mimeType];
54-
const ast = await parse(buffer, PARSER_CONFIG);
55-
const trimmed = ast.toText().trim();
48+
const text = await officeparser.parseOfficeAsync(buffer, PARSER_CONFIG);
49+
const trimmed = text.trim();
5650

5751
return {
5852
text: trimmed,

0 commit comments

Comments
 (0)