UNPKG

pdf-node

Version:

A modern, feature-rich PDF generation library for Node.js with TypeScript support. Convert HTML to PDF with Handlebars templates, buffers, and streams.

157 lines (149 loc) 4.85 kB
import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; import * as fs from 'fs'; import { generatePDF, addNewPage } from './index.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); // Test HTML template const htmlTemplate = `<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Buffer Test PDF</title> <style> body { font-family: Arial, sans-serif; margin: 40px; } .header { color: #333; border-bottom: 2px solid #007acc; padding-bottom: 10px; } .content { margin: 20px 0; line-height: 1.6; } .highlight { background-color: #f0f8ff; padding: 10px; border-left: 4px solid #007acc; } </style> </head> <body> <div class="header"> <h1>{{title}}</h1> <p>Generated for: {{user.name}}</p> </div> <div class="content"> <h2>Buffer Support Test</h2> <p>This PDF was generated using the new TypeScript version with buffer support!</p> <div class="highlight"> <strong>Key Benefits:</strong> <ul> <li>Type-safe PDF generation</li> <li>Perfect for APIs and web services</li> <li>No temporary files needed</li> <li>Direct HTTP response streaming</li> <li>Memory efficient for small to medium PDFs</li> </ul> </div> <h3>User Information</h3> <p><strong>Name:</strong> {{user.name}}</p> <p><strong>Email:</strong> {{user.email}}</p> <p><strong>Role:</strong> {{user.role}}</p> </div> ${addNewPage()} <div class="content"> <h2>Second Page</h2> <p>This content appears on a new page, demonstrating the addNewPage() function works with buffer output too!</p> <h3>TypeScript Features</h3> <ul> <li>Type checking for all function parameters</li> <li>IntelliSense and code completion</li> <li>Better documentation with JSDoc</li> <li>Improved error handling</li> </ul> <p>Generated at: {{timestamp}}</p> </div> <footer style="margin-top: 40px; padding-top: 20px; border-top: 1px solid #eee; font-size: 0.9em; color: #666;"> <p>Generated by pdf-node (TypeScript Edition)</p> </footer> </body> </html>`; // Test data const data = { title: 'TypeScript PDF Generation Test', user: { name: 'TypeScript Developer', email: 'typescript@example.com', role: 'Senior Developer' }, timestamp: new Date().toLocaleString() }; // Test buffer output async function testBufferOutput() { console.log('Testing buffer output...'); try { const result = (await generatePDF({ html: htmlTemplate, data, buffer: true, type: 'pdf', pdfOptions: { format: 'A4', border: { top: '10mm', right: '10mm', bottom: '10mm', left: '10mm' } } })); console.log('Buffer test successful!'); console.log(`Buffer size: ${result.size} bytes`); // Optionally save the buffer to a file for verification const outputPath = join(__dirname, '..', 'test-output-buffer.pdf'); fs.writeFileSync(outputPath, result.buffer); console.log(`PDF saved to: ${outputPath}`); return result; } catch (error) { console.error('Error in buffer test:', error); throw error; } } // Test file output async function testFileOutput() { console.log('Testing file output...'); const outputPath = join(__dirname, '..', 'test-output-file.pdf'); try { const result = (await generatePDF({ html: htmlTemplate, data, path: outputPath, type: 'pdf', pdfOptions: { format: 'A4', border: { top: '10mm', right: '10mm', bottom: '10mm', left: '10mm' } } })); console.log('File test successful!'); console.log(`PDF saved to: ${result.filename}`); return result; } catch (error) { console.error('Error in file test:', error); throw error; } } // Run tests async function runTests() { try { console.log('Starting TypeScript PDF generation tests...'); // Test buffer output await testBufferOutput(); // Test file output await testFileOutput(); console.log('All tests completed successfully!'); } catch (error) { console.error('Tests failed:', error); process.exit(1); } } // Run the tests runTests(); //# sourceMappingURL=test-buffer.js.map