dom-to-vector-pdf
Version:
Convert DOM elements to vector PDFs using jsPDF, dom-to-svg and svg2pdf.js
95 lines (70 loc) • 2.11 kB
Markdown
[中文](README.zh-CN.md) | [English](README.md)
A tool for converting DOM elements to vector PDFs using jsPDF, dom-to-svg and svg2pdf.js.
[](https://dom-to-vector-pdf.xzboss.cn/vue-example)
[](https://dom-to-vector-pdf.xzboss.cn/docs)
```bash
npm install dom-to-vector-pdf
```
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| selector | string | required | CSS selector for the DOM element to export |
| filename | string | required | Exported PDF file name |
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| font | string | required | Font file path or URL |
| 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 fontTTF from '@/assets/your-font.ttf'
import vectorInstance from "dom-to-vector-pdf";
export const ExportToPDF = (selector, title) => {
vectorInstance.registerFont([
{
font: fontTTF,
fontWeight: "400",
fontStyle: "normal",
},
{
font: fontTTF,
fontWeight: "700",
fontStyle: "normal",
},
]);
vectorInstance.exportPDF({
selector,
filename: title,
});
};
```
- Converts DOM elements to vector PDFs
- Preserves vector graphics and text
- Supports SVG elements
- Maintains font styles and weights
- Handles complex layouts
```
pnpm i
```
```
pnpm dev:vue
```
Contributions are welcome! Please feel free to submit a Pull Request.
MIT