使用pdfmake将html表格转成pdf
时间: 2023-09-08 07:11:55 浏览: 16
首先,需要将HTML表格转换为pdfmake支持的格式。可以使用html-to-pdfmake库将HTML转换为pdfmake格式。然后,使用pdfmake库生成PDF文件。下面是一个示例代码:
```javascript
// 导入所需的库
const pdfMake = require('pdfmake');
const htmlToPdfmake = require('html-to-pdfmake');
const fs = require('fs');
// 将html表格转换为pdfmake格式
const html = '<table><thead><tr><th>Column 1</th><th>Column 2</th></tr></thead><tbody><tr><td>Row 1, Column 1</td><td>Row 1, Column 2</td></tr><tr><td>Row 2, Column 1</td><td>Row 2, Column 2</td></tr></tbody></table>';
const pdfmakeTable = htmlToPdfmake(html);
// 生成PDF文件
const docDefinition = {
content: [
pdfmakeTable
]
};
const pdfDoc = pdfMake.createPdf(docDefinition);
pdfDoc.pipe(fs.createWriteStream('output.pdf'));
pdfDoc.end();
```
这段代码将HTML表格转换为pdfmake格式,并将其添加到PDF文档中。然后,将该文档保存为output.pdf文件。
相关推荐


















