A utility for converting pdf to image formats with support for different outputs: directly to file, base64 or buffer.
Overall
score
97%
pdf2pic supports three different ways to initialize PDF conversion, each accepting the same configuration options and returning identical Convert interfaces.
Initialize PDF conversion by providing a file system path to the PDF document.
/**
* Initialize PDF to image conversion from file path
* @param filePath - Path to the PDF file on the file system
* @param options - Configuration options for conversion
* @returns Convert interface for performing conversions
*/
function fromPath(filePath: string, options?: Options): Convert;Usage Examples:
import { fromPath } from "pdf2pic";
// Basic usage with minimal options
const convert = fromPath("/path/to/document.pdf");
const result = await convert(1);
// With custom options
const convert2 = fromPath("/path/to/document.pdf", {
format: "jpg",
width: 800,
height: 600,
savePath: "./output"
});
const result2 = await convert2(1, { responseType: "image" });Initialize PDF conversion from a Buffer containing PDF data, useful when working with PDF data in memory or from network requests.
/**
* Initialize PDF to image conversion from Buffer
* @param buffer - Buffer containing PDF data
* @param options - Configuration options for conversion
* @returns Convert interface for performing conversions
*/
function fromBuffer(buffer: Buffer, options?: Options): Convert;Usage Examples:
import { fromBuffer } from "pdf2pic";
import fs from "fs";
// From file system buffer
const pdfBuffer = fs.readFileSync("/path/to/document.pdf");
const convert = fromBuffer(pdfBuffer, {
format: "png",
density: 150
});
const result = await convert(1, { responseType: "base64" });
// From HTTP response
const response = await fetch("https://example.com/document.pdf");
const buffer = Buffer.from(await response.arrayBuffer());
const convert2 = fromBuffer(buffer);
const result2 = await convert2.bulk([1, 2, 3]);Initialize PDF conversion from a base64-encoded string containing PDF data, commonly used in web applications and API integrations.
/**
* Initialize PDF to image conversion from base64 string
* @param b64string - Base64-encoded PDF data
* @param options - Configuration options for conversion
* @returns Convert interface for performing conversions
*/
function fromBase64(b64string: string, options?: Options): Convert;Usage Examples:
import { fromBase64 } from "pdf2pic";
// From base64 string (typical in web APIs)
const base64Pdf = "JVBERi0xLjQKJdPr6eEKMSAwIG9iago8PC9UeXBlL..."; // truncated
const convert = fromBase64(base64Pdf, {
format: "jpg",
quality: 95,
width: 1024
});
// Convert to buffer for further processing
const result = await convert(1, { responseType: "buffer" });
const imageBuffer = result.buffer;
// Bulk conversion from base64
const allPages = await convert.bulk(-1, { responseType: "base64" });
allPages.forEach((page, index) => {
console.log(`Page ${index + 1} base64 length: ${page.base64.length}`);
});| Input Source | Use Case | Advantages | Considerations |
|---|---|---|---|
fromPath | Local files, server-side processing | Direct file access, efficient for large files | Requires file system access |
fromBuffer | In-memory data, network streams | Memory-based, works with any data source | Memory usage for large PDFs |
fromBase64 | Web APIs, JSON payloads | Web-friendly, easy serialization | Base64 encoding overhead (~33% larger) |
All initialization functions will validate inputs and throw errors for invalid parameters:
import { fromPath } from "pdf2pic";
try {
const convert = fromPath("/nonexistent/file.pdf");
const result = await convert(1);
} catch (error) {
console.error("Conversion failed:", error.message);
}Options passed to initialization functions apply to all subsequent conversions, but can be modified via the Convert interface:
import { fromPath } from "pdf2pic";
const convert = fromPath("/path/to/document.pdf", {
format: "png",
width: 800
});
// These will use png format and 800px width
await convert(1);
await convert.bulk([2, 3, 4]);
// Modify GraphicsMagick settings
convert.setGMClass("imagemagick"); // Use ImageMagick instead of GraphicsMagick
convert.setOptions(); // Apply any modified settingsInstall with Tessl CLI
npx tessl i tessl/npm-pdf2picevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10