### Static Site Integration Example Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md An HTML example for static site deployment, demonstrating how to use Extract2MD via its UMD bundle loaded from a CDN. It includes a file input, a button to trigger conversion, and an area to display the output. ```html
``` -------------------------------- ### Test Extract2MD Migration Source: https://github.com/hashangit/extract2md/blob/main/MIGRATION.md Provides an example of how to use the Extract2MD testing utilities to validate migration. It demonstrates running basic tests and full tests with a PDF file to ensure the new API functions as expected. These tests are crucial for verifying data integrity and functionality post-migration. ```javascript import { Extract2MDTests } from 'extract2md/test/scenarios.test.js'; const tests = new Extract2MDTests(); await tests.runBasicTests(); // With a PDF file await tests.runFullTests(pdfFile); ``` -------------------------------- ### Using the Legacy API (Backwards Compatibility) Source: https://github.com/hashangit/extract2md/blob/main/MIGRATION.md Provides an example of how to continue using the legacy Extract2MD API for backwards compatibility in version 1.0.6. It notes that this API is deprecated and scheduled for removal. ```javascript import { LegacyExtract2MDConverter } from 'extract2md'; const converter = new LegacyExtract2MDConverter(); // Use old API as before ``` -------------------------------- ### NPM Installation Source: https://github.com/hashangit/extract2md/blob/main/README.md Provides the command to install the extract2md library using npm, the Node Package Manager. ```bash npm install extract2md ``` -------------------------------- ### Generate Custom System Prompts Source: https://github.com/hashangit/extract2md/blob/main/README.md Shows how to generate custom system prompts for different extraction scenarios using the SystemPrompts utility from the 'extract2md' library. These prompts can guide the AI's behavior during text generation. ```javascript // For scenarios 3 & 4 (single extraction) const singlePrompt = SystemPrompts.getSingleExtractionPrompt( "Additional instruction: Preserve all technical terms." ); // For scenario 5 (combined extraction) const combinedPrompt = SystemPrompts.getCombinedExtractionPrompt( "Focus on creating comprehensive documentation." ); ``` -------------------------------- ### LLM Configuration Migration Source: https://github.com/hashangit/extract2md/blob/main/MIGRATION.md Explains the transition of LLM configuration parameters from the legacy Extract2MD API to the new v1.0.6 API. LLM settings are now grouped under a `webllm` object. ```javascript { llmModel: 'Llama-3.2-1B-Instruct-q4f16_1-MLC', llmTemperature: 0.7, llmMaxTokens: 4000 } ``` ```javascript { webllm: { modelId: 'Llama-3.2-1B-Instruct-q4f16_1-MLC', temperature: 0.7, maxTokens: 4000, streamingEnabled: false } } ``` -------------------------------- ### Quick Start: PDF to Markdown Conversion Scenarios in JavaScript Source: https://github.com/hashangit/extract2md/blob/main/README.md Demonstrates the five different conversion scenarios offered by Extract2MD. Each scenario utilizes a specific method to convert a PDF file to Markdown, ranging from quick text extraction to advanced OCR and LLM enhancements. ```javascript import Extract2MDConverter from 'extract2md'; // Scenario 1: Quick conversion only const markdown1 = await Extract2MDConverter.quickConvertOnly(pdfFile); // Scenario 2: High accuracy OCR conversion only const markdown2 = await Extract2MDConverter.highAccuracyConvertOnly(pdfFile); // Scenario 3: Quick conversion + LLM enhancement const markdown3 = await Extract2MDConverter.quickConvertWithLLM(pdfFile); // Scenario 4: High accuracy conversion + LLM enhancement const markdown4 = await Extract2MDConverter.highAccuracyConvertWithLLM(pdfFile); // Scenario 5: Combined extraction + LLM enhancement (most comprehensive) const markdown5 = await Extract2MDConverter.combinedConvertWithLLM(pdfFile); ``` -------------------------------- ### Build Extract2MD Package Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md Commands to build the Extract2MD package, including installing dependencies, running the build process, executing tests, and preparing for publishing. Requires Node.js 14+ and npm 7+. ```bash # Install dependencies npm install # Build the UMD bundle npm run build # Run tests npm test # Prepare for publishing npm run prepublishOnly ``` -------------------------------- ### TypeScript Configuration and Usage Source: https://github.com/hashangit/extract2md/blob/main/README.md Demonstrates how to use the 'extract2md' library with TypeScript, including importing types and configuring options like the WebLLM model and progress callbacks. It shows a complete example of converting a PDF file using `combinedConvertWithLLM`. ```typescript import Extract2MDConverter, { Extract2MDConfig, ProgressReport, CustomModelConfig } from 'extract2md'; const config: Extract2MDConfig = { webllm: { model: "Qwen3-0.6B-q4f16_1-MLC", options: { temperature: 0.7, maxTokens: 4096 } }, progressCallback: (progress: ProgressReport) => { console.log(progress.stage, progress.message); } }; const result: string = await Extract2MDConverter.combinedConvertWithLLM(pdfFile, config); ``` -------------------------------- ### Webpack Configuration for Web App Deployment Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md Example Webpack configuration for deploying a web application that uses Extract2MD. It includes fallback configurations for Node.js modules and copies necessary worker files to the public directory. ```javascript // Webpack configuration example module.exports = { resolve: { fallback: { "fs": false, "path": false } }, // Copy worker files to your public directory plugins: [ new CopyWebpackPlugin({ patterns: [ { from: 'node_modules/extract2md/dist/pdf.worker.min.mjs', to: 'public/' }, { from: 'node_modules/extract2md/dist/assets/tesseract-worker.min.js', to: 'public/' }, { from: 'node_modules/extract2md/dist/assets/tesseract-core.wasm.js', to: 'public/' } ] }) ] }; ``` -------------------------------- ### Combined Extraction Methods with LLM (New Feature) Source: https://github.com/hashangit/extract2md/blob/main/MIGRATION.md Introduces `combinedConvertWithLLM`, a new method in Extract2MD v1.0.6 that was not present in the legacy API. It allows for combined extraction strategies with LLM rewriting. ```javascript const result = await Extract2MDConverter.combinedConvertWithLLM(pdfFile, { ocr: { language: 'eng', oem: 1, psm: 6 }, webllm: { modelId: 'Llama-3.2-1B-Instruct-q4f16_1-MLC', temperature: 0.7, maxTokens: 4000, streamingEnabled: false } }); ``` -------------------------------- ### PDF with OCR and LLM Rewrite - Quick vs. High Accuracy Source: https://github.com/hashangit/extract2md/blob/main/MIGRATION.md Illustrates PDF to Markdown conversion incorporating both OCR and LLM rewriting. The new API provides `quickConvertWithLLM` and `highAccuracyConvertWithLLM`, with detailed configurations for OCR and WebLLM. ```javascript const converter = new Extract2MD(); const result = await converter.convertPDFToMarkdown(pdfFile, { useOCR: true, useLLM: true, ocrLanguage: 'eng', llmModel: 'Llama-3.2-1B-Instruct-q4f16_1-MLC', llmTemperature: 0.7 }); ``` ```javascript const result = await Extract2MDConverter.quickConvertWithLLM(pdfFile, { ocr: { language: 'eng', oem: 1, psm: 6 }, webllm: { modelId: 'Llama-3.2-1B-Instruct-q4f16_1-MLC', temperature: 0.7, maxTokens: 4000, streamingEnabled: false } }); ``` ```javascript const result = await Extract2MDConverter.highAccuracyConvertWithLLM(pdfFile, { ocr: { language: 'eng', oem: 1, psm: 8 }, webllm: { modelId: 'Llama-3.2-1B-Instruct-q4f16_1-MLC', temperature: 0.7, maxTokens: 4000, streamingEnabled: false } }); ``` -------------------------------- ### Worker Files Configuration Source: https://github.com/hashangit/extract2md/blob/main/README.md Explains the necessity of worker files for PDF.js and Tesseract.js and provides an example of how to configure their paths within the extract2md library, allowing for adjustments based on deployment environment. ```javascript // Default worker paths (adjust for your deployment) const config = { pdfJsWorkerSrc: "/pdf.worker.min.mjs", tesseract: { workerPath: "/tesseract-worker.min.js", corePath: "/tesseract-core.wasm.js" } }; ``` -------------------------------- ### OCR Configuration Migration Source: https://github.com/hashangit/extract2md/blob/main/MIGRATION.md Details the changes in OCR configuration between the legacy Extract2MD API and the new v1.0.6 API. The new API consolidates OCR settings under a nested `ocr` object. ```javascript { ocrLanguage: 'eng', ocrPSM: 6, ocrOEM: 1 } ``` ```javascript { ocr: { language: 'eng', psm: 6, oem: 1 } } ``` -------------------------------- ### Error Handling and Progress Tracking Source: https://github.com/hashangit/extract2md/blob/main/README.md Provides an example of configuring progress callbacks to monitor the conversion process and handle errors. It logs different stages of the conversion, including model loading, OCR, and AI enhancement, and catches potential exceptions. ```javascript const config = { progressCallback: (progress) => { switch (progress.stage) { case 'scenario_5_start': console.log('Starting combined conversion...'); break; case 'webllm_load_progress': console.log(`Loading model: ${progress.progress}%`); break; case 'ocr_page_process': console.log(`OCR: ${progress.currentPage}/${progress.totalPages}`); break; case 'webllm_generate_start': console.log('AI enhancement in progress...'); break; case 'scenario_5_complete': console.log('Conversion completed!'); break; default: console.log(`${progress.stage}: ${progress.message}`); } if (progress.error) { console.error('Error:', progress.error); } } }; try { const result = await Extract2MDConverter.combinedConvertWithLLM(pdfFile, config); console.log('Success:', result); } catch (error) { console.error('Conversion failed:', error.message); } ``` -------------------------------- ### Configure Extract2MD with JSON Source: https://github.com/hashangit/extract2md/blob/main/MIGRATION.md Demonstrates how to use an external JSON file for configuring OCR and WebLLM settings in Extract2MD. This allows for flexible and maintainable configuration without modifying code directly. The configuration is loaded and applied during the conversion process. ```json // config.json { "ocr": { "language": "eng", "oem": 1, "psm": 6 }, "webllm": { "modelId": "Llama-3.2-1B-Instruct-q4f16_1-MLC", "temperature": 0.7, "maxTokens": 4000, "streamingEnabled": false } } ``` ```javascript // In your code const result = await Extract2MDConverter.quickConvertWithLLM(pdfFile, 'config.json'); ``` -------------------------------- ### Content Security Policy (CSP) Configuration Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md Provides an example of Content Security Policy (CSP) headers to enhance security when deploying the Extract2MD library. It specifies allowed sources for scripts, workers, and connections, mitigating risks like cross-site scripting (XSS). ```http Content-Security-Policy: script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:; connect-src 'self' https://huggingface.co; ``` -------------------------------- ### Basic PDF Text Extraction (No OCR, No LLM) Source: https://github.com/hashangit/extract2md/blob/main/MIGRATION.md Demonstrates basic PDF to Markdown conversion without OCR or LLM processing. The legacy API used a flag, while the new API uses `quickConvertOnly` with optional OCR configuration. ```javascript const converter = new Extract2MD(); const result = await converter.convertPDFToMarkdown(pdfFile, { useOCR: false, useLLM: false }); ``` ```javascript const result = await Extract2MDConverter.quickConvertOnly(pdfFile, { // OCR config is optional - will use PDF text extraction only }); ``` -------------------------------- ### PDF with OCR (No LLM) - Quick vs. High Accuracy Source: https://github.com/hashangit/extract2md/blob/main/MIGRATION.md Shows how to perform PDF to Markdown conversion with OCR but without LLM rewriting. The new API offers `quickConvertOnly` and `highAccuracyConvertOnly` methods, allowing different OCR configurations. ```javascript const converter = new Extract2MD(); const result = await converter.convertPDFToMarkdown(pdfFile, { useOCR: true, useLLM: false, ocrLanguage: 'eng', ocrPSM: 6 }); ``` ```javascript const result = await Extract2MDConverter.quickConvertOnly(pdfFile, { ocr: { language: 'eng', oem: 1, psm: 6 } }); ``` ```javascript const result = await Extract2MDConverter.highAccuracyConvertOnly(pdfFile, { ocr: { language: 'eng', oem: 1, psm: 8 } }); ``` -------------------------------- ### Convert PDF to Markdown (Legacy vs. New API) Source: https://github.com/hashangit/extract2md/blob/main/MIGRATION.md Compares the legacy JavaScript code for converting a PDF to Markdown with the new API in version 1.0.6. The new API uses dedicated converter classes for different scenarios. ```javascript import Extract2MD from 'extract2md'; const converter = new Extract2MD(); const result = await converter.convertPDFToMarkdown(pdfFile, { useOCR: true, useLLM: false, ocrLanguage: 'eng' }); ``` ```javascript import { Extract2MDConverter } from 'extract2md'; const result = await Extract2MDConverter.quickConvertOnly(pdfFile, { ocr: { language: 'eng', oem: 1, psm: 6 } }); ``` -------------------------------- ### Configure Extract2MD Demo Settings (JavaScript) Source: https://github.com/hashangit/extract2md/blob/main/examples/demo.html Sets up the global configuration for the Extract2MD demo, including paths for worker files, LLM model details, system prompts, and post-processing rules. This configuration is essential for the library to function correctly, especially when deploying to a server. ```javascript window.demoConfig = { pdfJsWorkerSrc: "/dist/pdf.worker.min.mjs", tesseract: { oem: 1, psm: 8, workerPath: "/dist/assets/tesseract-worker.min.js", corePath: "/dist/assets/tesseract-core.wasm.js", langPath: "/dist/assets/lang-data/" }, llm: { model: "Qwen3-0.6B-q4f16_1-MLC", options: { temperature: 0.7, maxTokens: 4096 } }, systemPrompts: { singleExtraction: "Focus on preserving technical accuracy and code examples exactly as they appear in the original document.", combinedExtraction: "Create comprehensive documentation by intelligently combining the best elements from both quick extraction and OCR methods. Focus on accuracy and completeness." }, processing: { splitPascalCase: false, pdfRenderScale: 2.5, postProcessRules: [ { find: /\bAPI\b/g, replace: "API" }, { find: /\bJSON\b/g, replace: "JSON" }, { find: /\bHTML\b/g, replace: "HTML" }, { find: /\bCSS\b/g, replace: "CSS" }, { find: /\bJS\b/g, replace: "JavaScript" } ] }, progressCallback: (progress) => { updateProgress(progress); } }; console.log('Extract2MD Demo loaded successfully!'); ``` -------------------------------- ### Handle PDF Conversion Scenarios (JavaScript) Source: https://github.com/hashangit/extract2md/blob/main/examples/demo.html Implements event listeners for scenario selection cards, allowing users to choose different PDF conversion strategies. It updates the UI to reflect the selected scenario and stores the choice in a variable for later use. ```javascript let selectedScenario = 5; document.querySelectorAll('.scenario-card').forEach(card => { card.addEventListener('click', () => { document.querySelectorAll('.scenario-card').forEach(c => c.classList.remove('active')); card.classList.add('active'); selectedScenario = parseInt(card.dataset.scenario); document.getElementById('selectedScenario').textContent = card.querySelector('.scenario-title').textContent; }); }); ``` -------------------------------- ### Performance Monitoring with JavaScript Performance API Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md Shows how to measure the execution time of the `quickConvertOnly` function using the `performance.now()` API. This allows for performance monitoring and identifying potential bottlenecks in the conversion process. ```javascript const startTime = performance.now(); const result = await Extract2MDConverter.quickConvertOnly(file, config); const duration = performance.now() - startTime; console.log(`Conversion took ${duration}ms`); ``` -------------------------------- ### Load Configuration from JSON Source: https://github.com/hashangit/extract2md/blob/main/README.md Illustrates how to load configuration settings from a JSON string using `ConfigValidator.fromJSON` and then use this configuration with the `Extract2MDConverter.quickConvertWithLLM` method for PDF conversion. ```javascript import { ConfigValidator } from 'extract2md'; // Load from JSON string const config = ConfigValidator.fromJSON(configJsonString); // Use with any scenario const result = await Extract2MDConverter.quickConvertWithLLM(pdfFile, config); ``` -------------------------------- ### Error Handling and Logging in JavaScript Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md Demonstrates a robust error handling pattern using a try-catch block to gracefully manage potential errors during the `quickConvertOnly` operation. It includes logging detailed error information for monitoring and debugging purposes. ```javascript try { const result = await Extract2MDConverter.quickConvertOnly(file, config); } catch (error) { // Log error details for monitoring console.error('Extract2MD Error:', { type: error.name, message: error.message, scenario: 'quickConvertOnly', timestamp: new Date().toISOString() }); } ``` -------------------------------- ### Initialize and Use WebLLM Engine Source: https://github.com/hashangit/extract2md/blob/main/README.md Demonstrates how to initialize the WebLLM engine with validated configuration, generate text based on a prompt, and parse the output into clean Markdown. Requires 'extract2md' library. ```javascript import { WebLLMEngine, OutputParser, SystemPrompts, ConfigValidator } from 'extract2md'; // Validate configuration const validatedConfig = ConfigValidator.validate(userConfig); // Initialize WebLLM engine const engine = new WebLLMEngine(validatedConfig); await engine.initialize(); // Generate text const result = await engine.generate("Your prompt here"); // Parse output const parser = new OutputParser(); const cleanMarkdown = parser.parse(result); ``` -------------------------------- ### Code Splitting for Specific Scenarios with JavaScript Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md Illustrates code splitting using Webpack to separate specific functionalities, such as `quickConvertOnly`, into different chunks. This allows for more granular loading of code based on user interaction or application state. ```javascript const quickConvert = () => import('extract2md').then(m => m.Extract2MDConverter.quickConvertOnly); ``` -------------------------------- ### Perform PDF to Markdown Conversion (JavaScript) Source: https://github.com/hashangit/extract2md/blob/main/examples/demo.html The main asynchronous function to initiate PDF conversion using the Extract2MD library. It handles file input, selects the conversion method based on the user's scenario, displays results, and manages the UI state during conversion. ```javascript async function convertPDF() { if (isConverting) return; const fileInput = document.getElementById('pdfInput'); const pdfFile = fileInput.files[0]; if (!pdfFile) { showError('Please select a PDF file first.'); return; } isConverting = true; document.getElementById('convertBtn').disabled = true; document.getElementById('convertBtn').textContent = 'Converting...'; document.getElementById('outputDiv').classList.remove('active'); try { let result; switch (selectedScenario) { case 1: result = await Extract2MD.Extract2MDConverter.quickConvertOnly(pdfFile, window.demoConfig); break; case 2: result = await Extract2MD.Extract2MDConverter.highAccuracyConvertOnly(pdfFile, window.demoConfig); break; case 3: result = await Extract2MD.Extract2MDConverter.quickConvertWithLLM(pdfFile, window.demoConfig); break; case 4: result = await Extract2MD.Extract2MDConverter.highAccuracyConvertWithLLM(pdfFile, window.demoConfig); break; case 5: result = await Extract2MD.Extract2MDConverter.combinedConvertWithLLM(pdfFile, window.demoConfig); break; default: throw new Error('Invalid scenario selected'); } showResult(result); } catch (error) { console.error('Conversion failed:', error); showError('Conversion failed: ' + error.message); } finally { isConverting = false; document.getElementById('convertBtn').disabled = false; document.getElementById('convertBtn').textContent = 'Convert PDF'; document.getElementById('progressDiv').classList.remove('active'); } } ``` -------------------------------- ### Expose Functions Globally (JavaScript) Source: https://github.com/hashangit/extract2md/blob/main/examples/demo.html This code snippet makes the 'convertPDF' and 'copyToClipboard' functions globally accessible by assigning them to the window object. This allows these functions to be called from HTML or other global scripts. ```javascript // Make functions globally available window.convertPDF = convertPDF; window.copyToClipboard = copyToClipboard; ``` -------------------------------- ### Display Conversion Results (JavaScript) Source: https://github.com/hashangit/extract2md/blob/main/examples/demo.html Handles the display of the generated Markdown content after a successful conversion. It updates a designated output area with the Markdown text and makes the output section visible to the user. ```javascript function showResult(markdown) { lastResult = markdown; document.getElementById('outputContent').textContent = markdown; document.getElementById('outputDiv').classList.add('active'); } ``` -------------------------------- ### Configuration Options for Extract2MD in JavaScript Source: https://github.com/hashangit/extract2md/blob/main/README.md Illustrates how to configure the Extract2MD converter. This includes settings for PDF.js, Tesseract OCR, WebLLM models, custom prompts, processing options, and progress callbacks. ```javascript const config = { // PDF.js Worker pdfJsWorkerSrc: "../pdf.worker.min.mjs", // Tesseract OCR Settings tesseract: { workerPath: "./tesseract-worker.min.js", corePath: "./tesseract-core.wasm.js", langPath: "./lang-data/", language: "eng", options: {} }, // LLM Configuration webllm: { model: "Qwen3-0.6B-q4f16_1-MLC", // Optional: Custom model customModel: { model: "https://huggingface.co/mlc-ai/your-model/resolve/main/", model_id: "YourModel-ID", model_lib: "https://example.com/your-model.wasm", required_features: ["shader-f16"], overrides: { conv_template: "qwen" } }, options: { temperature: 0.7, maxTokens: 4096 } }, // System Prompt Customizations systemPrompts: { singleExtraction: "Focus on preserving code examples exactly.", combinedExtraction: "Pay attention to tables and diagrams from OCR." }, // Processing Options processing: { splitPascalCase: false, pdfRenderScale: 2.5, postProcessRules: [ { find: /\bAPI\b/g, replace: "API" } ] }, // Progress Tracking progressCallback: (progress) => { console.log(`${progress.stage}: ${progress.message}`); if (progress.currentPage) { console.log(`Page ${progress.currentPage}/${progress.totalPages}`); } } }; // Use configuration const markdown = await Extract2MDConverter.combinedConvertWithLLM(pdfFile, config); ``` -------------------------------- ### Extract2MDConverter.quickConvertWithLLM Source: https://context7.com/hashangit/extract2md/llms.txt Combines fast PDF.js extraction with WebLLM AI enhancement for improved clarity, error correction, and Markdown formatting. Requires WebGPU support. ```APIDOC ## Extract2MDConverter.quickConvertWithLLM ### Description Combines fast PDF.js extraction with WebLLM AI enhancement. After extracting text, the content is processed by a local LLM to improve clarity, fix errors, and enhance Markdown formatting. Requires WebGPU support in the browser. ### Method `Extract2MDConverter.quickConvertWithLLM(pdfFile, config)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **pdfFile** (File) - The PDF file to convert. - **config** (Object) - Configuration object for the conversion process. - **webllm** (Object) - WebLLM configuration. - **model** (string) - The LLM model to use (e.g., 'Qwen3-0.6B-q4f16_1-MLC'). - **options** (Object) - LLM generation options (e.g., `temperature`, `maxTokens`). - **systemPrompts** (Object) - Custom system prompts for the LLM. - **singleExtraction** (string) - Prompt for single extraction enhancement. - **progressCallback** (Function) - Callback function to track conversion progress. ### Request Example ```javascript import { Extract2MDConverter } from 'extract2md'; const pdfFile = document.getElementById('pdfInput').files[0]; const config = { webllm: { model: 'Qwen3-0.6B-q4f16_1-MLC', options: { temperature: 0.7, maxTokens: 4096 } }, systemPrompts: { singleExtraction: 'Focus on preserving code examples and technical terms exactly.' }, progressCallback: (progress) => { switch (progress.stage) { case 'webllm_load_progress': console.log(`Loading LLM: ${Math.round(progress.progress * 100)}%`); break; case 'webllm_generate_start': console.log('AI enhancement in progress...'); break; case 'scenario_3_complete': console.log('Conversion with LLM enhancement completed!'); break; } } }; try { const enhancedMarkdown = await Extract2MDConverter.quickConvertWithLLM(pdfFile, config); document.getElementById('output').innerHTML = `${enhancedMarkdown}`;
} catch (error) {
if (error.message.includes('WebGPU')) {
alert('WebGPU not supported. Please use Chrome 113+ or Edge 113+.');
} else {
console.error('Conversion failed:', error.message);
}
}
```
### Response
#### Success Response (200)
- **enhancedMarkdown** (string) - The converted and AI-enhanced Markdown content.
```
--------------------------------
### Handle Extract2MD Errors
Source: https://github.com/hashangit/extract2md/blob/main/MIGRATION.md
Illustrates robust error handling for the new Extract2MD API. It shows how to catch specific error types like ConfigurationError, OCRError, and WebLLMError, allowing for targeted user feedback and debugging. General errors are also caught for comprehensive error management.
```javascript
try {
const result = await Extract2MDConverter.quickConvertOnly(pdfFile, config);
} catch (error) {
if (error.name === 'ConfigurationError') {
console.error('Configuration issue:', error.message);
} else if (error.name === 'OCRError') {
console.error('OCR processing failed:', error.message);
} else if (error.name === 'WebLLMError') {
console.error('LLM processing failed:', error.message);
} else {
console.error('General error:', error.message);
}
}
```
--------------------------------
### Node.js Server Deployment Configuration
Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md
Demonstrates server-side usage of Extract2MD, focusing on configuration validation. Note that full functionality might be limited due to browser dependencies.
```javascript
// Server-side usage (configuration validation only)
import { ConfigValidator } from 'extract2md/src/utils/ConfigValidator.js';
const validator = new ConfigValidator();
const isValid = validator.validate(config);
```
--------------------------------
### Migrate from Legacy API
Source: https://github.com/hashangit/extract2md/blob/main/README.md
Compares the usage of the legacy `LegacyExtract2MDConverter` API with the recommended new API for PDF conversion tasks like quick conversion, high-accuracy conversion, and LLM rewriting.
```javascript
import { LegacyExtract2MDConverter } from 'extract2md';
// Old way
const converter = new LegacyExtract2MDConverter(options);
const quick = await converter.quickConvert(pdfFile);
const ocr = await converter.highAccuracyConvert(pdfFile);
const enhanced = await converter.llmRewrite(text);
// New way (recommended)
const quick = await Extract2MDConverter.quickConvertOnly(pdfFile, config);
const ocr = await Extract2MDConverter.highAccuracyConvertOnly(pdfFile, config);
const enhanced = await Extract2MDConverter.quickConvertWithLLM(pdfFile, config);
```
--------------------------------
### Lazy Loading Module with JavaScript
Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md
Demonstrates how to implement lazy loading for the Extract2MDConverter module, ensuring it's only loaded when explicitly needed. This improves initial load times by deferring the import of the module.
```javascript
const { Extract2MDConverter } = await import('extract2md');
```
--------------------------------
### Comprehensive PDF to Markdown Conversion with LLM Merging (JavaScript)
Source: https://context7.com/hashangit/extract2md/llms.txt
Combines PDF.js and Tesseract.js extraction in parallel, then uses an LLM to intelligently merge and enhance the results for the highest quality output. This method requires specifying paths for Tesseract.js workers and language data. It takes a PDF file and a configuration object, returning the comprehensive Markdown.
```javascript
import { Extract2MDConverter } from 'extract2md';
const pdfFile = document.getElementById('pdfInput').files[0];
const config = {
pdfJsWorkerSrc: '/pdf.worker.min.mjs',
tesseract: {
workerPath: '/tesseract-worker.min.js',
corePath: '/tesseract-core.wasm.js',
langPath: '/lang-data/',
language: 'eng'
},
webllm: {
model: 'Qwen3-0.6B-q4f16_1-MLC',
options: {
temperature: 0.7,
maxTokens: 4096
}
},
systemPrompts: {
combinedExtraction: 'Create comprehensive documentation by merging both sources. Preserve all technical details and code examples.'
},
processing: {
splitPascalCase: false,
pdfRenderScale: 2.5,
postProcessRules: [
{ find: /\bAPI\b/g, replace: 'API' }
]
},
progressCallback: (progress) => {
const statusEl = document.getElementById('status');
statusEl.textContent = `${progress.stage}: ${progress.message}`;
if (progress.stage === 'scenario_5_complete') {
statusEl.textContent = 'Conversion completed successfully!';
}
}
};
try {
const comprehensiveMarkdown = await Extract2MDConverter.combinedConvertWithLLM(pdfFile, config);
document.getElementById('output').innerHTML = marked.parse(comprehensiveMarkdown);
} catch (error) {
console.error('Combined conversion failed:', error);
}
```
--------------------------------
### Update Conversion Progress Display (JavaScript)
Source: https://github.com/hashangit/extract2md/blob/main/examples/demo.html
A function to visually update the user on the PDF conversion progress. It displays messages, page numbers, loading percentages, and token usage, making the conversion process transparent. Errors encountered during conversion are also displayed.
```javascript
function updateProgress(progress) {
const progressDiv = document.getElementById('progressDiv');
const messageEl = document.getElementById('progressMessage');
const detailsEl = document.getElementById('progressDetails');
progressDiv.classList.add('active');
messageEl.textContent = progress.message;
let details = [];
if (progress.currentPage && progress.totalPages) {
const pageProgress = Math.round((progress.currentPage / progress.totalPages) * 100);
details.push(`Page Progress: ${pageProgress}% (${progress.currentPage}/${progress.totalPages})`);
}
if (progress.progress !== undefined) {
const loadProgress = Math.round(progress.progress * 100);
details.push(`Loading: ${loadProgress}%`);
}
if (progress.usage) {
details.push(`Tokens: ${progress.usage.total_tokens || 'N/A'}`);
}
detailsEl.textContent = details.join(' | ');
if (progress.error) {
showError('Conversion Error: ' + (progress.error.message || progress.error));
}
}
```
--------------------------------
### Copy Text to Clipboard (JavaScript)
Source: https://github.com/hashangit/extract2md/blob/main/examples/demo.html
This function copies the content of 'lastResult' to the user's clipboard using the Navigator Clipboard API. It provides visual feedback to the user by changing the button text to 'Copied!' for 2 seconds. It requires 'lastResult' to be defined and accessible.
```javascript
function copyToClipboard() {
if (lastResult) {
navigator.clipboard.writeText(lastResult).then(() => {
const btn = document.querySelector('.copy-btn');
const originalText = btn.textContent;
btn.textContent = 'Copied!';
setTimeout(() => btn.textContent = originalText, 2000);
});
}
}
```
--------------------------------
### Use Extract2MD via CDN in HTML
Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md
Includes the Extract2MD UMD bundle from a CDN in an HTML file, making it available as a global variable for direct use in the browser. Demonstrates a basic conversion function.
```html
```
--------------------------------
### High-Accuracy OCR PDF to Markdown Conversion with Tesseract.js
Source: https://context7.com/hashangit/extract2md/llms.txt
Performs OCR-based PDF conversion using Tesseract.js, ideal for scanned documents or images. This method renders pages to canvas and applies OCR, offering higher accuracy at the cost of speed. Configuration includes Tesseract worker paths, language data, and rendering scale for improved accuracy. Progress is reported during OCR processing.
```javascript
import { Extract2MDConverter } from 'extract2md';
const pdfFile = document.getElementById('pdfInput').files[0];
const config = {
tesseract: {
workerPath: '/tesseract-worker.min.js',
corePath: '/tesseract-core.wasm.js',
langPath: '/lang-data/',
language: 'eng', // Use 'eng+fra' for multiple languages
options: {}
},
processing: {
pdfRenderScale: 2.5 // Higher scale = better OCR accuracy
},
progressCallback: (progress) => {
if (progress.stage === 'ocr_page_process') {
const percent = Math.round((progress.currentPage / progress.totalPages) * 100);
document.getElementById('status').textContent = `OCR Processing: ${percent}%`;
}
}
};
try {
const markdown = await Extract2MDConverter.highAccuracyConvertOnly(pdfFile, config);
console.log('OCR conversion complete:', markdown.length, 'characters');
} catch (error) {
if (error.message.includes('Tesseract worker')) {
console.error('OCR worker failed to initialize. Check worker paths.');
} else {
console.error('OCR conversion failed:', error.message);
}
}
```
--------------------------------
### Basic Markdown Extraction with Extract2MD
Source: https://context7.com/hashangit/extract2md/llms.txt
Demonstrates basic markdown extraction from raw output using the Extract2MD parser. It cleans and formats the content, removing specific elements like thinking blocks.
```javascript
const { Extract2MDConverter } = require('extract2md');
const parser = new Extract2MDConverter();
const rawOutput = "# Document Title\n\nThis is the cleaned content with proper formatting.\n\n- Item 1\n- Item 2\n`;
const cleanedOutput = parser.parse(rawOutput);
console.log(cleanedOutput);
// Output: "# Document Title\n\nThis is the cleaned content with proper formatting.\n\n- Item 1\n- Item 2"
```
```javascript
// Remove thinking blocks specifically
const noThinking = parser.removeThinkingBlocks(rawOutput);
// Validate markdown output
const validation = parser.validateMarkdown(cleanedOutput);
console.log('Is valid:', validation.isValid);
console.log('Issues:', validation.issues);
// Apply custom cleanup rules
const customRules = [
{ find: /\bfoo\b/g, replace: 'bar' },
{ find: /\s+$/gm, replace: '' }
];
const customCleaned = parser.applyCustomRules(cleanedOutput, customRules);
// Extract just the markdown content from verbose LLM responses
const verboseResponse = "Here's the rewritten text:\n\n# Actual Content\nThe document content...";
const pureMarkdown = parser.extractMarkdownContent(verboseResponse);
```
--------------------------------
### Configuring CDN for Worker Paths in JavaScript
Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md
Shows how to configure Extract2MD to use a Content Delivery Network (CDN) for its static assets, specifically worker files like PDF and Tesseract. This can improve loading performance by serving assets from geographically closer servers.
```javascript
window.EXTRACT2MD_CONFIG = {
workerPaths: {
pdf: 'https://cdn.example.com/pdf.worker.min.mjs',
tesseract: 'https://cdn.example.com/tesseract-worker.min.js'
}
};
```
--------------------------------
### Extract2MDConverter.highAccuracyConvertWithLLM
Source: https://context7.com/hashangit/extract2md/llms.txt
Combines Tesseract.js OCR extraction with WebLLM AI enhancement, ideal for scanned documents requiring accurate text recognition and AI-driven formatting improvements.
```APIDOC
## Extract2MDConverter.highAccuracyConvertWithLLM
### Description
Combines Tesseract.js OCR extraction with WebLLM AI enhancement. Ideal for scanned documents that need both accurate text recognition and improved formatting through AI processing.
### Method
`Extract2MDConverter.highAccuracyConvertWithLLM(pdfFile, config)`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **pdfFile** (File) - The PDF file to convert.
- **config** (Object) - Configuration object for the conversion process.
- **tesseract** (Object) - Tesseract.js configuration.
- **language** (string) - Language for OCR (e.g., 'eng').
- **options** (Object) - Tesseract.js options.
- **webllm** (Object) - WebLLM configuration.
- **model** (string) - The LLM model to use (e.g., 'Qwen3-0.6B-q4f16_1-MLC').
- **options** (Object) - LLM generation options (e.g., `temperature`, `maxTokens`).
- **processing** (Object) - Processing options.
- **pdfRenderScale** (number) - Scale factor for rendering PDF pages for OCR (higher for better accuracy on scanned docs).
- **progressCallback** (Function) - Callback function to track conversion progress and errors.
### Request Example
```javascript
import { Extract2MDConverter } from 'extract2md';
const pdfFile = document.getElementById('pdfInput').files[0];
const config = {
tesseract: {
language: 'eng',
options: {}
},
webllm: {
model: 'Qwen3-0.6B-q4f16_1-MLC',
options: {
temperature: 0.7,
maxTokens: 4096
}
},
processing: {
pdfRenderScale: 3.0 // Higher for better OCR on scanned docs
},
progressCallback: (progress) => {
document.getElementById('status').textContent = progress.message;
if (progress.error) {
console.error('Processing error:', progress.error);
}
}
};
const result = await Extract2MDConverter.highAccuracyConvertWithLLM(pdfFile, config);
console.log('High accuracy + LLM result:', result);
```
### Response
#### Success Response (200)
- **result** (Object) - An object containing the conversion results. The exact structure may vary, but typically includes the processed Markdown content.
```
--------------------------------
### Import Extract2MD using ES6 Modules
Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md
Imports the Extract2MD converter using ES6 module syntax. This method is suitable for modern bundlers and supports tree shaking for optimized builds.
```javascript
import { Extract2MDConverter } from 'extract2md';
// Use ES6 modules with tree shaking support
```
--------------------------------
### Quick PDF to Markdown Conversion with WebLLM Enhancement (JavaScript)
Source: https://context7.com/hashangit/extract2md/llms.txt
Converts PDF to Markdown using PDF.js extraction and enhances the output with a local LLM for improved clarity and formatting. Requires WebGPU support in the browser. The function takes a PDF file and a configuration object, returning the enhanced Markdown string.
```javascript
import { Extract2MDConverter } from 'extract2md';
const pdfFile = document.getElementById('pdfInput').files[0];
const config = {
webllm: {
model: 'Qwen3-0.6B-q4f16_1-MLC',
options: {
temperature: 0.7,
maxTokens: 4096
}
},
systemPrompts: {
singleExtraction: 'Focus on preserving code examples and technical terms exactly.'
},
progressCallback: (progress) => {
switch (progress.stage) {
case 'webllm_load_progress':
console.log(`Loading LLM: ${Math.round(progress.progress * 100)}%`);
break;
case 'webllm_generate_start':
console.log('AI enhancement in progress...');
break;
case 'scenario_3_complete':
console.log('Conversion with LLM enhancement completed!');
break;
}
}
};
try {
const enhancedMarkdown = await Extract2MDConverter.quickConvertWithLLM(pdfFile, config);
document.getElementById('output').innerHTML = `${enhancedMarkdown}`;
} catch (error) {
if (error.message.includes('WebGPU')) {
alert('WebGPU not supported. Please use Chrome 113+ or Edge 113+.');
} else {
console.error('Conversion failed:', error.message);
}
}
```
--------------------------------
### React Integration for PDF to Markdown Conversion
Source: https://github.com/hashangit/extract2md/blob/main/DEPLOYMENT.md
Provides a React component that allows users to upload a PDF file and convert it to Markdown using Extract2MDConverter. It includes state management for loading status and the conversion result, along with basic error handling.
```jsx
import React, { useState } from 'react';
import { Extract2MDConverter } from 'extract2md';
function PDFConverter() {
const [result, setResult] = useState('');
const [loading, setLoading] = useState(false);
const handleConvert = async (file) => {
setLoading(true);
try {
const markdown = await Extract2MDConverter.quickConvertOnly(file, {
tesseract: { language: 'eng', oem: 1, psm: 6 }
});
setResult(markdown);
} catch (error) {
console.error('Conversion failed:', error);
} finally {
setLoading(false);
}
};
return (
Converting...
} {result &&{result}}