CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-pdf2pic

A utility for converting pdf to image formats with support for different outputs: directly to file, base64 or buffer.

Overall
score

97%

Overview
Eval results
Files

input-sources.mddocs/

PDF Input Sources

pdf2pic supports three different ways to initialize PDF conversion, each accepting the same configuration options and returning identical Convert interfaces.

Capabilities

From File Path

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" });

From Buffer

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]);

From Base64

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 Comparison

Input SourceUse CaseAdvantagesConsiderations
fromPathLocal files, server-side processingDirect file access, efficient for large filesRequires file system access
fromBufferIn-memory data, network streamsMemory-based, works with any data sourceMemory usage for large PDFs
fromBase64Web APIs, JSON payloadsWeb-friendly, easy serializationBase64 encoding overhead (~33% larger)

Common Patterns

Error Handling

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);
}

Option Inheritance

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 settings

Install with Tessl CLI

npx tessl i tessl/npm-pdf2pic

docs

configuration.md

conversion.md

index.md

input-sources.md

tile.json