- Add Node.js implementation using @push.rocks/smartpdf - Add browser implementation with PDF.js and Web Workers - Support configurable quality, dimensions, and page selection - Include comprehensive TypeScript definitions and error handling - Provide extensive test coverage for both environments - Add download functionality and browser compatibility checking
124 lines
2.1 KiB
TypeScript
124 lines
2.1 KiB
TypeScript
/**
|
|
* Configuration options for preview generation
|
|
*/
|
|
export interface IPreviewOptions {
|
|
/**
|
|
* JPEG quality (1-100)
|
|
* @default 80
|
|
*/
|
|
quality?: number;
|
|
|
|
/**
|
|
* Maximum width in pixels
|
|
*/
|
|
width?: number;
|
|
|
|
/**
|
|
* Maximum height in pixels
|
|
*/
|
|
height?: number;
|
|
|
|
/**
|
|
* PDF page number to convert (1-based)
|
|
* @default 1
|
|
*/
|
|
page?: number;
|
|
|
|
/**
|
|
* Scale factor for rendering
|
|
* @default 1.0
|
|
*/
|
|
scale?: number;
|
|
}
|
|
|
|
/**
|
|
* Preview generation result
|
|
*/
|
|
export interface IPreviewResult {
|
|
/**
|
|
* Generated JPEG image buffer
|
|
*/
|
|
buffer: Buffer;
|
|
|
|
/**
|
|
* Image dimensions
|
|
*/
|
|
dimensions: {
|
|
width: number;
|
|
height: number;
|
|
};
|
|
|
|
/**
|
|
* File size in bytes
|
|
*/
|
|
size: number;
|
|
|
|
/**
|
|
* MIME type
|
|
*/
|
|
mimeType: 'image/jpeg';
|
|
}
|
|
|
|
/**
|
|
* Supported input formats (extensible)
|
|
*/
|
|
export type TSupportedInputFormat = 'pdf';
|
|
|
|
/**
|
|
* Supported output formats (extensible)
|
|
*/
|
|
export type TSupportedOutputFormat = 'jpeg';
|
|
|
|
/**
|
|
* Base interface for format processors (extensible architecture)
|
|
*/
|
|
export interface IFormatProcessor {
|
|
/**
|
|
* Supported input format
|
|
*/
|
|
inputFormat: TSupportedInputFormat;
|
|
|
|
/**
|
|
* Supported output format
|
|
*/
|
|
outputFormat: TSupportedOutputFormat;
|
|
|
|
/**
|
|
* Process the input and generate preview
|
|
*/
|
|
processPreview(input: Buffer, options: IPreviewOptions): Promise<IPreviewResult>;
|
|
}
|
|
|
|
/**
|
|
* PDF-specific processor interface
|
|
*/
|
|
export interface IPdfProcessor extends IFormatProcessor {
|
|
inputFormat: 'pdf';
|
|
outputFormat: 'jpeg';
|
|
}
|
|
|
|
/**
|
|
* Error types for preview generation
|
|
*/
|
|
export type TPreviewError =
|
|
| 'INVALID_INPUT'
|
|
| 'UNSUPPORTED_FORMAT'
|
|
| 'PROCESSING_FAILED'
|
|
| 'INVALID_OPTIONS'
|
|
| 'PDF_CORRUPTED'
|
|
| 'PAGE_NOT_FOUND';
|
|
|
|
/**
|
|
* Custom error class for preview operations
|
|
*/
|
|
export class PreviewError extends Error {
|
|
public readonly errorType: TPreviewError;
|
|
public readonly originalError?: Error;
|
|
|
|
constructor(errorType: TPreviewError, message: string, originalError?: Error) {
|
|
super(message);
|
|
this.name = 'PreviewError';
|
|
this.errorType = errorType;
|
|
this.originalError = originalError;
|
|
}
|
|
} |