You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

fresh-tabula-js在NestJS中无法运行的问题及解决咨询

在NestJS中使用fresh-tabula-js提取PDF表格的问题解决

问题描述

在Express服务器中可正常使用fresh-tabula-js提取PDF表格,但迁移到NestJS后出现两个问题:

  • 调用getData()时,报错"this.tabula.getData is not a function"
  • 调用extractCsv()时,报错"error TS2339: Property 'extractCsv' does not exist on type 'Tabula'"

参考代码示例

Express 可正常运行的代码

import Tabula from "fresh-tabula-js";

const table = new Tabula("t5.pdf", { spreadsheet: true });
console.log(table.extractCsv().output);

NestJS 报错代码1

import { Injectable } from '@nestjs/common';
import Tabula = require('fresh-tabula-js');

@Injectable()
export class PdfExtractorService {
  tabula: Tabula;
  constructor() {
    this.tabula = new Tabula('t5.pdf', { spreadsheet: true });
  }

  extractPdfToJson() {
    const data: Promise<{ output: string; error: string }> =
      this.tabula.getData();
    console.log(data);
  }
}

NestJS 报错代码2

import { Injectable } from '@nestjs/common';
import Tabula = require('fresh-tabula-js');

@Injectable()
export class PdfExtractorService {
  tabula: Tabula;
  constructor() {
    this.tabula = new Tabula('t5.pdf', { spreadsheet: true });
  }

  extractPdfToJson() {
    const data: Promise<{ output: string; error: string }> =
      this.tabula.extractCsv().output;
    console.log(data);
  }
}

问题原因

  1. 导入方式不兼容:fresh-tabula-js采用CommonJS导出,NestJS的TypeScript环境下直接用require导入会导致类型识别异常。
  2. TypeScript类型缺失:该包没有官方提供的TS类型定义,导致TS无法识别类的方法。
  3. 异步方法调用错误:extractCsv()和getData()都是异步方法,直接访问.output会跳过Promise的异步处理,同时触发类型错误。

解决方案

1. 修正导入方式

在tsconfig.json中确保启用以下配置:

{
  "compilerOptions": {
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
  }
}

然后改用默认导入:

import Tabula from 'fresh-tabula-js';

2. 添加自定义类型声明

在项目的src目录下创建fresh-tabula-js.d.ts文件,补充类型定义:

declare module 'fresh-tabula-js' {
  export interface TabulaResult {
    output: string;
    error: string;
  }

  class Tabula {
    constructor(pdfPath: string, options?: { spreadsheet?: boolean });
    getData(): Promise<TabulaResult>;
    extractCsv(): Promise<TabulaResult>;
    // 若需要其他方法,可在此补充
  }

  export default Tabula;
}

3. 正确处理异步方法

将方法改为异步,使用await处理Promise:

import { Injectable } from '@nestjs/common';
import Tabula from 'fresh-tabula-js';

@Injectable()
export class PdfExtractorService {
  private tabula: Tabula;

  constructor() {
    this.tabula = new Tabula('t5.pdf', { spreadsheet: true });
  }

  async extractPdfToJson() {
    try {
      const result = await this.tabula.getData();
      console.log('提取的JSON数据:', result.output);
      return result.output;
    } catch (err) {
      console.error('PDF提取失败:', err);
      throw err;
    }
  }

  async extractPdfToCsv() {
    try {
      const result = await this.tabula.extractCsv();
      console.log('提取的CSV数据:', result.output);
      return result.output;
    } catch (err) {
      console.error('CSV提取失败:', err);
      throw err;
    }
  }
}

内容的提问来源于stack exchange,提问作者Rajesh Panjiyar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 13:46:27