(
htmlContent: string,
options: ExportOptions = {}
)
| 10 | * Generate PDF from HTML content using jsPDF and html2canvas |
| 11 | */ |
| 12 | export const generatePDF = async ( |
| 13 | htmlContent: string, |
| 14 | options: ExportOptions = {} |
| 15 | ): Promise<void> => { |
| 16 | const { |
| 17 | filename = 'DataKit_Export', |
| 18 | title = 'DataKit Export', |
| 19 | includeTimestamp = true |
| 20 | } = options; |
| 21 | |
| 22 | try { |
| 23 | // Dynamic imports |
| 24 | const jsPDF = (await import('jspdf')).default; |
| 25 | const html2canvas = (await import('html2canvas')).default; |
| 26 | |
| 27 | // Create temporary container for PDF generation |
| 28 | const tempContainer = document.createElement('div'); |
| 29 | tempContainer.style.position = 'absolute'; |
| 30 | tempContainer.style.left = '-9999px'; |
| 31 | tempContainer.style.width = '794px'; // A4 width in pixels |
| 32 | tempContainer.style.backgroundColor = '#ffffff'; |
| 33 | tempContainer.style.color = '#000000'; |
| 34 | tempContainer.style.fontFamily = 'Arial, sans-serif'; |
| 35 | tempContainer.style.padding = '40px'; |
| 36 | tempContainer.style.lineHeight = '1.4'; |
| 37 | |
| 38 | // Add title |
| 39 | const titleElement = document.createElement('h1'); |
| 40 | titleElement.textContent = title; |
| 41 | titleElement.style.fontSize = '24px'; |
| 42 | titleElement.style.marginBottom = '20px'; |
| 43 | titleElement.style.color = '#000000'; |
| 44 | titleElement.style.fontWeight = 'bold'; |
| 45 | titleElement.style.textAlign = 'center'; |
| 46 | titleElement.style.borderBottom = '2px solid #000000'; |
| 47 | titleElement.style.paddingBottom = '10px'; |
| 48 | tempContainer.appendChild(titleElement); |
| 49 | |
| 50 | // Add timestamp if requested |
| 51 | if (includeTimestamp) { |
| 52 | const dateDiv = document.createElement('div'); |
| 53 | dateDiv.textContent = `Generated on: ${new Date().toLocaleDateString()} with DataKit`; |
| 54 | dateDiv.style.fontSize = '12px'; |
| 55 | dateDiv.style.color = '#666666'; |
| 56 | dateDiv.style.textAlign = 'center'; |
| 57 | dateDiv.style.marginBottom = '30px'; |
| 58 | tempContainer.appendChild(dateDiv); |
| 59 | } |
| 60 | |
| 61 | // Add main content |
| 62 | const contentDiv = document.createElement('div'); |
| 63 | contentDiv.innerHTML = htmlContent; |
| 64 | tempContainer.appendChild(contentDiv); |
| 65 | |
| 66 | document.body.appendChild(tempContainer); |
| 67 | |
| 68 | // Generate canvas from the container |
| 69 | const canvas = await html2canvas(tempContainer, { |
no outgoing calls
no test coverage detected