MCPcopy Create free account
hub / github.com/dwgx/WindsurfAPI / tryExtractPdf

Function tryExtractPdf

src/pdf.js:145–165  ·  view source on GitHub ↗
(base64Data)

Source from the content-addressed store, hash-verified

143 * @returns {{ text: string, pageCount: number } | null}
144 */
145export function tryExtractPdf(base64Data) {
146 try {
147 const buf = Buffer.from(base64Data, 'base64');
148 if (buf.length < 5 || buf.subarray(0, 5).toString() !== '%PDF-') return null;
149
150 const text = extractPdfText(buf);
151 if (!text.trim()) {
152 log.warn('PDF has no extractable text layer (scanned/image-only PDF)');
153 return { text: '', pageCount: 0 };
154 }
155
156 const pageCount = (buf.toString('latin1').match(/\/Type\s*\/Page\b/g) || []).length;
157 return { text, pageCount };
158 } catch (e) {
159 log.warn(`PDF extraction failed: ${e.message}`);
160 if (/exceeds safety limit|maxOutputLength|too large|Buffer larger/i.test(e.message) || e.code === 'ERR_BUFFER_TOO_LARGE') {
161 return { text: 'PDF 内容无法提取', pageCount: 0 };
162 }
163 return null;
164 }
165}

Callers 2

pdf.test.jsFile · 0.90
extractImagesFunction · 0.90

Calls 1

extractPdfTextFunction · 0.85

Tested by

no test coverage detected