|
22 | 22 |
|
23 | 23 | ## 📋 Prerequisites |
24 | 24 |
|
25 | | -- **Node.js** >= 14.0.0 |
| 25 | +- **Node.js** >= 12.0.0 |
26 | 26 | - **Java Runtime Environment (JRE)** >= 8 |
27 | 27 | - Required for Apache Tika and PDFBox |
28 | 28 | - [Download Java](https://www.java.com/en/download/) |
@@ -142,6 +142,131 @@ const thumbnailPath = await pdf2html.thumbnail(pdfBuffer, { |
142 | 142 | }); |
143 | 143 | ``` |
144 | 144 |
|
| 145 | +## 💻 TypeScript Support |
| 146 | + |
| 147 | +This package includes TypeScript type definitions out of the box. No need to install `@types/pdf2html`. |
| 148 | + |
| 149 | +### Basic TypeScript Usage |
| 150 | + |
| 151 | +```typescript |
| 152 | +import * as pdf2html from 'pdf2html'; |
| 153 | +// or |
| 154 | +import { html, text, pages, meta, thumbnail, PDFMetadata, PDFProcessingError } from 'pdf2html'; |
| 155 | + |
| 156 | +async function convertPDF() { |
| 157 | + try { |
| 158 | + // All methods accept string paths or Buffers |
| 159 | + const htmlContent: string = await pdf2html.html('document.pdf'); |
| 160 | + const textContent: string = await pdf2html.text(Buffer.from(pdfData)); |
| 161 | + |
| 162 | + // Full type safety for options |
| 163 | + const thumbnailPath = await pdf2html.thumbnail('document.pdf', { |
| 164 | + page: 1, // number |
| 165 | + imageType: 'png', // 'png' | 'jpg' |
| 166 | + width: 300, // number |
| 167 | + height: 400, // number |
| 168 | + }); |
| 169 | + |
| 170 | + // TypeScript knows the shape of metadata |
| 171 | + const metadata: PDFMetadata = await pdf2html.meta('document.pdf'); |
| 172 | + console.log(metadata['pdf:producer']); // string | undefined |
| 173 | + console.log(metadata.resourceName); // string | undefined |
| 174 | + } catch (error) { |
| 175 | + if (error instanceof pdf2html.PDFProcessingError) { |
| 176 | + console.error('PDF processing failed:', error.message); |
| 177 | + console.error('Exit code:', error.exitCode); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +### Type Definitions |
| 184 | + |
| 185 | +```typescript |
| 186 | +// Input types - all methods accept either file paths or Buffers |
| 187 | +type PDFInput = string | Buffer; |
| 188 | + |
| 189 | +// Options interfaces |
| 190 | +interface ProcessingOptions { |
| 191 | + maxBuffer?: number; // Maximum buffer size in bytes |
| 192 | +} |
| 193 | + |
| 194 | +interface PageOptions extends ProcessingOptions { |
| 195 | + text?: boolean; // Extract text instead of HTML |
| 196 | +} |
| 197 | + |
| 198 | +interface ThumbnailOptions extends ProcessingOptions { |
| 199 | + page?: number; // Page number (default: 1) |
| 200 | + imageType?: 'png' | 'jpg'; // Image format (default: 'png') |
| 201 | + width?: number; // Width in pixels (default: 160) |
| 202 | + height?: number; // Height in pixels (default: 226) |
| 203 | +} |
| 204 | + |
| 205 | +// Metadata structure with common fields |
| 206 | +interface PDFMetadata { |
| 207 | + 'pdf:PDFVersion'?: string; |
| 208 | + 'pdf:producer'?: string; |
| 209 | + 'xmp:CreatorTool'?: string; |
| 210 | + 'dc:title'?: string; |
| 211 | + 'dc:creator'?: string; |
| 212 | + resourceName?: string; |
| 213 | + [key: string]: any; // Allows additional properties |
| 214 | +} |
| 215 | + |
| 216 | +// Error class |
| 217 | +class PDFProcessingError extends Error { |
| 218 | + command?: string; // The command that failed |
| 219 | + exitCode?: number; // The process exit code |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +### IntelliSense Support |
| 224 | + |
| 225 | +Full IntelliSense support in VS Code and other TypeScript-aware editors: |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | +- Auto-completion for all methods and options |
| 230 | +- Inline documentation on hover |
| 231 | +- Type checking at compile time |
| 232 | +- Catch errors before runtime |
| 233 | + |
| 234 | +### Advanced TypeScript Usage |
| 235 | + |
| 236 | +```typescript |
| 237 | +import { PDFProcessor, utils } from 'pdf2html'; |
| 238 | + |
| 239 | +// Using the PDFProcessor class directly |
| 240 | +const html = await PDFProcessor.toHTML('document.pdf'); |
| 241 | + |
| 242 | +// Using utility classes |
| 243 | +const { FileManager, HTMLParser } = utils; |
| 244 | +await FileManager.ensureDirectories(); |
| 245 | + |
| 246 | +// Type guards |
| 247 | +function isPDFProcessingError(error: unknown): error is pdf2html.PDFProcessingError { |
| 248 | + return error instanceof pdf2html.PDFProcessingError; |
| 249 | +} |
| 250 | + |
| 251 | +// Generic helper with proper typing |
| 252 | +async function processPDFSafely<T>(operation: () => Promise<T>, fallback: T): Promise<T> { |
| 253 | + try { |
| 254 | + return await operation(); |
| 255 | + } catch (error) { |
| 256 | + if (isPDFProcessingError(error)) { |
| 257 | + console.error(`PDF operation failed: ${error.message}`); |
| 258 | + } |
| 259 | + return fallback; |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +// Usage |
| 264 | +const pages = await processPDFSafely( |
| 265 | + () => pdf2html.pages('document.pdf', { text: true }), |
| 266 | + [] // fallback to empty array |
| 267 | +); |
| 268 | +``` |
| 269 | + |
145 | 270 | ## ⚙️ Advanced Configuration |
146 | 271 |
|
147 | 272 | ### Buffer Size Configuration |
@@ -307,7 +432,7 @@ Contributions are welcome! Please feel free to submit a Pull Request. For major |
307 | 432 |
|
308 | 433 | ## 📝 License |
309 | 434 |
|
310 | | -This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 435 | +This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details. |
311 | 436 |
|
312 | 437 | ## 🙏 Acknowledgments |
313 | 438 |
|
|
0 commit comments