Open Document
This guide covers all the ways to open and load PDF documents.
Basic Loading
Section titled “Basic Loading”import { PDFium } from '@scaryterry/pdfium';import { promises as fs } from 'fs';
const data = await fs.readFile('document.pdf');
using pdfium = await PDFium.init();using document = await pdfium.openDocument(data);
console.log(`Loaded ${document.pageCount} pages`);Input Types
Section titled “Input Types”Uint8Array
Section titled “Uint8Array”const uint8 = new Uint8Array(buffer);using document = await pdfium.openDocument(uint8);ArrayBuffer
Section titled “ArrayBuffer”const arrayBuffer = await file.arrayBuffer();using document = await pdfium.openDocument(arrayBuffer);Node.js Buffer
Section titled “Node.js Buffer”const buffer = await fs.readFile('document.pdf');// Buffer is compatible with Uint8Arrayusing document = await pdfium.openDocument(buffer);Password-Protected PDFs
Section titled “Password-Protected PDFs”Known Password
Section titled “Known Password”using document = await pdfium.openDocument(data, { password: 'secret123',});Prompt for Password
Section titled “Prompt for Password”import { DocumentError, PDFiumErrorCode } from '@scaryterry/pdfium';
async function openWithPasswordPrompt( pdfium: PDFium, data: Uint8Array): Promise<PDFiumDocument> { let password: string | undefined;
while (true) { try { return await pdfium.openDocument(data, { password }); } catch (error) { if (!(error instanceof DocumentError)) throw error;
if (error.code === PDFiumErrorCode.DOC_PASSWORD_REQUIRED) { password = await promptUser('Enter password:'); } else if (error.code === PDFiumErrorCode.DOC_PASSWORD_INCORRECT) { password = await promptUser('Incorrect. Try again:'); } else { throw error; }
if (!password) { throw new Error('Password required'); } } }}Error Handling
Section titled “Error Handling”import { DocumentError, PDFiumErrorCode,} from '@scaryterry/pdfium';
try { using document = await pdfium.openDocument(data);} catch (error) { if (error instanceof DocumentError) { switch (error.code) { case PDFiumErrorCode.DOC_FORMAT_INVALID: console.error('Not a valid PDF file'); break; case PDFiumErrorCode.DOC_PASSWORD_REQUIRED: console.error('Password required'); break; case PDFiumErrorCode.DOC_PASSWORD_INCORRECT: console.error('Wrong password'); break; case PDFiumErrorCode.DOC_SECURITY_UNSUPPORTED: console.error('Unsupported encryption'); break; default: console.error(`Document error: ${error.message}`); } } else { throw error; }}Loading from Different Sources
Section titled “Loading from Different Sources”Node.js File
Section titled “Node.js File”import { promises as fs } from 'fs';
const data = await fs.readFile('path/to/document.pdf');using document = await pdfium.openDocument(data);Browser File Input
Section titled “Browser File Input”async function handleFileInput(input: HTMLInputElement) { const file = input.files?.[0]; if (!file) return;
const data = await file.arrayBuffer(); using document = await pdfium.openDocument(data); // Use document...}Fetch from URL
Section titled “Fetch from URL”const response = await fetch('https://example.com/document.pdf');const data = await response.arrayBuffer();using document = await pdfium.openDocument(data);Base64 Encoded
Section titled “Base64 Encoded”function base64ToUint8Array(base64: string): Uint8Array { const binary = atob(base64); const bytes = new Uint8Array(binary.length); for (let i = 0; i < binary.length; i++) { bytes[i] = binary.charCodeAt(i); } return bytes;}
const data = base64ToUint8Array(base64String);using document = await pdfium.openDocument(data);Document Information
Section titled “Document Information”After opening:
using document = await pdfium.openDocument(data);
console.log(`Pages: ${document.pageCount}`);console.log(`Attachments: ${document.attachmentCount}`);
// Check for bookmarksconst bookmarks = document.getBookmarks();console.log(`Bookmarks: ${bookmarks.length}`);Resource Limits
Section titled “Resource Limits”Configure limits when initialising PDFium:
const pdfium = await PDFium.init({ limits: { maxDocumentSize: 50 * 1024 * 1024, // 50 MB max },});
// Loading a larger file will throw MemoryErrortry { using document = await pdfium.openDocument(hugeFile);} catch (error) { if (error instanceof MemoryError) { console.error('File too large'); }}Validation Pattern
Section titled “Validation Pattern”interface ValidationResult { valid: boolean; pageCount?: number; encrypted?: boolean; error?: string;}
async function validatePDF( pdfium: PDFium, data: Uint8Array): Promise<ValidationResult> { try { using document = await pdfium.openDocument(data); return { valid: true, pageCount: document.pageCount, encrypted: false, }; } catch (error) { if (error instanceof DocumentError) { if (error.code === PDFiumErrorCode.DOC_PASSWORD_REQUIRED) { return { valid: true, encrypted: true, }; } return { valid: false, error: error.message, }; } throw error; }}Complete Example
Section titled “Complete Example”import { PDFium, DocumentError, PDFiumErrorCode } from '@scaryterry/pdfium';import { promises as fs } from 'fs';
async function loadPDF(filePath: string, password?: string) { // Read file const data = await fs.readFile(filePath);
// Initialise library using pdfium = await PDFium.init({ limits: { maxDocumentSize: 100 * 1024 * 1024, }, });
// Attempt to open try { using document = await pdfium.openDocument(data, { password });
console.log(`Loaded: ${filePath}`); console.log(`Pages: ${document.pageCount}`);
// Process document... for (const page of document.pages()) { using p = page; console.log(`Page ${p.index + 1}: ${p.width} x ${p.height} points`); }
return true; } catch (error) { if (error instanceof DocumentError) { switch (error.code) { case PDFiumErrorCode.DOC_FORMAT_INVALID: console.error(`Invalid PDF: ${filePath}`); break; case PDFiumErrorCode.DOC_PASSWORD_REQUIRED: console.error('Password required. Use --password option.'); break; case PDFiumErrorCode.DOC_PASSWORD_INCORRECT: console.error('Incorrect password.'); break; default: console.error(`Error: ${error.message}`); } return false; } throw error; }}
// UsageloadPDF('document.pdf');loadPDF('encrypted.pdf', 'secret123');See Also
Section titled “See Also”- PDFium — openDocument API
- PDFiumDocument — Document API
- Error Handling — Error patterns
- Progressive Loading — Streaming large PDFs