Nodejs如何生成word或pdf?

Nodejs如何生成word或pdf?

如题,满足业务需求,生成合同啦,生成报表啦,巴拉巴拉巴拉拉~ 知道的麻烦告诉下呗~ 我只知道mht的文件可以用word打开,但是我不知道how to 生成mht啊 = =

10 回复

当然可以!Node.js 提供了多种库来帮助你生成 Word 和 PDF 文件。下面我会分别介绍如何使用这些库来生成 Word 和 PDF 文件。

生成 Word 文件

你可以使用 docx 库来生成 Word 文档。这是一个非常流行的库,可以用来创建复杂的 Word 文档。

安装依赖

首先,你需要安装 docx 库:

npm install docx

示例代码

以下是一个简单的示例代码,展示如何生成一个包含文本、段落和表格的 Word 文档:

const { Document, Packer, Paragraph, TextRun } = require('docx');

// 创建一个新的文档
const doc = new Document({
    sections: [
        {
            properties: {},
            children: [
                new Paragraph({
                    children: [
                        new TextRun('这是一个示例文档'),
                    ],
                }),
                new Paragraph({
                    children: [
                        new TextRun('这是一段正文'),
                    ],
                }),
                new Paragraph({
                    children: [
                        new TextRun('这是一张表格'),
                    ],
                }),
                new Table({
                    rows: [
                        [new TableCell({ children: [new Paragraph('第一行第一列')] }), new TableCell({ children: [new Paragraph('第一行第二列')] })],
                        [new TableCell({ children: [new Paragraph('第二行第一列')] }), new TableCell({ children: [new Paragraph('第二行第二列')] })],
                    ],
                }),
            ],
        },
    ],
});

// 打包并保存文档
Packer.toBuffer(doc).then((buffer) => {
    require('fs').writeFile(`example.docx`, buffer, (err) => {
        if (err) throw err;
        console.log('Document created successfully');
    });
});

生成 PDF 文件

生成 PDF 文件可以使用 pdfkit 库。这是一个功能强大的库,可以用来创建复杂的 PDF 文档。

安装依赖

首先,你需要安装 pdfkit 库:

npm install pdfkit

示例代码

以下是一个简单的示例代码,展示如何生成一个包含文本和图片的 PDF 文档:

const PDFDocument = require('pdfkit');
const fs = require('fs');

// 创建一个新的 PDF 文档
const doc = new PDFDocument();

// 将文档输出到文件
doc.pipe(fs.createWriteStream('output.pdf'));

// 添加文本
doc.fontSize(25).text('这是一个示例 PDF 文档', 100, 100);

// 添加图片
doc.image('path/to/your/image.png', {
    fit: [250, 300],
    align: 'center',
    valign: 'center'
});

// 结束文档
doc.end();

总结

通过上面的示例代码,你可以看到如何使用 Node.js 来生成 Word 和 PDF 文件。docx 库非常适合生成 Word 文档,而 pdfkit 库则适合生成 PDF 文档。希望这些示例对你有所帮助!


mht就是HTML格式的嘛,只是有些标记是微软特殊的标记

现在node还没有这方面的模块么

这需要一个支持pdf的库,这个库以前有人用C++写过(pdflib, clibpdf),只需要将C++代码移到nodejs下即可。当然,现在已经有人做了,如pdfkit

这个生成出来是图形吗?

赞一个pdfkit so cool! 请问您一下 word有什么办法吗? 我的思路是写一个mht的文件然后把后缀改成.doc

感觉您没有理解我的意思 由于mht可以被word打开,所示是不是可以写一个mht的文件然后把后缀改成.doc?

我npm search word 没有找到相关的

要在Node.js中生成Word或PDF文档,可以使用一些专门的库来实现。下面是两种常见的方法:生成PDF和生成Word文档。

生成PDF

为了生成PDF文件,你可以使用pdfmake库。首先需要安装该库:

npm install pdfmake

然后可以使用以下代码生成一个简单的PDF文件:

const pdfMake = require('pdfmake/build/pdfmake');
const pdfFonts = require('pdfmake/build/vfs_fonts');

pdfMake.vfs = pdfFonts.pdfMake.vfs;

const documentDefinition = {
  content: [
    { text: 'Hello World', style: 'header' },
    { text: 'This is a sample PDF created with pdfmake.', margin: [0, 15, 0, 15] }
  ],
  styles: {
    header: {
      fontSize: 18,
      bold: true
    }
  }
};

pdfMake.createPdf(documentDefinition).download('example.pdf');

这段代码创建了一个包含简单文本的PDF文件,并将其下载到用户的计算机上。

生成Word文档

对于生成Word文档,可以使用docx库。同样地,先安装该库:

npm install docx

以下是一个简单的示例,展示如何生成一个Word文档:

const { Document, Packer, Paragraph, TextRun } = require('docx');

const doc = new Document({
  sections: [
    {
      properties: {},
      children: [
        new Paragraph({
          children: [
            new TextRun('Hello World'),
          ]
        }),
        new Paragraph({
          children: [
            new TextRun('This is a sample Word document created with docx.'),
          ]
        })
      ]
    }
  ]
});

Packer.toBuffer(doc).then((buffer) => {
  require('fs').writeFile('example.docx', buffer, (err) => {
    if (err) throw err;
    console.log('Document created successfully');
  });
});

这段代码创建了一个包含简单文本的Word文档,并保存到本地文件系统中。

总结

通过上述代码示例,你可以看到,生成PDF和Word文档在Node.js中是非常直接且易于实现的任务。根据你的具体需求,可以选择合适的库来完成这一任务。希望这些示例对你有所帮助!

回到顶部