@cedrugs/pdf-parse

中文文档
1.0.0发布于 2025/09/05MITTypeScriptCommonJS近 30 天下载 6489

Fork of pdf-parse with fixes

原始使用文档
文档目录20 节
  1. Why this fork?
  2. Installation
  3. Option A — Use this scoped package directly
  4. Option B — Keep the old import name via npm alias
  5. Basic Usage — Local Files
  6. CommonJS
  7. ESM / TypeScript
  8. Basic Usage — HTTP
  9. Exception Handling
  10. TypeScript
  11. Options
  12. pagerender (callback)
  13. max (number)
  14. version (string, pdf.js version)
  15. Extend — Custom pagerender
  16. Similar Packages
  17. Tests
  18. Support / Issues
  19. License
  20. Acknowledgements

@cedrugs/pdf-parse

Pure JavaScript, cross-platform PDF text & metadata extraction.
A maintained fork of pdf-parse with bundled TypeScript types, ESM-friendly default export, and Node 18+ support.

npm license node


Why this fork?

  • Types included — ships index.d.ts (no need for @types/pdf-parse).
  • ESM & CJS friendly — default import works out of the box.
  • Same API as upstream for drop-in replacement.
  • Node 18+ minimum for modern runtimes.

If you want to keep your existing import pdf from "pdf-parse" usage, see the Install (alias) section.


Installation

Option A — Use this scoped package directly

npm i @cedrugs/pdf-parse
# or
pnpm add @cedrugs/pdf-parse
# or
yarn add @cedrugs/pdf-parse

Option B — Keep the old import name via npm alias

npm i pdf-parse@npm:@cedrugs/pdf-parse

Now import pdf from "pdf-parse" continues to work, but resolves to this fork.


Basic Usage — Local Files

CommonJS

const fs = require('fs');
const pdf = require('@cedrugs/pdf-parse'); // or 'pdf-parse' if using the alias

const dataBuffer = fs.readFileSync('path/to/file.pdf');

pdf(dataBuffer).then((data) => {
    // number of pages
    console.log(data.numpages);
    // number of rendered pages
    console.log(data.numrender);
    // PDF info
    console.log(data.info);
    // PDF metadata
    console.log(data.metadata);
    // PDF.js version
    // see https://mozilla.github.io/pdf.js/getting_started/
    console.log(data.version);
    // PDF text
    console.log(data.text);
});

ESM / TypeScript

import pdf from '@cedrugs/pdf-parse'; // default export supported
import { readFileSync } from 'node:fs';

const buf = readFileSync('path/to/file.pdf');
const data = await pdf(buf);

console.log(data.text);

Basic Usage — HTTP

You can use packages like crawler-request which integrate with pdf-parse.


Exception Handling

const fs = require('fs');
const pdf = require('@cedrugs/pdf-parse');

const buf = fs.readFileSync('path/to/file.pdf');

pdf(buf)
    .then((data) => {
        // use data
    })
    .catch((err) => {
        // handle exceptions
        console.error(err);
    });

TypeScript

This fork bundles its own .d.ts. No external @types/pdf-parse needed.

import pdf from '@cedrugs/pdf-parse';

const res = await pdf(Buffer.from('...'));
res.text;       // string
res.numpages;   // number
res.version;    // "default" | "v1.9.426" | "v1.10.100" | "v1.10.88" | "v2.0.550"

Options

const DEFAULT_OPTIONS = {
    // internal page parser callback
    // set this if you need a custom output format instead of raw text
    pagerender: render_page,

    // max page number to parse (<= 0 means “all pages”)
    max: 0,

    // see https://mozilla.github.io/pdf.js/getting_started/
    version: 'v1.10.100'
};

pagerender (callback)

Customize how each page is rendered/extracted if you need structured output beyond plain text.

max (number)

Maximum number of pages to parse. Use 0 or a negative value to parse all pages.

version (string, pdf.js version)

  • 'default'
  • 'v1.9.426'
  • 'v1.10.100'
  • 'v1.10.88'
  • 'v2.0.550'

Default version is v1.10.100. See the pdf.js getting started guide.


Extend — Custom pagerender

function render_page(pageData) {
    // see https://mozilla.github.io/pdf.js/
    const render_options = {
        // replace all whitespace with standard spaces (0x20)
        normalizeWhitespace: false,
        // do not attempt to combine cedrugse-line TextItems
        disableCombineTextItems: false
    };

    return pageData.getTextContent(render_options).then((textContent) => {
        let lastY;
        let text = '';
        for (const item of textContent.items) {
            if (lastY === item.transform[5] || !lastY) {
                text += item.str;
            } else {
                text += '\n' + item.str;
            }
            lastY = item.transform[5];
        }
        return text;
    });
}

const options = { pagerender: render_page };
const dataBuffer = require('fs').readFileSync('path/to/file.pdf');

require('@cedrugs/pdf-parse')(dataBuffer, options).then((data) => {
    // use custom-formatted output
});

Similar Packages

(Different trade-offs: dependencies, maintenance, platform support, and output formats.)


Tests

  • npm test (runs mocha)
  • See the test directory in upstream for usage ideas; contributions adding tests here are welcome.

Support / Issues


License

MIT

Acknowledgements

This project builds on the excellent work of the original pdf-parse and the PDF.js team.

版本与发布记录

@cedrugs/pdf-parse 共发布 1 个版本,首次发布于 2025/09/05;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2025/09/051.2 年前

关于 @cedrugs/pdf-parse 的常见问题

@cedrugs/pdf-parse 是什么?

Fork of pdf-parse with fixes

如何安装 @cedrugs/pdf-parse?

在项目目录运行 npm install @cedrugs/pdf-parse,或使用 pnpm add / yarn add / bun add。

@cedrugs/pdf-parse 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cedrugs/pdf-parse 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cedrugs/pdf-parse 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cedrugs/pdf-parse 可以商用吗?

@cedrugs/pdf-parse 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cedrugs/pdf-parse 的下载量有多少?

根据 npm 官方下载统计,@cedrugs/pdf-parse 在 2026/10/10 – 2026/11/08 这 30 天内被下载 6,489 次,日均约 216 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cedrugs/pdf-parse 还在维护吗?

最新版本 1.0.0 发布于 2025/09/05(1.2 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/09/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。