
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Fast, ultra-accurate text extraction from any image or PDF, even challenging ones, with structured markdown output powered by vision models.
Fast, ultra-accurate text extraction from any image or PDF—including challenging ones—with structured Markdown output powered by vision models.
OcrLLM requires GraphicsMagick and Ghostscript for PDF processing. These dependencies are typically installed automatically when you install the package, especially on macOS. However, if the automatic installation fails, you may need to install them manually.
To verify that they are installed, run the following commands:
For GraphicsMagick:
gm version
For Ghostscript:
gs -version
If these commands return errors, you can install the dependencies using the following methods:
brew install graphicsmagick ghostscript
Download and install the following:
Ensure that both executables are added to your system's PATH
environment variable.
sudo apt-get update && sudo apt-get install -y graphicsmagick ghostscript
These are the most common installation methods, but feel free to install GraphicsMagick and Ghostscript in any way that suits you best. The important thing is to ensure that both are successfully installed on your system.
Install the ocr-llm
package via npm:
npm install ocr-llm
import {OcrLLM} from 'ocr-llm';
const ocrllm = new OcrLLM({
provider: 'openai',
key: 'your-api-key',
});
// Extract text from an image
const imageResult = await ocrllm.image('path/to/image.jpg');
console.log(imageResult.content);
// Process a PDF document
const pdfResults = await ocrllm.pdf('path/to/document.pdf');
pdfResults.forEach(page => {
console.log(`Page ${page.page}:`, page.content);
});
OcrLLM accepts multiple input formats:
Input Type | Example |
---|---|
File paths | '/path/to/image.jpg' , 'C:\\Documents\\scan.pdf' |
URLs | 'https://example.com/image.png' , 'https://files.com/document.pdf' |
Base64 strings | 'data:image/jpeg;base64,/9j/4AAQSkZJRg...' |
Buffer objects | Buffer.from(imageData) , fs.readFileSync('image.jpg') |
OcrLLM
Classnew OcrLLM(config)
Creates a new instance of OcrLLM.
config
(Object):
provider
(string): OCR provider (currently only 'openai'
is supported)key
(string): API key for the providerOcrLLM
instanceocrllm.image(input)
Processes a single image.
input
(string | Buffer): File path, URL, base64 string, or BufferPromise<ImageResult>
content
(string): Extracted text in Markdown formatmetadata
(Object): Processing metadataocrllm.pdf(input)
Processes a PDF document.
input
(string | Buffer): File path, URL, base64 string, or BufferPromise<PageResult[]>
page
(number): Page numbercontent
(string): Extracted text in Markdown formatmetadata
(Object): Processing metadataocrllm.pdfImages(inputs)
Processes multiple PDF page images.
inputs
(Array<string | Buffer>): Array of image URLs, base64 strings, or BuffersPromise<PageResult[]>
page
(number): Page numbercontent
(string): Extracted text in Markdown formatmetadata
(Object): Processing metadataOcrLLM includes built-in error handling with detailed error messages and automatic retries for transient failures.
try {
const result = await ocrllm.image('path/to/image.jpg');
} catch (error) {
console.error('Processing failed:', error.message);
}
OcrLLM uses the following model:
Provider | Model | Description |
---|---|---|
OpenAI | gpt-4o-mini | High-performance model optimized for efficient text extraction with excellent accuracy and speed. |
When using OcrLLM in serverless environments like Vercel (for example, when hosting a Next.js application that implements text extraction in an API route handler), the core library's PDF processing requires system-level dependencies (GraphicsMagick, Ghostscript) that cannot be installed. However, OcrLLM provides a browser-specific implementation that can handle the PDF-to-image conversion step directly in the browser.
By using the browser package for PDF conversion and the main OcrLLM package for text extraction, you can maintain full functionality without needing system dependencies on your server. This hybrid approach gives you the best of both worlds: client-side PDF handling and server-side OCR processing.
First, convert the PDF to images in the browser:
import {pdfto} from 'ocr-llm/browser';
const dataUrls = await pdfto.images(pdfFile, {
output: 'dataurl',
});
Then, send the image data URLs to your API and process them:
import {OcrLLM} from 'ocr-llm';
const ocrllm = new OcrLLM({
provider: 'openai',
key: 'your-api-key',
});
const results = await ocrllm.pdfImages(dataUrls);
results.forEach(page => {
console.log(`Page ${page.page}:`, page.content);
});
Remember that we are sending the data URL of each PDF page as an array to the API or Next.js API route handler. When hosting on providers like Vercel, processing PDFs with more than 25 pages (depending on the content size of each page) may trigger a FUNCTION_PAYLOAD_TOO_LARGE
error due to their 4.5MB function body size limit. Similar limitations may exist on other hosting platforms.
pdfto.images
API Referencepdfto.images(pdfFile, options);
Parameters:
pdfFile
: The PDF file as a File
object.options
(optional):
format
(string): Output image format. Options are 'png'
or 'jpg'
. Default is 'png'
.scale
(number): Scale factor for the output images. Increase for better quality. Default is 1.0
.pages
(string | number | number[] | object): Page selection. Options are 'all'
, 'first'
, 'last'
, a page number, an array of page numbers, or an object { start?: number, end?: number }
. Default is 'all'
.output
(string): Output format. Options are 'buffer'
, 'base64'
, 'blob'
, or 'dataurl'
. Default is 'base64'
.docParams
(object): Additional PDF document parameters.Returns: Promise<string[]>
- An array of image data in the specified output format.
Example Usage with Options:
const urls = await pdfto.images(pdfFile, {
format: 'png',
scale: 2.0,
pages: {start: 1, end: 5},
output: 'dataurl',
});
We welcome contributions from the community to enhance OcrLLM's capabilities and make it even more powerful. ❤️
For guidelines on contributing, please read the Contributing Guide.
FAQs
Fast, ultra-accurate text extraction from any image or PDF, even challenging ones, with structured markdown output powered by vision models.
The npm package ocr-llm receives a total of 8 weekly downloads. As such, ocr-llm popularity was classified as not popular.
We found that ocr-llm demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.