pdfxtract 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +40 -0
- package/dist/cli.js +125 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# pdfxtract
|
|
2
|
+
|
|
3
|
+
A lightweight CLI to recursively extract text from PDF files in a directory.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npx pdfxtract <directory> [options]
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Options
|
|
12
|
+
|
|
13
|
+
| Option | Description |
|
|
14
|
+
| --- | --- |
|
|
15
|
+
| `-o, --output <dir>` | Output directory (preserves subdirectory structure) |
|
|
16
|
+
| `--overwrite` | Overwrite existing TXT files |
|
|
17
|
+
| `-v, --verbose` | Show detailed output |
|
|
18
|
+
| `-V, --version` | Show version number |
|
|
19
|
+
| `-h, --help` | Show help |
|
|
20
|
+
|
|
21
|
+
## Examples
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
# Convert all PDFs in place
|
|
25
|
+
npx pdfxtract ./pdfs
|
|
26
|
+
|
|
27
|
+
# Output to a separate directory
|
|
28
|
+
npx pdfxtract ./pdfs -o ./output
|
|
29
|
+
|
|
30
|
+
# Overwrite existing files with verbose logging
|
|
31
|
+
npx pdfxtract ./pdfs -o ./output --overwrite -v
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Requirements
|
|
35
|
+
|
|
36
|
+
Node.js >= 20.0.0
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
MIT
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { basename, dirname, extname, join, relative, resolve } from "node:path";
|
|
5
|
+
import { parseArgs } from "node:util";
|
|
6
|
+
import { extractText, getDocumentProxy } from "unpdf";
|
|
7
|
+
|
|
8
|
+
//#region src/converter.ts
|
|
9
|
+
async function convertPdf(pdfPath, txtPath) {
|
|
10
|
+
const buffer = await readFile(pdfPath);
|
|
11
|
+
const pdf = await getDocumentProxy(new Uint8Array(buffer));
|
|
12
|
+
try {
|
|
13
|
+
const { text } = await extractText(pdf, { mergePages: true });
|
|
14
|
+
await writeFile(txtPath, text, "utf-8");
|
|
15
|
+
} finally {
|
|
16
|
+
pdf.destroy();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/cli.ts
|
|
22
|
+
const { values, positionals } = parseArgs({
|
|
23
|
+
options: {
|
|
24
|
+
output: {
|
|
25
|
+
type: "string",
|
|
26
|
+
short: "o"
|
|
27
|
+
},
|
|
28
|
+
overwrite: {
|
|
29
|
+
type: "boolean",
|
|
30
|
+
default: false
|
|
31
|
+
},
|
|
32
|
+
verbose: {
|
|
33
|
+
type: "boolean",
|
|
34
|
+
short: "v",
|
|
35
|
+
default: false
|
|
36
|
+
},
|
|
37
|
+
version: {
|
|
38
|
+
type: "boolean",
|
|
39
|
+
short: "V",
|
|
40
|
+
default: false
|
|
41
|
+
},
|
|
42
|
+
help: {
|
|
43
|
+
type: "boolean",
|
|
44
|
+
short: "h",
|
|
45
|
+
default: false
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
allowPositionals: true
|
|
49
|
+
});
|
|
50
|
+
if (values.version) {
|
|
51
|
+
const require = createRequire(import.meta.url);
|
|
52
|
+
const { version } = require("../package.json");
|
|
53
|
+
console.log(`pdfxtract v${version}`);
|
|
54
|
+
process.exit(0);
|
|
55
|
+
}
|
|
56
|
+
if (values.help || positionals.length === 0) {
|
|
57
|
+
console.log(`
|
|
58
|
+
Usage: pdfxtract <directory> [options]
|
|
59
|
+
|
|
60
|
+
Arguments:
|
|
61
|
+
directory Directory containing PDF files to convert
|
|
62
|
+
|
|
63
|
+
Options:
|
|
64
|
+
-o, --output <dir> Output directory (preserves subdirectory structure)
|
|
65
|
+
--overwrite Overwrite existing TXT files
|
|
66
|
+
-v, --verbose Show detailed output
|
|
67
|
+
-V, --version Show version number
|
|
68
|
+
-h, --help Show this help message
|
|
69
|
+
|
|
70
|
+
Examples:
|
|
71
|
+
npx pdfxtract ./pdfs
|
|
72
|
+
npx pdfxtract ./pdfs -o ./output
|
|
73
|
+
npx pdfxtract ./pdfs -o ./output --overwrite -v
|
|
74
|
+
`.trim());
|
|
75
|
+
process.exit(values.help ? 0 : 1);
|
|
76
|
+
}
|
|
77
|
+
const inputDir = resolve(positionals[0]);
|
|
78
|
+
const outputDir = values.output ? resolve(values.output) : void 0;
|
|
79
|
+
try {
|
|
80
|
+
const s = await stat(inputDir);
|
|
81
|
+
if (!s.isDirectory()) {
|
|
82
|
+
console.error(`Error: "${inputDir}" is not a directory.`);
|
|
83
|
+
process.exit(1);
|
|
84
|
+
}
|
|
85
|
+
} catch {
|
|
86
|
+
console.error(`Error: "${inputDir}" does not exist.`);
|
|
87
|
+
process.exit(1);
|
|
88
|
+
}
|
|
89
|
+
const entries = await readdir(inputDir, { recursive: true });
|
|
90
|
+
const pdfFiles = entries.filter((entry) => extname(entry).toLowerCase() === ".pdf");
|
|
91
|
+
if (pdfFiles.length === 0) {
|
|
92
|
+
console.log("No PDF files found.");
|
|
93
|
+
process.exit(0);
|
|
94
|
+
}
|
|
95
|
+
console.log(`Found ${pdfFiles.length} PDF file(s).`);
|
|
96
|
+
let converted = 0;
|
|
97
|
+
let skipped = 0;
|
|
98
|
+
let failed = 0;
|
|
99
|
+
for (const relPath of pdfFiles) {
|
|
100
|
+
const pdfPath = join(inputDir, relPath);
|
|
101
|
+
const txtName = `${basename(relPath, extname(relPath))}.txt`;
|
|
102
|
+
let txtPath;
|
|
103
|
+
if (outputDir) {
|
|
104
|
+
const txtDir = join(outputDir, dirname(relPath));
|
|
105
|
+
await mkdir(txtDir, { recursive: true });
|
|
106
|
+
txtPath = join(txtDir, txtName);
|
|
107
|
+
} else txtPath = join(dirname(pdfPath), txtName);
|
|
108
|
+
if (!values.overwrite) try {
|
|
109
|
+
await stat(txtPath);
|
|
110
|
+
if (values.verbose) console.log(` Skip: ${relPath} (TXT already exists)`);
|
|
111
|
+
skipped++;
|
|
112
|
+
continue;
|
|
113
|
+
} catch {}
|
|
114
|
+
try {
|
|
115
|
+
await convertPdf(pdfPath, txtPath);
|
|
116
|
+
converted++;
|
|
117
|
+
if (values.verbose) console.log(` Done: ${relPath} -> ${relative(process.cwd(), txtPath)}`);
|
|
118
|
+
} catch (err) {
|
|
119
|
+
failed++;
|
|
120
|
+
console.error(` Fail: ${relPath} - ${err instanceof Error ? err.message : err}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
console.log(`\nComplete: ${converted} converted, ${skipped} skipped, ${failed} failed.`);
|
|
124
|
+
|
|
125
|
+
//#endregion
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pdfxtract",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Recursively convert PDF files to TXT in a directory",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"pdfxtract": "dist/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=20.0.0"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist/"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsdown",
|
|
17
|
+
"check": "biome check .",
|
|
18
|
+
"fix": "biome check --write .",
|
|
19
|
+
"knip": "knip",
|
|
20
|
+
"typecheck": "tsc",
|
|
21
|
+
"prepublishOnly": "npm run build"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"pdf",
|
|
25
|
+
"txt",
|
|
26
|
+
"text",
|
|
27
|
+
"converter",
|
|
28
|
+
"cli"
|
|
29
|
+
],
|
|
30
|
+
"author": "Naoki Yazawa",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/NaokiYazawa/pdfxtract.git"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/NaokiYazawa/pdfxtract#readme",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/NaokiYazawa/pdfxtract/issues"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"unpdf": "^0.12.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@biomejs/biome": "2.4.6",
|
|
45
|
+
"knip": "^5.86.0",
|
|
46
|
+
"tsdown": "^0.9.0",
|
|
47
|
+
"typescript": "^5.8.0"
|
|
48
|
+
}
|
|
49
|
+
}
|