refactor: extract PDF processing utilities to PdfProcessor.ts
Extract PDF validation and metadata operations from PdfToImageConverter to improve modularity and separation of concerns. Changes: - Create src/converters/pdf/PdfProcessor.ts with PDF utilities - Move isValidPdf() and getPdfInfo() methods with full implementations - Add validatePdfFile() and isValidPdfFile() helper methods - Update PdfToImageConverter.ts to delegate to PdfProcessor - Maintain backward compatibility for all existing method signatures 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
3 files changed
+121
-49
No files matched your search
@@ -5,6 +5,7 @@
|
||||
|
||||
import * as fs from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
import { PdfProcessor } from './pdf/PdfProcessor';
|
||||
|
||||
export interface PdfConversionOptions {
|
||||
/** Quality/scale factor for the rendered image (default: 2.0 for HiDPI) */
|
||||
@@ -91,16 +92,15 @@ export class PdfToImageConverter {
|
||||
quality: options.quality || 90
|
||||
};
|
||||
|
||||
// Ensure the PDF file exists
|
||||
try {
|
||||
await fs.access(pdfPath);
|
||||
} catch (error) {
|
||||
// Validate PDF file exists and is valid
|
||||
const pdfValidation = await PdfProcessor.validatePdfFile(pdfPath);
|
||||
if (!pdfValidation.success) {
|
||||
return {
|
||||
imagePath: '',
|
||||
originalDimensions: { width: 0, height: 0 },
|
||||
imageDimensions: { width: 0, height: 0 },
|
||||
success: false,
|
||||
error: `PDF file not found: ${pdfPath}`
|
||||
error: pdfValidation.error
|
||||
};
|
||||
}
|
||||
|
||||
@@ -393,15 +393,7 @@ export class PdfToImageConverter {
|
||||
* Check if a file is a valid PDF
|
||||
*/
|
||||
public async isValidPdf(filePath: string): Promise<boolean> {
|
||||
try {
|
||||
const buffer = await fs.readFile(filePath);
|
||||
|
||||
// Check PDF signature
|
||||
const signature = buffer.subarray(0, 4).toString();
|
||||
return signature === '%PDF';
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
return await PdfProcessor.isValidPdf(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -413,33 +405,6 @@ export class PdfToImageConverter {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}> {
|
||||
try {
|
||||
// pdf-to-img doesn't provide metadata directly
|
||||
// We'll use a basic approach with the PDF signature check
|
||||
const isValid = await this.isValidPdf(filePath);
|
||||
if (!isValid) {
|
||||
return {
|
||||
pageCount: 0,
|
||||
dimensions: { width: 0, height: 0 },
|
||||
success: false,
|
||||
error: 'Invalid PDF file'
|
||||
};
|
||||
}
|
||||
|
||||
// For now, return basic info since pdf-to-img focuses on conversion
|
||||
// In a real implementation, you might want to use pdf-parse or similar for metadata
|
||||
return {
|
||||
pageCount: 1, // We only handle first page anyway
|
||||
dimensions: { width: 612, height: 792 }, // Standard letter size estimate
|
||||
success: true
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
pageCount: 0,
|
||||
dimensions: { width: 0, height: 0 },
|
||||
success: false,
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
return await PdfProcessor.getPdfInfo(filePath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* PDF Processing utilities for validation and metadata extraction.
|
||||
* Handles PDF-specific operations separate from image conversion.
|
||||
*/
|
||||
|
||||
import * as fs from 'fs/promises';
|
||||
|
||||
export interface PdfMetadata {
|
||||
/** Number of pages in the PDF */
|
||||
pageCount: number;
|
||||
/** PDF dimensions (width x height) */
|
||||
dimensions: { width: number; height: number };
|
||||
/** Operation success status */
|
||||
success: boolean;
|
||||
/** Error message if operation failed */
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export class PdfProcessor {
|
||||
/**
|
||||
* Check if a file is a valid PDF by examining its signature.
|
||||
* @param filePath Path to the file to validate
|
||||
* @returns True if the file is a valid PDF, false otherwise
|
||||
*/
|
||||
public static async isValidPdf(filePath: string): Promise<boolean> {
|
||||
try {
|
||||
const buffer = await fs.readFile(filePath);
|
||||
|
||||
// Check PDF signature - all valid PDFs must start with %PDF
|
||||
const signature = buffer.subarray(0, 4).toString();
|
||||
return signature === '%PDF';
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get PDF metadata including page count and dimensions.
|
||||
* @param filePath Path to the PDF file
|
||||
* @returns PDF metadata with page count, dimensions, and status
|
||||
*/
|
||||
public static async getPdfInfo(filePath: string): Promise<PdfMetadata> {
|
||||
try {
|
||||
// First validate that this is a proper PDF file
|
||||
const isValid = await this.isValidPdf(filePath);
|
||||
if (!isValid) {
|
||||
return {
|
||||
pageCount: 0,
|
||||
dimensions: { width: 0, height: 0 },
|
||||
success: false,
|
||||
error: 'Invalid PDF file'
|
||||
};
|
||||
}
|
||||
|
||||
// For now, return basic info since we use pdf-to-img for conversion
|
||||
// In a real implementation, you might want to use pdf-parse or similar for metadata
|
||||
// We only handle first page anyway in the conversion process
|
||||
return {
|
||||
pageCount: 1, // We only handle first page anyway
|
||||
dimensions: { width: 612, height: 792 }, // Standard letter size estimate
|
||||
success: true
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
pageCount: 0,
|
||||
dimensions: { width: 0, height: 0 },
|
||||
success: false,
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a PDF file exists and is accessible.
|
||||
* @param filePath Path to the PDF file
|
||||
* @returns Object with success status and error message if applicable
|
||||
*/
|
||||
public static async validatePdfFile(filePath: string): Promise<{
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}> {
|
||||
try {
|
||||
await fs.access(filePath);
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: `PDF file not found: ${filePath}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a file exists and is a valid PDF.
|
||||
* Combines file existence check with PDF validation.
|
||||
* @param filePath Path to the file to check
|
||||
* @returns True if file exists and is a valid PDF, false otherwise
|
||||
*/
|
||||
public static async isValidPdfFile(filePath: string): Promise<boolean> {
|
||||
const validation = await this.validatePdfFile(filePath);
|
||||
if (!validation.success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return await this.isValidPdf(filePath);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user