@dariyd/react-native-pdf-page-image

中文文档
2.1.0发布于 2026/09/25MITTypeScriptCommonJS零依赖近 30 天下载 4185

Render PDF pages to JPEG or PNG images in React Native. Built for New Architecture (TurboModules + Codegen). Uses PDFKit on iOS and PdfRenderer on Android.

原始使用文档
文档目录15 节
  1. Installation
  2. iOS
  3. Android
  4. Requirements
  5. What's new in 2.1
  6. What changed in 2.0
  7. API
  8. PdfPageImage.open(uri)
  9. PdfPageImage.generate(uri, page, scale?, options?)
  10. PdfPageImage.generateAllPages(uri, scale?, options?)
  11. PdfPageImage.close(uri)
  12. Supported URI formats
  13. Example
  14. Notes
  15. License

@dariyd/react-native-pdf-page-image

Render PDF pages to JPEG or PNG images in React Native. Built for the New Architecture (TurboModules + Codegen).

  • iOS: PDFKit
  • Android: PdfRenderer

Installation

npm install @dariyd/react-native-pdf-page-image
# or
yarn add @dariyd/react-native-pdf-page-image

iOS

cd ios && pod install

Android

No additional setup required — auto-linked via Gradle.

Requirements

  • React Native 0.76+ (New Architecture enabled)
  • iOS 15.0+
  • Android API 24+

What's new in 2.1

  • PdfPageImage.compress(uri, options?) — writes a smaller copy of a PDF with every page re-encoded as a JPEG (default 150 dpi, quality 70). Page sizes are kept; text becomes part of the page image. Works page by page on a background thread, so long scans don't spike memory. On Android the JPEGs are embedded directly (the system PdfDocument would store them uncompressed).
const result = await PdfPageImage.compress('file:///path/to/scan.pdf', { dpi: 150, quality: 70 });
if (result.bytes < result.originalBytes * 0.8) {
  // keep result.uri (a file:// in the temp/cache directory)
}

Best for scans and photos. A PDF made of text (an emailed invoice) may come out larger — always compare bytes with originalBytes.

What changed in 2.0

  • JPEG output by default (quality: 80) — ~10–20× smaller than PNG for scanned/photographic pages. Pass { format: 'png' } for the old behavior.
  • iOS renders at true 1× scale. Previously the output was silently multiplied by the device screen scale (3× on modern iPhones), so scale: 1.0 produced 3× the pixels — and different sizes than Android. Now 1 PDF point = scale pixels on both platforms.
  • New maxDimension option — cap the long edge in pixels without knowing the page size. Ideal for thumbnails.
  • iOS temp files moved from the app's Documents directory to the temporary directory (no more user-visible litter if close() is missed).

API

PdfPageImage.open(uri)

Opens a PDF and returns page count.

const info = await PdfPageImage.open('file:///path/to/document.pdf');
console.log(info.pageCount); // 5

Returns: Promise<{ uri: string, pageCount: number }>

PdfPageImage.generate(uri, page, scale?, options?)

Renders a single page to an image.

// Full-quality render at 2x
const image = await PdfPageImage.generate('file:///path/to/document.pdf', 0, 2.0);

// Lightweight thumbnail: long edge capped at 640 px
const thumb = await PdfPageImage.generate(pdfUri, 0, 1.0, { maxDimension: 640 });
console.log(thumb.uri);    // file:///.../<uuid>.jpg
ParameterTypeDescription
uristringPDF file URI
pagenumberPage index (0-based)
scalenumber?Pixels per PDF point (default: 1.0, range: 0.1 – 10.0)
optionsGenerateOptions?Output options (below)

GenerateOptions:

OptionTypeDescription
format'jpeg' | 'png'Output encoding (default: 'jpeg')
qualitynumber?JPEG quality 1–100 (default: 80; ignored for PNG)
maxDimensionnumber?Cap the long edge in pixels; reduces the effective scale when needed (default: 0 = no cap)

Returns: Promise<{ uri: string, width: number, height: number }>

PdfPageImage.generateAllPages(uri, scale?, options?)

Renders all pages to images. Same options as generate, applied per page.

const pages = await PdfPageImage.generateAllPages(pdfUri, 1.5, { quality: 90 });
pages.forEach((page, i) => {
  console.log(`Page ${i}: ${page.uri} (${page.width}x${page.height})`);
});

Returns: Promise<Array<{ uri: string, width: number, height: number }>>

PdfPageImage.close(uri)

Closes the PDF and deletes temporary image files. Call this when you're done to free memory.

await PdfPageImage.close('file:///path/to/document.pdf');

Returns: Promise<void>

Supported URI formats

FormatExample
File path/path/to/file.pdf
File URIfile:///path/to/file.pdf
HTTP/HTTPShttps://example.com/doc.pdf
Base64 data URIdata:application/pdf;base64,JVBERi0...
Content URI (Android)content://com.provider/doc.pdf

Example

import { PdfPageImage } from '@dariyd/react-native-pdf-page-image';

async function renderPdfThumbnails(pdfUri: string) {
  try {
    const { pageCount } = await PdfPageImage.open(pdfUri);
    console.log(`PDF has ${pageCount} pages`);

    // Render first page as a lightweight thumbnail
    const thumbnail = await PdfPageImage.generate(pdfUri, 0, 1.0, {
      maxDimension: 640,
    });
    // Use thumbnail.uri in an <Image /> component

    // Or render all pages at full quality
    const allPages = await PdfPageImage.generateAllPages(pdfUri, 2.0, {
      quality: 90,
    });

    // Clean up when done
    await PdfPageImage.close(pdfUri);
  } catch (error) {
    console.error('PDF rendering failed:', error);
  }
}

Notes

  • Pages are cached per URI + page index + scale + options — repeated calls return cached results instantly
  • Temporary image files are stored in the temporary directory (iOS) or cache directory (Android)
  • Always call close() when done to free memory and delete temporary files
  • Pages are drawn on a white background, so JPEG (no alpha channel) loses nothing
  • Page rotation (90°, 180°, 270°) is handled automatically on iOS

License

MIT

版本与发布记录

@dariyd/react-native-pdf-page-image 共发布 3 个版本,首次发布于 2026/04/09;过去 12 个月发布了 3 个版本。

版本发布日期距今
2.1.0latest2026/09/251 个月前
2.0.02026/07/203 个月前
1.0.12026/04/097 个月前

关于 @dariyd/react-native-pdf-page-image 的常见问题

@dariyd/react-native-pdf-page-image 是什么?

Render PDF pages to JPEG or PNG images in React Native. Built for New Architecture (TurboModules + Codegen). Uses PDFKit on iOS and PdfRenderer on Android.

如何安装 @dariyd/react-native-pdf-page-image?

在项目目录运行 npm install @dariyd/react-native-pdf-page-image,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.2.0)、react-native(>=0.76.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dariyd/react-native-pdf-page-image 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dariyd/react-native-pdf-page-image 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@dariyd/react-native-pdf-page-image 可以商用吗?

@dariyd/react-native-pdf-page-image 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dariyd/react-native-pdf-page-image 的下载量有多少?

根据 npm 官方下载统计,@dariyd/react-native-pdf-page-image 在 2026/10/12 – 2026/11/10 这 30 天内被下载 4,185 次,日均约 140 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dariyd/react-native-pdf-page-image 还在维护吗?

最新版本 2.1.0 发布于 2026/09/25(1 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/04/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。