### Install Unpdf Source: https://unjs.io/packages/unpdf/index Instructions for installing the unpdf package using different package managers like pnpm, npm, and yarn. ```bash # pnpm pnpm add unpdf # npm npm install unpdf # yarn yarn add unpdf ``` -------------------------------- ### Render PDF Page as Image with Unpdf Source: https://unjs.io/packages/unpdf/index Demonstrates how to use the `renderPageAsImage` method from the 'unpdf' package to convert a PDF page into an image. It includes configuring Unpdf with PDF.js and handling file operations in a Node.js environment. ```javascript import { configureUnPDF, renderPageAsImage } from "unpdf"; await configureUnPDF({ // Use the official PDF.js build pdfjs: () => import("pdfjs-dist"), }); const pdf = await readFile("./dummy.pdf"); const buffer = new Uint8Array(pdf); const pageNumber = 1; const result = await renderPageAsImage(buffer, pageNumber, { canvas: () => import("canvas"), }); await writeFile("dummy-page-1.png", Buffer.from(result)); ``` -------------------------------- ### Access PDF.js API Source: https://unjs.io/packages/unpdf/index Shows how to access the underlying PDF.js API through `getResolvedPDFJS`. This is useful for platforms like Deno or when direct access to PDF.js methods like `getDocument` is needed. It demonstrates fetching document metadata and extracting text page by page. ```javascript import { getResolvedPDFJS } from "unpdf"; const { getDocument } = await getResolvedPDFJS(); const data = Deno.readFileSync("dummy.pdf"); const doc = await getDocument(data).promise; console.log(await doc.getMetadata()); for (let i = 1; i <= doc.numPages; i++) { const page = await doc.getPage(i); const textContent = await page.getTextContent(); const contents = textContent.items.map((item) => item.str).join(" "); console.log(contents); } ``` -------------------------------- ### getMeta Method Signature Source: https://unjs.io/packages/unpdf/index Shows the TypeScript signature for the `getMeta` function. This function accepts binary data or a `PDFDocumentProxy` and returns a Promise resolving to an object containing PDF information and metadata. ```typescript function getMeta(data: BinaryData | PDFDocumentProxy): Promise<{ info: Record; metadata: Record; }>; ``` -------------------------------- ### Configure Unpdf with Custom PDF.js Build Source: https://unjs.io/packages/unpdf/index Explains how to configure `unpdf` to use a different PDF.js build, such as the official or a legacy version, by providing a custom resolver function to `configureUnPDF`. This must be called before using other `unpdf` methods. ```javascript // Before using any other method, define the PDF.js module // if you need another PDF.js build import { configureUnPDF } from "unpdf"; await configureUnPDF({ // Use the official PDF.js build (make sure to install it first) pdfjs: () => import("pdfjs-dist"), }); // Now, you can use the other methods // … ``` -------------------------------- ### configureUnPDF Method Signature Source: https://unjs.io/packages/unpdf/index Provides the TypeScript signature for the `configureUnPDF` function. This function is used to set a custom PDF.js module, like a legacy build, and must be invoked before any other `unpdf` methods are called. ```typescript function configureUnPDF(config: UnPDFConfiguration): Promise; ``` -------------------------------- ### Unpdf Configuration Interface Source: https://unjs.io/packages/unpdf/index Defines the TypeScript interface `UnPDFConfiguration` for configuring `unpdf`. It includes an optional `pdfjs` property that accepts a function returning a Promise resolving to the PDFJS object, allowing the use of custom PDF.js builds. ```typescript interface UnPDFConfiguration { /** * By default, UnPDF will use the latest version of PDF.js compiled for * serverless environments. If you want to use a different version, you can * provide a custom resolver function. * * @example * // Use the official PDF.js build (make sure to install it first) * () => import('pdfjs-dist') */ pdfjs?: () => Promise; } ``` -------------------------------- ### Extract Text From PDF Source: https://unjs.io/packages/unpdf/index Demonstrates how to extract text content from a PDF file using the `extractText` and `getDocumentProxy` functions from the 'unpdf' library. It shows loading a PDF from a URL or the filesystem and processing it. ```javascript import { extractText, getDocumentProxy } from "unpdf"; // Fetch a PDF file from the web const buffer = await fetch( "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf", ).then((res) => res.arrayBuffer()); // Or load it from the filesystem // const buffer = await readFile("./dummy.pdf"); // Load PDF from buffer const pdf = await getDocumentProxy(new Uint8Array(buffer)); // Extract text from PDF const { totalPages, text } = await extractText(pdf, { mergePages: true }); ``` -------------------------------- ### getResolvedPDFJS Method Signature Source: https://unjs.io/packages/unpdf/index Presents the TypeScript signature for the `getResolvedPDFJS` function. It returns a Promise that resolves to the PDF.js module. If no custom build has been defined, it initializes the bundled serverless build. ```typescript function getResolvedPDFJS(): Promise; ``` -------------------------------- ### extractText Method Signature Source: https://unjs.io/packages/unpdf/index Details the TypeScript signature for the `extractText` function. It takes PDF data and an optional configuration object to merge pages. It returns a Promise with the total number of pages and the extracted text, either as a single string or an array of strings. ```typescript function extractText( data: BinaryData | PDFDocumentProxy, { mergePages }?: { mergePages?: boolean }, ): Promise<{ totalPages: number; text: string | string[]; }>; ``` -------------------------------- ### Unpdf renderPageAsImage Type Declaration Source: https://unjs.io/packages/unpdf/index Provides the TypeScript type declaration for the `renderPageAsImage` function in Unpdf. It outlines the function's parameters, including the PDF data, page number, and optional configuration for canvas, scale, width, and height, as well as its return type. ```typescript declare function renderPageAsImage( data: BinaryData, pageNumber: number, options?: { canvas?: () => Promise; /** @default 1 */ scale?: number; width?: number; height?: number; }, ): Promise; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.