PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
ComPDF Flutter SDK supports rendering PDF pages as thumbnails for preview, navigation, document directory, page selector, and custom PDF management UIs.
Two approaches are available:
CPDFPageThumbnailProvider as an ImageProvider in Flutter's Image widget.CPDFPageThumbnail for a ready-to-use widget with placeholder and error builders.If you already have a CPDFDocument instance, use it as the thumbnail source to avoid reopening the file.
The following example renders the first page of an opened document as a thumbnail.
final CPDFDocument document = await CPDFDocument.createInstance();
await document.open('/path/to/sample.pdf');
CPDFPageThumbnail.document(
document: document,
pageIndex: 0,
options: const CPDFPageThumbnailOptions(width: 240),
)| Class | Purpose |
|---|---|
CPDFPageThumbnailProvider | ImageProvider for Flutter's Image widget |
CPDFPageThumbnail | Ready-to-use thumbnail widget with placeholder and error builders |
CPDFPageThumbnailOptions | Rendering options: size, background, compression, caching |
CPDFPageThumbnailService | Cache management and metrics |
CPDFPageThumbnailCachePolicy | Cache strategy: memory, disk, both, or none |
CPDFPageThumbnailProvider is an ImageProvider. Pass it directly to Flutter's Image widget.
import 'package:compdfkit_flutter/compdfkit.dart';
import 'package:flutter/material.dart';
class ThumbnailView extends StatelessWidget {
const ThumbnailView({
super.key,
required this.document,
required this.pageIndex,
});
final CPDFDocument document;
final int pageIndex;
@override
Widget build(BuildContext context) {
return Image(
image: CPDFPageThumbnailProvider.document(
document: document,
pageIndex: pageIndex,
options: const CPDFPageThumbnailOptions(
width: 300,
compression: CPDFPageCompression.jpeg,
),
),
fit: BoxFit.contain,
errorBuilder: (context, error, stackTrace) {
return const Icon(Icons.broken_image_outlined);
},
);
}
}CPDFPageThumbnail wraps CPDFPageThumbnailProvider with a placeholder and error builder for convenience.
CPDFPageThumbnail.document(
document: document,
pageIndex: 0,
options: const CPDFPageThumbnailOptions(width: 300),
fit: BoxFit.contain,
placeholderBuilder: (context) {
return const Center(child: CircularProgressIndicator());
},
errorBuilder: (context, error, stackTrace) {
return const Icon(Icons.broken_image_outlined);
},
)If you do not have a CPDFDocument instance yet, create a thumbnail from a local PDF file path.
Image(
image: CPDFPageThumbnailProvider.file(
filePath: '/path/to/sample.pdf',
pageIndex: 0,
password: '',
options: const CPDFPageThumbnailOptions(width: 240),
),
)When a document is already open in your app, prefer the document source.
Use CPDFPageThumbnailOptions to control thumbnail size, background, image format, and caching.
const options = CPDFPageThumbnailOptions(
width: 600,
backgroundColor: Colors.white,
drawAnnot: true,
drawForm: true,
compression: CPDFPageCompression.jpeg,
jpegQuality: 85,
cachePolicy: CPDFPageThumbnailCachePolicy.memoryAndDisk,
);| Parameter | Description |
|---|---|
width / height | Thumbnail target size in pixels; if only one dimension is set, the other is calculated from the page aspect ratio |
scale | Scale factor applied to the original page size when width and height are unset |
backgroundColor | Page background color |
drawAnnot | Whether to render annotations on the thumbnail |
drawForm | Whether to render form widgets on the thumbnail |
compression | Image format; supports png and jpeg |
jpegQuality | JPEG quality, from 1 to 100 |
cachePolicy | Thumbnail cache strategy |
diskCacheTtl | Disk cache time-to-live |
maxPixelCount | Maximum number of pixels allowed per thumbnail |
In a thumbnail list, always set an explicit width or height to avoid excessively large renders.
Thumbnails are cached by default to reduce re-rendering and improve scrolling performance.
| Policy | Description |
|---|---|
memoryOnly | Memory cache only, no disk caching |
memoryAndDisk | Default. Memory and disk caching |
diskOnly | Disk caching only |
noCache | No SDK-level caching |
The cache key is derived from the document source, page index, page rotation, render size, background color, annotation/form flags, and image format. When the page is rotated or render parameters change, a new cache entry is used automatically.
Disk cache is stored under ComPDFKit.getTemporaryDirectory():
<temporaryDirectory>/compdfkit_page_thumbnails/Cache file names are hashed and do not contain the original page index or image extension. The content is not encrypted.
await CPDFPageThumbnailService.shared.clearCache();If the document content changes without a file modification time update (for example, edits made in memory), use cacheVersion to force a refresh.
var cacheVersion = 0;
final provider = CPDFPageThumbnailProvider.document(
document: document,
pageIndex: pageIndex,
cacheVersion: cacheVersion,
);
// Increment the version when the document changes in memory.
cacheVersion++;When the page rotation changes, the cache automatically distinguishes between different rotation angles.
Thumbnail loading may fail due to an invalid file path, incorrect password, invalid page index, or render error. Always provide an error fallback.
Image(
image: CPDFPageThumbnailProvider.document(
document: document,
pageIndex: pageIndex,
),
errorBuilder: (context, error, stackTrace) {
return const Icon(Icons.broken_image_outlined);
},
)CPDFPageThumbnail also accepts an errorBuilder:
CPDFPageThumbnail.document(
document: document,
pageIndex: pageIndex,
errorBuilder: (context, error, stackTrace) {
return const Text('Thumbnail failed');
},
)CPDFPageThumbnailProvider.document or CPDFPageThumbnail.document.CPDFReaderWidgetController, use controller.document as the document source.width or height in a thumbnail list.memoryAndDisk) for long lists to avoid repeated rendering.cacheVersion to refresh thumbnails after document changes that are not saved to disk.errorBuilder so that the UI provides clear visual feedback when loading fails.