You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Linux环境下无需浏览器依赖,用Node.js(TypeScript)实现HTML转PDF

无需浏览器依赖的Node.js(TypeScript) HTML转PDF包推荐

1. html-to-pdfmake + pdfmake

  • 核心特点:纯JavaScript实现,无需任何外部浏览器或二进制依赖,完全基于Node.js环境运行,原生支持TypeScript。通过html-to-pdfmake将HTML转换为pdfmake兼容的文档结构,再由pdfmake生成PDF。
  • 适用场景:结构简单、仅需基础HTML标签(标题、段落、粗斜体等)的转换,不依赖复杂CSS布局。
  • TypeScript示例:
import pdfMake from 'pdfmake';
import htmlToPdfmake from 'html-to-pdfmake';
import fs from 'fs';

// 待转换的HTML内容
const htmlContent = `
  <h1>测试文档标题</h1>
  <p>这是一段支持<b>加粗</b>、<i>斜体</i>格式的普通文本。</p>
`;

// 将HTML转换为pdfmake可识别的结构
const pdfContent = htmlToPdfmake(htmlContent);

// 配置pdfmake字体(默认提供Roboto字体,也可自定义)
const fonts = {
  Roboto: {
    normal: 'node_modules/pdfmake/build/fonts/Roboto/Roboto-Regular.ttf',
    bold: 'node_modules/pdfmake/build/fonts/Roboto/Roboto-Medium.ttf',
    italics: 'node_modules/pdfmake/build/fonts/Roboto/Roboto-Italic.ttf',
    bolditalics: 'node_modules/pdfmake/build/fonts/Roboto/Roboto-MediumItalic.ttf'
  }
};

const printer = new pdfMake(fonts);
const docDefinition = { content: pdfContent };

// 生成PDF并写入文件
const pdfDoc = printer.createPdfKitDocument(docDefinition);
pdfDoc.pipe(fs.createWriteStream('output.pdf'));
pdfDoc.end();

2. wkhtmltopdf(Node封装版)

  • 核心特点:基于轻量级WebKit渲染器wkhtmltopdf,体积远小于Chromium,仅需安装对应二进制工具,无需完整浏览器环境。对复杂HTML和CSS布局的支持优于纯JS方案。
  • 适用场景:需要处理带复杂样式、表格、自定义页面布局的HTML内容。
  • 前置操作:Linux服务器上通过包管理器安装wkhtmltopdf(如Debian/Ubuntu执行apt-get install wkhtmltopdf)。
  • TypeScript示例:
import wkhtmltopdf from 'wkhtmltopdf';
import fs from 'fs';

const htmlContent = `
  <html>
    <head>
      <style>
        .container { width: 90%; margin: 20px auto; padding: 15px; border: 1px solid #eee; }
        h1 { color: #3498db; text-align: center; }
        table { width: 100%; border-collapse: collapse; margin-top: 15px; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
      </style>
    </head>
    <body>
      <div class="container">
        <h1>带样式的报表</h1>
        <table>
          <tr><th>序号</th><th>内容</th></tr>
          <tr><td>1</td><td>测试行1</td></tr>
          <tr><td>2</td><td>测试行2</td></tr>
        </table>
      </div>
    </body>
  </html>
`;

// 配置PDF参数并生成文件
wkhtmltopdf(htmlContent, {
  pageSize: 'A4',
  margin: { top: '15mm', right: '10mm', bottom: '15mm', left: '10mm' }
}).pipe(fs.createWriteStream('styled-output.pdf'));

3. html-pdf-node(html-pdf模式)

  • 核心特点:可选基于phantomjs的渲染模式,phantomjs二进制体积远小于Chromium,无需完整浏览器。API简洁,快速集成,支持TypeScript。
  • 注意:phantomjs已停止维护,但对于简单HTML转换场景仍能稳定运行。
  • TypeScript示例:
import htmlPdf from 'html-pdf-node';
import fs from 'fs';

const convertOptions = { format: 'A4', margin: '10mm' };
const htmlFile = { content: '<h1>快速转换示例</h1><p>无需复杂配置即可生成PDF</p>' };

// 生成PDF缓冲区并保存到文件
htmlPdf.generatePdf(htmlFile, convertOptions).then(pdfBuffer => {
  fs.writeFileSync('quick-output.pdf', pdfBuffer);
});

内容的提问来源于stack exchange,提问作者Shiva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 23:40:35