Files
obsidian-typst-pdf-export/src/converters/pdf/PdfProcessor.ts
T
Jarvis 9345198337 refactor: clean up ESLint warnings and code quality
- Remove unused imports and variables across multiple files
- Fix prefer-const warnings by changing let to const
- Remove unused catch block variables or convert to parameter-less catch blocks
- Update package-lock.json with TypeScript-ESLint v8 dependencies
- Reduce ESLint warnings from 42 to 22 (remaining are necessary 'any' types)
- All remaining warnings are for Obsidian API compatibility

Fixes improve code quality while maintaining functionality.
Remaining 'any' type warnings are appropriate for Obsidian plugin development.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-10 17:47:17 +02:00

108 lines
3.0 KiB
TypeScript

/**
* 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 {
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: unknown) {
const errorMessage = error instanceof Error ? error.message : String(error);
return {
pageCount: 0,
dimensions: { width: 0, height: 0 },
success: false,
error: errorMessage
};
}
}
/**
* 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 {
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);
}
}