import React, { useState, useCallback, useEffect } from 'react'; import { Upload, Download, Copy, Chrome, FileText, AlertCircle, CheckCircle } from 'lucide-react'; const PDFToTextApp = () => { const [extractedText, setExtractedText] = useState(''); const [isProcessing, setIsProcessing] = useState(false); const [fileName, setFileName] = useState(''); const [error, setError] = useState(''); const [copySuccess, setCopySuccess] = useState(false); const [pdfLibLoaded, setPdfLibLoaded] = useState(false); // Load PDF.js library useEffect(() => { const loadPDFJS = () => { if (window.pdfjsLib) { setPdfLibLoaded(true); return; } const script = document.createElement('script'); script.src = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js'; script.onload = () => { if (window.pdfjsLib) { window.pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js'; setPdfLibLoaded(true); } }; script.onerror = () => { setError('Failed to load PDF processing library. Please refresh the page.'); }; document.head.appendChild(script); }; loadPDFJS(); }, []); const handleFileUpload = useCallback(async (file) => { if (!file || file.type !== 'application/pdf') { setError('Please select a valid PDF file'); return; } if (file.size > 10 * 1024 * 1024) { setError('File size must be less than 10MB'); return; } if (!pdfLibLoaded) { setError('PDF processing library is still loading. Please wait a moment and try again.'); return; } setError(''); setIsProcessing(true); setFileName(file.name); try { const arrayBuffer = await file.arrayBuffer(); const typedArray = new Uint8Array(arrayBuffer); const loadingTask = window.pdfjsLib.getDocument({ data: typedArray, }); const pdf = await loadingTask.promise; let fullText = ''; for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) { const page = await pdf.getPage(pageNum); const textContent = await page.getTextContent(); const pageText = textContent.items .map(item => item.str) .join(' ') .replace(/\s+/g, ' ') .trim(); if (pageText) { fullText += `Page ${pageNum}:\n${pageText}\n\n`; } } if (!fullText.trim()) { setError('No text found in this PDF. It might be a scanned document or contain only images.'); } else { setExtractedText(fullText.trim()); } } catch (err) { console.error('PDF extraction error:', err); setError('Failed to extract text from PDF. The file might be encrypted, corrupted, or contain only images.'); } finally { setIsProcessing(false); } }, [pdfLibLoaded]); const handleDrop = useCallback((e) => { e.preventDefault(); if (!pdfLibLoaded) return; const files = e.dataTransfer.files; if (files.length > 0) { handleFileUpload(files[0]); } }, [handleFileUpload, pdfLibLoaded]); const handleDragOver = useCallback((e) => { e.preventDefault(); }, []); const handleFileSelect = useCallback((e) => { const files = e.target.files; if (files.length > 0) { handleFileUpload(files[0]); } }, [handleFileUpload]); const downloadText = useCallback(() => { const blob = new Blob([extractedText], { type: 'text/plain' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = fileName.replace('.pdf', '.txt'); document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }, [extractedText, fileName]); const copyToClipboard = useCallback(async () => { try { await navigator.clipboard.writeText(extractedText); setCopySuccess(true); setTimeout(() => setCopySuccess(false), 2000); } catch (err) { console.error('Failed to copy text'); } }, [extractedText]); return (
{/* Chrome Extension Promotion Banner */}

Get Our Chrome Extension!

Convert PDFs directly in your browser - even faster and more convenient!

Add to Chrome
{/* Main Content */}
{/* Header */}

PDF to Text Converter

Extract text from your PDF files instantly. Fast, secure, and completely free. All processing happens in your browser - your files never leave your device.

{/* Upload Section */}

Drag and drop your PDF here

or click to browse files

Maximum file size: 10MB

{error && (
{error}
)} {isProcessing && (

Extracting text from PDF...

)}
{/* Results Section */} {extractedText && (

Extracted Text

                {extractedText}
              
)} {/* Extension Promotion Section */}

Why Install Our Chrome Extension?

Right-Click Conversion

Convert PDFs directly from any webpage with a simple right-click

Always Available

No need to open a separate tab - works wherever you are browsing

Faster Processing

Optimized for speed with instant access to your conversion history

Install Chrome Extension - It's Free!
{/* Footer */}
); }; export default PDFToTextApp;