Skip to content

Quick Start

This guide will help you get started with noto-pdf-ts quickly.

Installation

Terminal window
npm install noto-pdf-ts

Basic Usage

Rendering All Pages

import { openPdf } from 'noto-pdf-ts';
import fs from 'node:fs/promises';
const pdf = await openPdf('/path/to/document.pdf');
for await (const page of pdf.renderPages()) {
console.log(`Rendering page ${page.pageNumber}/${page.totalPages}`);
await fs.writeFile(`page-${page.pageNumber}.jpg`, page.buffer);
}
await pdf.close();

Using await using (ES2024)

With ES2024’s explicit resource management, you can use await using for automatic cleanup:

import { openPdf } from 'noto-pdf-ts';
import fs from 'node:fs/promises';
await using pdf = await openPdf('/path/to/document.pdf');
for await (const page of pdf.renderPages()) {
await fs.writeFile(`page-${page.pageNumber}.jpg`, page.buffer);
}
// pdf is automatically closed when the scope ends

One-Line Convenience Function

For simple use cases, use renderPdfPages:

import { renderPdfPages } from 'noto-pdf-ts';
import fs from 'node:fs/promises';
for await (const page of renderPdfPages('/path/to/document.pdf')) {
await fs.writeFile(`page-${page.pageNumber}.jpg`, page.buffer);
}
// PDF is automatically opened and closed

Rendering Options

Scale

Control the output resolution with the scale option:

// Thumbnail (smaller, faster)
for await (const page of pdf.renderPages({ scale: 0.5 })) { }
// High resolution (larger, more detailed)
for await (const page of pdf.renderPages({ scale: 3.0 })) { }

Format

Choose between JPEG (smaller) and PNG (lossless):

// JPEG (default)
for await (const page of pdf.renderPages({ format: 'jpeg' })) { }
// PNG
for await (const page of pdf.renderPages({ format: 'png' })) { }

Quality

Control JPEG quality (0-1):

// High quality
for await (const page of pdf.renderPages({ format: 'jpeg', quality: 0.95 })) { }
// Lower quality, smaller files
for await (const page of pdf.renderPages({ format: 'jpeg', quality: 0.5 })) { }

Specific Pages

Render only specific pages:

// Specific page numbers
for await (const page of pdf.renderPages({ pages: [1, 3, 5] })) { }
// Page range
for await (const page of pdf.renderPages({ pages: { start: 1, end: 5 } })) { }

Opening Encrypted PDFs

const pdf = await openPdf('/path/to/encrypted.pdf', {
password: 'secret'
});

Error Handling

import { openPdf, PdfError } from 'noto-pdf-ts';
try {
const pdf = await openPdf('/path/to/document.pdf');
// ...
} catch (error) {
if (error instanceof PdfError) {
switch (error.code) {
case 'FILE_NOT_FOUND':
console.error('File not found');
break;
case 'PASSWORD_REQUIRED':
console.error('Password required');
break;
case 'INVALID_PDF':
console.error('Invalid PDF file');
break;
default:
console.error(`Error: ${error.message}`);
}
}
}

Next Steps