* Export as PDF * Uses high-resolution PNG embedded in PDF for print quality
(
element: HTMLElement,
width: number,
height: number,
viewport: { x: number; y: number; zoom: number },
options: Required<ExportOptions>
)
| 145 | * Uses high-resolution PNG embedded in PDF for print quality |
| 146 | */ |
| 147 | async function exportToPdf( |
| 148 | element: HTMLElement, |
| 149 | width: number, |
| 150 | height: number, |
| 151 | viewport: { x: number; y: number; zoom: number }, |
| 152 | options: Required<ExportOptions> |
| 153 | ): Promise<void> { |
| 154 | // Generate high-resolution PNG first |
| 155 | const pngDataUrl = await toPng(element, { |
| 156 | backgroundColor: options.backgroundColor, |
| 157 | width: width, |
| 158 | height: height, |
| 159 | pixelRatio: options.scale, |
| 160 | style: { |
| 161 | width: `${width}px`, |
| 162 | height: `${height}px`, |
| 163 | transform: `translate(${viewport.x}px, ${viewport.y}px) scale(${viewport.zoom})`, |
| 164 | border: 'none', |
| 165 | outline: 'none', |
| 166 | }, |
| 167 | filter: createNodeFilter(), |
| 168 | }); |
| 169 | |
| 170 | // Create PDF |
| 171 | const orientation = width > height ? 'landscape' : 'portrait'; |
| 172 | const pdf = new jsPDF({ |
| 173 | orientation, |
| 174 | unit: 'px', |
| 175 | format: [width, height], |
| 176 | hotfixes: ['px_scaling'], // Fix pixel scaling issues |
| 177 | }); |
| 178 | |
| 179 | // Add image to PDF |
| 180 | pdf.addImage(pngDataUrl, 'PNG', 0, 0, width, height); |
| 181 | |
| 182 | // Save PDF |
| 183 | pdf.save(`${options.fileName}.pdf`); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Trigger file download |
no test coverage detected