dom-to-vector-pdf
Version:
Convert DOM elements to vector PDFs using jsPDF, dom-to-svg and svg2pdf.js
118 lines (90 loc) • 3.13 kB
Markdown
A tool for converting DOM elements to vector PDFs using jsPDF, dom-to-svg and svg2pdf.js.
```bash
npm install dom-to-vector-pdf
```
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| id | string | required | DOM element ID to export |
| filename | string | required | Exported PDF file name |
| orientation | 'portrait' \| 'landscape' | 'portrait' | PDF orientation |
| unit | 'px' | Unit for measurements(only px) |
| beforeSvgConvert | (svgElement: SVGElement) => void | - | Custom hook for processing SVG elements |
| beforePdfSave | (pdf: jsPDF) => void | - | Custom hook for processing PDF document |
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| font | string | required | Font file path or URL |
| fontId | string | required | Font ID for identifying the font |
| fontStyle | 'normal' \| 'italic' | 'normal' | Font style |
| fontWeight | string \| number | - | Font weight (100-900) |
### Lifecycle Hooks
| Hook | Type | Description |
|------|------|-------------|
| afterDomClone | (clonedElement: HTMLElement) => void | Triggered after DOM clone |
| beforeSvgConvert | (svgElement: SVGElement) => void | Triggered before SVG conversion |
| beforePdfGenerate | (pdf: jsPDF) => void | Triggered before PDF generation |
| beforePdfSave | (pdf: jsPDF) => void | Triggered before PDF save |
```javascript
import vectorInstance from "dom-to-vector-pdf";
export const ExportToPDF = (id, title) => {
vectorInstance.registerFont([
{
font: PingFangRegular,
fontId: "PingFang",
fontWeight: "400",
fontStyle: "normal",
},
{
font: PingFangHeavy,
fontId: "PingFang",
fontWeight: "700",
fontStyle: "normal",
},
]);
vectorInstance.exportPDF({
id,
filename: title,
});
};
```
- Converts DOM elements to vector PDFs
- Preserves vector graphics and text
- Supports SVG elements
- Maintains font styles and weights
- Handles complex layouts
- [ ] Inline style handling
- [ ] Style priority management
- [ ] Shadow DOM support
- [ ] iframe support
- [ ] Current implementation uses 16px as base font size for scaling
- [ ] Need to improve icon font size handling
- [ ] Currently only supports inline styles where property names match element attributes
- [ ] Need to enhance SVG style handling
- [ ] Text appears slightly lower than background
- Current workaround: Shift all text up by 3 pixels
- [ ✅ ] Image background export
- [ ] Canvas export
- [ ] other unit
- [ ] Currently limited to single font family
- Font ID must be consistent during registration
- [ ] Need to add support for multiple fonts
- [ ] Consider WOFF2 format compatibility
- [ ] Image export quality needs improvement
Contributions are welcome! Please feel free to submit a Pull Request.
MIT