|
| 1 | +# Browser Migration Summary |
| 2 | + |
| 3 | +## 🎉 What's New |
| 4 | + |
| 5 | +PDFStudio v0.2.0 is now **isomorphic** - it works in both Node.js AND browsers! |
| 6 | + |
| 7 | +## 🏗️ Architecture Changes |
| 8 | + |
| 9 | +### Platform Abstraction Layer |
| 10 | + |
| 11 | +Created a new platform abstraction layer to support multiple environments: |
| 12 | + |
| 13 | +``` |
| 14 | +src/ |
| 15 | +├── platform/ |
| 16 | +│ ├── interfaces/ # Abstract interfaces |
| 17 | +│ │ ├── IFileSystem.ts |
| 18 | +│ │ └── IImageProcessor.ts |
| 19 | +│ ├── node/ # Node.js implementations |
| 20 | +│ │ ├── NodeFileSystem.ts |
| 21 | +│ │ └── NodeImageProcessor.ts |
| 22 | +│ ├── browser/ # Browser implementations |
| 23 | +│ │ ├── BrowserFileSystem.ts |
| 24 | +│ │ └── BrowserImageProcessor.ts |
| 25 | +│ └── PlatformFactory.ts # Auto-detects environment |
| 26 | +``` |
| 27 | + |
| 28 | +### Key Changes |
| 29 | + |
| 30 | +#### 1. Image Processing |
| 31 | +- **Node.js**: Still uses `sharp` for optimal performance |
| 32 | +- **Browser**: Uses Canvas API for image processing |
| 33 | +- **Interface**: `IImageProcessor` provides unified API |
| 34 | + |
| 35 | +#### 2. File System |
| 36 | +- **Node.js**: Uses native `fs` module |
| 37 | +- **Browser**: Uses FileReader API, fetch API, and Blob API |
| 38 | +- **Interface**: `IFileSystem` provides unified API |
| 39 | + |
| 40 | +#### 3. Async APIs |
| 41 | +Several methods are now async to support browser file loading: |
| 42 | + |
| 43 | +```typescript |
| 44 | +// Before (Node.js only) |
| 45 | +doc.registerFont({ name: 'MyFont', source: './font.ttf' }); |
| 46 | +doc.save('output.pdf'); |
| 47 | + |
| 48 | +// After (Node.js + Browser) |
| 49 | +await doc.registerFont({ name: 'MyFont', source: './font.ttf' }); |
| 50 | +await doc.save('output.pdf'); |
| 51 | +``` |
| 52 | + |
| 53 | +### Breaking Changes |
| 54 | + |
| 55 | +#### Async Methods |
| 56 | + |
| 57 | +The following methods are now `async`: |
| 58 | + |
| 59 | +- `PDFDocument.registerFont()` → `async registerFont()` |
| 60 | +- `PDFDocument.save()` → `async save()` |
| 61 | +- `PDFDocument.toBuffer()` → `async toBuffer()` |
| 62 | +- `PDFDocument.end()` → `async end()` |
| 63 | +- `PDFWriter.registerCustomFont()` → `async registerCustomFont()` |
| 64 | +- `PDFWriter.generate()` → `async generate()` |
| 65 | +- `PDFWriter.embedImage()` → `async embedImage()` |
| 66 | +- `ImageParser.load()` → `async load()` |
| 67 | + |
| 68 | +#### Migration Example |
| 69 | + |
| 70 | +**Before (v0.1.x):** |
| 71 | +```typescript |
| 72 | +const doc = new PDFDocument(); |
| 73 | +doc.registerFont({ name: 'MyFont', source: './font.ttf' }); |
| 74 | +doc.text('Hello', 100, 100); |
| 75 | +doc.save('output.pdf'); |
| 76 | +``` |
| 77 | + |
| 78 | +**After (v0.2.x):** |
| 79 | +```typescript |
| 80 | +const doc = new PDFDocument(); |
| 81 | +await doc.registerFont({ name: 'MyFont', source: './font.ttf' }); |
| 82 | +doc.text('Hello', 100, 100); |
| 83 | +await doc.save('output.pdf'); |
| 84 | +``` |
| 85 | + |
| 86 | +### Dependencies Changes |
| 87 | + |
| 88 | +#### New Dependencies |
| 89 | +- `buffer` - Buffer polyfill for browsers |
| 90 | +- `esbuild` - Build tool for browser bundle |
| 91 | + |
| 92 | +#### Modified Dependencies |
| 93 | +- `sharp` - Moved to `optionalDependencies` (not required in browsers) |
| 94 | + |
| 95 | +### Build System |
| 96 | + |
| 97 | +New build scripts: |
| 98 | + |
| 99 | +```json |
| 100 | +{ |
| 101 | + "scripts": { |
| 102 | + "build": "npm run build:node && npm run build:browser", |
| 103 | + "build:node": "tsc", |
| 104 | + "build:browser": "node scripts/build-browser.js" |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +Output files: |
| 110 | +- `dist/index.js` - Node.js version (CommonJS) |
| 111 | +- `dist/pdfstudio.standalone.js` - Browser version (IIFE, minified) |
| 112 | +- `dist/pdfstudio.standalone.debug.js` - Browser version (IIFE, debug) |
| 113 | + |
| 114 | +## 🚀 New Features |
| 115 | + |
| 116 | +### Browser Support |
| 117 | +- ✅ Generate PDFs directly in the browser |
| 118 | +- ✅ No server required |
| 119 | +- ✅ Same API as Node.js |
| 120 | +- ✅ Modern browsers (Chrome, Firefox, Safari, Edge) |
| 121 | + |
| 122 | +### File Handling |
| 123 | +- ✅ Load images from URLs |
| 124 | +- ✅ Load images from File objects (`<input type="file">`) |
| 125 | +- ✅ Load images from data URLs |
| 126 | +- ✅ Load fonts from URLs or File objects |
| 127 | +- ✅ Download PDFs or get as Blob |
| 128 | + |
| 129 | +### Browser-Specific Features |
| 130 | +```javascript |
| 131 | +// Download PDF |
| 132 | +await doc.save('output.pdf'); // Triggers download |
| 133 | + |
| 134 | +// Get as Blob (for upload, viewing, etc.) |
| 135 | +const buffer = await doc.toBuffer(); |
| 136 | +const blob = new Blob([buffer], { type: 'application/pdf' }); |
| 137 | +const url = URL.createObjectURL(blob); |
| 138 | + |
| 139 | +// View in browser |
| 140 | +window.open(url, '_blank'); |
| 141 | + |
| 142 | +// Upload to server |
| 143 | +const formData = new FormData(); |
| 144 | +formData.append('pdf', blob, 'document.pdf'); |
| 145 | +await fetch('/api/upload', { method: 'POST', body: formData }); |
| 146 | +``` |
| 147 | + |
| 148 | +## 📚 Documentation |
| 149 | + |
| 150 | +New documentation: |
| 151 | +- `BROWSER.md` - Complete browser guide |
| 152 | +- `examples/browser/` - Browser examples |
| 153 | +- `examples/browser/README.md` - Browser examples documentation |
| 154 | +- Updated main README with browser section |
| 155 | + |
| 156 | +## 🔄 Migration Checklist |
| 157 | + |
| 158 | +For existing users upgrading from v0.1.x to v0.2.x: |
| 159 | + |
| 160 | +- [ ] Update all `doc.save()` calls to `await doc.save()` |
| 161 | +- [ ] Update all `doc.registerFont()` calls to `await doc.registerFont()` |
| 162 | +- [ ] Update all `doc.toBuffer()` calls to `await doc.toBuffer()` |
| 163 | +- [ ] Update all `doc.end()` calls to `await doc.end()` |
| 164 | +- [ ] Update all `ImageParser.load()` calls to `await ImageParser.load()` |
| 165 | +- [ ] Wrap affected code in `async` functions |
| 166 | +- [ ] Test your application |
| 167 | + |
| 168 | +### Example Migration |
| 169 | + |
| 170 | +**Before:** |
| 171 | +```typescript |
| 172 | +function generateInvoice() { |
| 173 | + const doc = new PDFDocument(); |
| 174 | + doc.registerFont({ name: 'Arial', source: './arial.ttf' }); |
| 175 | + doc.text('Invoice', 100, 100); |
| 176 | + doc.save('invoice.pdf'); |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +**After:** |
| 181 | +```typescript |
| 182 | +async function generateInvoice() { |
| 183 | + const doc = new PDFDocument(); |
| 184 | + await doc.registerFont({ name: 'Arial', source: './arial.ttf' }); |
| 185 | + doc.text('Invoice', 100, 100); |
| 186 | + await doc.save('invoice.pdf'); |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +## 🎯 Benefits |
| 191 | + |
| 192 | +1. **Universal**: Write once, run anywhere (Node.js + Browser) |
| 193 | +2. **No Breaking API**: Same API for both environments |
| 194 | +3. **Better Performance**: Optimized implementations for each platform |
| 195 | +4. **Modern**: Uses latest browser APIs (Canvas, Blob, Web Crypto) |
| 196 | +5. **Future-Proof**: Foundation for other platforms (React Native, Electron) |
| 197 | + |
| 198 | +## 🐛 Known Limitations |
| 199 | + |
| 200 | +### Browser Version |
| 201 | +1. **Memory**: Large PDFs (>50MB) may hit browser memory limits |
| 202 | +2. **Performance**: Image processing slightly slower than Node.js (Canvas vs sharp) |
| 203 | +3. **Sync Crypto**: Some crypto operations are async in browsers |
| 204 | + |
| 205 | +## 📞 Support |
| 206 | + |
| 207 | +- 📖 [Browser Documentation](BROWSER.md) |
| 208 | +- 📖 [Main Documentation](README.md) |
| 209 | +- 💬 [GitHub Issues](https://github.com/pdfstudio-dev/pdfstudio/issues) |
| 210 | +- 📧 Email: hello@ideas2code.dev |
0 commit comments