openvibers/openvibe.finance serverpdf.js
pdf.js
| 1 | ; |
| 2 | /** |
| 3 | * A PDF's text, page by page, with no dependency: the small extractor OpenVibe.AI uses for files on a turn |
| 4 | * (OpenVibe.AI server/file-inputs.js pdfText/contentText, MIT, the same organisation), so a statement is read here and |
| 5 | * never leaves Finance. |
| 6 | * |
| 7 | * pdfText(buffer) → { pages: [text] } | { pages: [], reason } |
| 8 | * |
| 9 | * It reads uncompressed and FlateDecode content streams and the text the Tj, TJ, ' and " operators show; a new line at |
| 10 | * T*, Td, TD and ET. An encrypted PDF, a scanned one (images only) or one with a custom font encoding has no readable |
| 11 | * text, and says so. |
| 12 | */ |
| 13 | const zlib = require('node:zlib'); |
| 14 | |
| 15 | const MAX_STREAMS = 500; |
| 16 | const MAX_TEXT_CHARS = 400000; |
| 17 | |
| 18 | function pdfText(buffer) { |
| 19 | const raw = Buffer.isBuffer(buffer) ? buffer.toString('latin1') : String(buffer || ''); |
| 20 | if (!raw.startsWith('%PDF')) return { pages: [], reason: 'this is not a PDF' }; |
| 21 | if (/\/Encrypt\s/.test(raw)) return { pages: [], reason: 'the PDF is encrypted (password protected)' }; |
| 22 | const pages = []; |
| 23 | let total = 0; |
| 24 | let seen = 0; |
| 25 | const re = /stream\r?\n?([\s\S]*?)\r?\n?endstream/g; |
| 26 | let m; |
| 27 | while ((m = re.exec(raw)) && seen < MAX_STREAMS) { |
| 28 | seen++; |
| 29 | const before = raw.slice(Math.max(0, m.index - 400), m.index); |
| 30 | let data = Buffer.from(m[1], 'latin1'); |
| 31 | if (/\/FlateDecode\b/.test(before)) { |
| 32 | try { data = zlib.inflateSync(data, { maxOutputLength: 8 * 1024 * 1024 }); } catch { continue; } |
| 33 | } else if (/\/Filter\b/.test(before)) { |
| 34 | continue; // an image (DCT) or another filter, not text |
| 35 | } |
| 36 | const text = contentText(data.toString('latin1')); |
| 37 | if (text) { pages.push(text); total += text.length; } |
| 38 | if (total >= MAX_TEXT_CHARS) break; |
| 39 | } |
| 40 | if (!pages.length) return { pages: [], reason: 'the PDF has no readable text (it may be scanned pages, or use a custom font encoding)' }; |
| 41 | return { pages }; |
| 42 | } |
| 43 | |
| 44 | /** The text the Tj/TJ/'/" operators show in one content stream. */ |
| 45 | function contentText(stream) { |
| 46 | const parts = []; |
| 47 | let i = 0; |
| 48 | const n = stream.length; |
| 49 | const readString = () => { |
| 50 | let out = ''; |
| 51 | let depth = 1; |
| 52 | i++; |
| 53 | while (i < n && depth > 0) { |
| 54 | const c = stream[i++]; |
| 55 | if (c === '\\') { |
| 56 | const d = stream[i++]; |
| 57 | if (d === 'n') out += '\n'; |
| 58 | else if (d === 'r') out += '\r'; |
| 59 | else if (d === 't') out += '\t'; |
| 60 | else if (d === 'b' || d === 'f') out += ' '; |
| 61 | else if (d === '\n' || d === '\r') { if (d === '\r' && stream[i] === '\n') i++; } |
| 62 | else if (/[0-7]/.test(d)) { |
| 63 | let oct = d; |
| 64 | while (oct.length < 3 && /[0-7]/.test(stream[i])) oct += stream[i++]; |
| 65 | out += String.fromCharCode(parseInt(oct, 8)); |
| 66 | } else out += d; |
| 67 | } else if (c === '(') { depth++; out += c; } |
| 68 | else if (c === ')') { depth--; if (depth) out += c; } |
| 69 | else out += c; |
| 70 | } |
| 71 | return out; |
| 72 | }; |
| 73 | while (i < n) { |
| 74 | const c = stream[i]; |
| 75 | if (c === '(') { |
| 76 | const s = readString(); |
| 77 | let j = i; |
| 78 | while (j < n && /\s/.test(stream[j])) j++; |
| 79 | if (stream.startsWith('Tj', j)) { parts.push(s); i = j + 2; continue; } |
| 80 | if (stream.startsWith("'", j) || stream.startsWith('"', j)) { parts.push(`\n${s}`); i = j + 1; continue; } |
| 81 | continue; |
| 82 | } |
| 83 | if (c === '[') { |
| 84 | i++; |
| 85 | const arr = []; |
| 86 | while (i < n && stream[i] !== ']') { |
| 87 | if (stream[i] === '(') arr.push(readString()); |
| 88 | else i++; |
| 89 | } |
| 90 | i++; |
| 91 | const tm = /^\s*TJ/.exec(stream.slice(i, i + 8)); |
| 92 | if (tm) { parts.push(arr.join('')); i += tm[0].length; } |
| 93 | continue; |
| 94 | } |
| 95 | if (stream.startsWith('T*', i) || stream.startsWith('Td', i) || stream.startsWith('TD', i) || stream.startsWith('ET', i)) { |
| 96 | parts.push('\n'); i += 2; continue; |
| 97 | } |
| 98 | i++; |
| 99 | } |
| 100 | return parts.join('').replace(/[ \t]+\n/g, '\n').replace(/\n{3,}/g, '\n\n').trim(); |
| 101 | } |
| 102 | |
| 103 | module.exports = { pdfText }; |
| 104 |