pdf_image_renderer 包通过利用 Android 和 iOS 的原生 PDF 渲染功能,实现了快速且简单的 PDF 文档渲染。它提供了一种轻量级、高效的解决方案,可将 PDF 页面转换为图像,而无需依赖纯 Dart 实现。这种做法确保了高性能和准确的视觉输出,与原生操作系统行为高度一致。适用于需要快速预览或缩略图的 PDF 应用程序,简化了集成过程,设置简单。该包由 cloudacy.com 维护,兼容两大主流移动平台。
适合场景
生成PDF缩略图
在应用程序中渲染PDF页面
创建文档的图像预览
显示PDF内容而无需完整查看器
在 Flutter 中集成原生 PDF 渲染
主要特点
使用原生操作系统PDF渲染器
高性能和高精度
所需设置最少
跨平台支持(Android、iOS)
轻便高效
适合对象
需要PDF预览的移动应用
基于图像的文档处理
性能敏感的渲染应用
寻求原生渲染保真度的开发者
避免使用大型PDF库的项目
使用建议
仅限 Android 和 iOS
不支持网页或桌面端
依赖于平台特定的PDF渲染
可能因设备操作系统版本略有不同
不支持交互式PDF功能
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
pdf_image_renderer
Renders PDFs to bitmaps using native renderers.
Usage
See the example folder for a fully working flutter example.
classMyAppextendsStatefulWidget{
@override_MyAppStatecreateState() => _MyAppState();
}
class_MyAppStateextendsState<MyApp> {
Uint8List image;
voidrenderPdfImage() async {
// Get a path from a pdf file (we are using the file_picker package (https://pub.dev/packages/file_picker))String path = awaitFilePicker.getFilePath(type: FileType.custom, allowedExtensions: ['pdf']);
// Initialize the rendererfinal pdf = PdfImageRenderer(path: path);
// open the pdf documentawait pdf.open(
// password: 'password', // optional (iOS and Android 15.0+ support - ignored otherwise)
)
// open a page from the pdf document using the page indexawait pdf.openPage(pageIndex: 0);
// get the render size after the page is loadedfinal size = await pdf.getPageSize(pageIndex: 0);
// get the actual image of the pagefinal img = await pdf.renderPage(
pageIndex: pageIndex,
x: 0,
y: 0,
width: size.width, // you can pass a custom size here to crop the image
height: size.height, // you can pass a custom size here to crop the image
scale: 1, // increase the scale for better quality (e.g. for zooming)
background: Colors.white,
);
// close the page againawait pdf.closePage(pageIndex: 0);
// close the PDF after rendering the page
pdf.close();
// use setState to update the renderersetState(() {
image = img;
});
}
@overridevoidinitState() {
super.initState();
}
// you can use this image later in your build function@overrideWidgetbuild(BuildContext context) {
returnMaterialApp(
home: Scaffold(
appBar: AppBar(
title: constText('pdf_image_renderer'),
),
body: Center(
child: image != null ? Image(image: MemoryImage(image)) : Text("Loading..."),
)
)
);
}
}