Skip to content

A fast, helpful, and open-source document parser

add pdfium file

Logan Markewich13d603102b5c
diff --git a/src/engines/pdf/pdfium-renderer.ts b/src/engines/pdf/pdfium-renderer.ts
new file mode 100644
index 0000000..8ddecac
--- /dev/null
+++ b/src/engines/pdf/pdfium-renderer.ts
@@ -0,0 +1,72 @@
+import { PDFiumLibrary, type PDFiumPageRenderOptions } from '@hyzyla/pdfium';
+import sharp from 'sharp';
+import { promises as fs } from 'fs';
+
+/**
+ * PDFium-based PDF screenshot renderer
+ * Uses native PDFium library for high-quality, fast screenshots
+ */
+export class PdfiumRenderer {
+ private pdfium: PDFiumLibrary | null = null;
+
+ async init(): Promise<void> {
+ if (!this.pdfium) {
+ this.pdfium = await PDFiumLibrary.init();
+ }
+ }
+
+ async renderPageToBuffer(
+ pdfPath: string,
+ pageNumber: number,
+ dpi: number = 150
+ ): Promise<Buffer> {
+ await this.init();
+
+ if (!this.pdfium) {
+ throw new Error('PDFium not initialized');
+ }
+
+ // Read PDF file
+ const pdfBuffer = await fs.readFile(pdfPath);
+
+ // Load document
+ const document = await this.pdfium.loadDocument(pdfBuffer);
+
+ try {
+ // Get page (0-indexed in pdfium)
+ const page = document.getPage(pageNumber - 1);
+
+ // Calculate scale from DPI (72 DPI is the default)
+ const scale = dpi / 72;
+
+ // Render page using Sharp for image processing
+ const image = await page.render({
+ scale,
+ render: async (options: PDFiumPageRenderOptions) => {
+ return await sharp(options.data, {
+ raw: {
+ width: options.width,
+ height: options.height,
+ channels: 4, // RGBA
+ },
+ })
+ .png()
+ .toBuffer();
+ },
+ });
+
+ return Buffer.from(image.data);
+ } finally {
+ // Clean up document
+ document.destroy();
+ }
+ }
+
+ async close(): Promise<void> {
+ // PDFium WASM doesn't need explicit cleanup
+ if (this.pdfium) {
+ this.pdfium.destroy();
+ this.pdfium = null;
+ }
+ }
+}