BREAKING CHANGE(smartpdf): improve image generation quality and API consistency
- Renamed convertPDFToWebpPreviews to convertPDFToWebpBytes for consistency - Added configurable scale options with DPI support - Changed default scale to 3.0 (216 DPI) for better quality - Added DPI helper methods and scale constants
This commit is contained in:
107
readme.md
107
readme.md
@@ -179,7 +179,7 @@ async function extractTextFromPdf() {
|
||||
```
|
||||
|
||||
### Converting PDF to PNG Images
|
||||
Convert each page of a PDF into PNG images:
|
||||
Convert each page of a PDF into PNG images with configurable quality:
|
||||
|
||||
```typescript
|
||||
async function convertPdfToPng() {
|
||||
@@ -189,9 +189,16 @@ async function convertPdfToPng() {
|
||||
// Load a PDF
|
||||
const pdf = await smartPdf.readFileToPdfObject('./document.pdf');
|
||||
|
||||
// Convert to PNG images (one per page)
|
||||
// Convert to PNG images with default high quality (216 DPI)
|
||||
const pngImages: Uint8Array[] = await smartPdf.convertPDFToPngBytes(pdf.buffer);
|
||||
|
||||
// Or specify custom scale/DPI
|
||||
const highResPngs = await smartPdf.convertPDFToPngBytes(pdf.buffer, {
|
||||
scale: SmartPdf.SCALE_PRINT, // 6.0 scale = ~432 DPI
|
||||
maxWidth: 3000, // Optional: limit maximum width
|
||||
maxHeight: 4000 // Optional: limit maximum height
|
||||
});
|
||||
|
||||
// Save each page as a PNG
|
||||
pngImages.forEach((pngBuffer, index) => {
|
||||
fs.writeFileSync(`page-${index + 1}.png`, pngBuffer);
|
||||
@@ -201,6 +208,102 @@ async function convertPdfToPng() {
|
||||
}
|
||||
```
|
||||
|
||||
#### Understanding Scale and DPI
|
||||
PDF.js renders at 72 DPI by default. Use these scale factors for different quality levels:
|
||||
- `SmartPdf.SCALE_SCREEN` (2.0): ~144 DPI - Good for screen display
|
||||
- `SmartPdf.SCALE_HIGH` (3.0): ~216 DPI - High quality (default)
|
||||
- `SmartPdf.SCALE_PRINT` (6.0): ~432 DPI - Print quality
|
||||
- Custom DPI: `scale = SmartPdf.getScaleForDPI(300)` for 300 DPI
|
||||
|
||||
### Converting PDF to WebP Images
|
||||
Generate web-optimized images using WebP format. WebP provides 25-35% better compression than PNG/JPEG while maintaining quality:
|
||||
|
||||
```typescript
|
||||
async function createWebPImages() {
|
||||
const smartPdf = await SmartPdf.create();
|
||||
await smartPdf.start();
|
||||
|
||||
// Load a PDF
|
||||
const pdf = await smartPdf.readFileToPdfObject('./document.pdf');
|
||||
|
||||
// Create high-quality WebP images (default: 3.0 scale = 216 DPI, 85% quality)
|
||||
const webpImages = await smartPdf.convertPDFToWebpBytes(pdf.buffer);
|
||||
|
||||
// Save WebP images
|
||||
webpImages.forEach((webpBuffer, index) => {
|
||||
fs.writeFileSync(`page-${index + 1}.webp`, webpBuffer);
|
||||
});
|
||||
|
||||
await smartPdf.stop();
|
||||
}
|
||||
```
|
||||
|
||||
#### Creating Thumbnails
|
||||
Generate small thumbnail images for PDF galleries or document lists:
|
||||
|
||||
```typescript
|
||||
async function createThumbnails() {
|
||||
const smartPdf = await SmartPdf.create();
|
||||
await smartPdf.start();
|
||||
|
||||
const pdf = await smartPdf.readFileToPdfObject('./document.pdf');
|
||||
|
||||
// Create small thumbnails (0.5 scale = ~36 DPI, 70% quality)
|
||||
const thumbnails = await smartPdf.convertPDFToWebpBytes(pdf.buffer, {
|
||||
scale: 0.5, // Small readable thumbnails
|
||||
quality: 70 // Lower quality for smaller files
|
||||
});
|
||||
|
||||
// Save thumbnails
|
||||
thumbnails.forEach((thumb, index) => {
|
||||
fs.writeFileSync(`thumb-${index + 1}.webp`, thumb);
|
||||
});
|
||||
|
||||
await smartPdf.stop();
|
||||
}
|
||||
```
|
||||
|
||||
#### Constrained Dimensions
|
||||
Create previews with maximum width/height constraints, useful for responsive layouts:
|
||||
|
||||
```typescript
|
||||
async function createConstrainedPreviews() {
|
||||
const smartPdf = await SmartPdf.create();
|
||||
await smartPdf.start();
|
||||
|
||||
const pdf = await smartPdf.readFileToPdfObject('./document.pdf');
|
||||
|
||||
// Create previews that fit within 800x600 pixels
|
||||
const previews = await smartPdf.convertPDFToWebpBytes(pdf.buffer, {
|
||||
scale: 1.0, // Start with full size
|
||||
quality: 90, // High quality
|
||||
maxWidth: 800, // Maximum 800px wide
|
||||
maxHeight: 600 // Maximum 600px tall
|
||||
});
|
||||
|
||||
// The method automatically scales down to fit within constraints
|
||||
previews.forEach((preview, index) => {
|
||||
fs.writeFileSync(`preview-constrained-${index + 1}.webp`, preview);
|
||||
});
|
||||
|
||||
await smartPdf.stop();
|
||||
}
|
||||
```
|
||||
|
||||
#### WebP Options
|
||||
The `convertPDFToWebpBytes` method accepts these options:
|
||||
|
||||
- `scale`: Scale factor for preview size (default: 3.0 for ~216 DPI)
|
||||
- `quality`: WebP compression quality (default: 85, range: 0-100)
|
||||
- `maxWidth`: Maximum width in pixels (optional)
|
||||
- `maxHeight`: Maximum height in pixels (optional)
|
||||
|
||||
Common scale values:
|
||||
- `0.5`: Thumbnails (~36 DPI)
|
||||
- `2.0`: Screen display (~144 DPI)
|
||||
- `3.0`: High quality (~216 DPI, default)
|
||||
- `6.0`: Print quality (~432 DPI)
|
||||
|
||||
### Using External Browser Instance
|
||||
For advanced use cases, you can provide your own Puppeteer browser instance:
|
||||
|
||||
|
Reference in New Issue
Block a user