MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / extractPDFPages

Function extractPDFPages

source code/utils/pdf.ts:181–302  ·  view source on GitHub ↗
(
  filePath: string,
  options?: { firstPage?: number; lastPage?: number },
)

Source from the content-addressed store, hash-verified

179 * @param options Optional page range (1-indexed, inclusive)
180 */
181export async function extractPDFPages(
182 filePath: string,
183 options?: { firstPage?: number; lastPage?: number },
184): Promise<PDFResult<PDFExtractPagesResult>> {
185 try {
186 const fs = getFsImplementation()
187 const stats = await fs.stat(filePath)
188 const originalSize = stats.size
189
190 if (originalSize === 0) {
191 return {
192 success: false,
193 error: { reason: 'empty', message: `PDF file is empty: ${filePath}` },
194 }
195 }
196
197 if (originalSize > PDF_MAX_EXTRACT_SIZE) {
198 return {
199 success: false,
200 error: {
201 reason: 'too_large',
202 message: `PDF file exceeds maximum allowed size for text extraction (${formatFileSize(PDF_MAX_EXTRACT_SIZE)}).`,
203 },
204 }
205 }
206
207 const available = await isPdftoppmAvailable()
208 if (!available) {
209 return {
210 success: false,
211 error: {
212 reason: 'unavailable',
213 message:
214 'pdftoppm is not installed. Install poppler-utils (e.g. `brew install poppler` or `apt-get install poppler-utils`) to enable PDF page rendering.',
215 },
216 }
217 }
218
219 const uuid = randomUUID()
220 const outputDir = join(getToolResultsDir(), `pdf-${uuid}`)
221 await mkdir(outputDir, { recursive: true })
222
223 // pdftoppm produces files like <prefix>-01.jpg, <prefix>-02.jpg, etc.
224 const prefix = join(outputDir, 'page')
225 const args = ['-jpeg', '-r', '100']
226 if (options?.firstPage) {
227 args.push('-f', String(options.firstPage))
228 }
229 if (options?.lastPage && options.lastPage !== Infinity) {
230 args.push('-l', String(options.lastPage))
231 }
232 args.push(filePath, prefix)
233 const { code, stderr } = await execFileNoThrow('pdftoppm', args, {
234 timeout: 120_000,
235 useCwd: false,
236 })
237
238 if (code !== 0) {

Callers 1

callInnerFunction · 0.85

Calls 8

getFsImplementationFunction · 0.85
formatFileSizeFunction · 0.85
isPdftoppmAvailableFunction · 0.85
getToolResultsDirFunction · 0.85
mkdirFunction · 0.85
execFileNoThrowFunction · 0.85
readdirFunction · 0.85
errorMessageFunction · 0.70

Tested by

no test coverage detected