html-pdf vs pdfkit vs puppeteer
PDF生成库
html-pdfpdfkitpuppeteer类似的npm包:

PDF生成库

PDF生成库用于将HTML内容转换为PDF文档,广泛应用于生成发票、报告和其他文档。它们提供了不同的功能和灵活性,适用于各种需求。选择合适的库可以帮助开发者高效地创建高质量的PDF文件,满足不同的业务需求和技术要求。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
html-pdf03,646-4685 年前MIT
pdfkit010,5966.09 MB4026 个月前MIT
puppeteer093,69563 kB28512 天前Apache-2.0

功能对比: html-pdf vs pdfkit vs puppeteer

功能强度

  • html-pdf:

    html-pdf提供简单的HTML到PDF转换功能,适合快速生成基本文档,但在复杂布局和样式方面有限。

  • pdfkit:

    pdfkit提供强大的PDF创建功能,支持文本、图形、图像等多种元素的绘制,适合需要复杂文档的场景。

  • puppeteer:

    puppeteer不仅支持PDF生成,还能抓取网页和执行自动化测试,功能非常强大,适合需要全面网页处理的项目。

学习曲线

  • html-pdf:

    html-pdf的学习曲线较低,易于上手,适合初学者和需要快速实现PDF功能的开发者。

  • pdfkit:

    pdfkit的学习曲线相对较陡,开发者需要理解其API和PDF文档结构,适合有一定经验的开发者。

  • puppeteer:

    puppeteer的学习曲线较高,尤其是对于不熟悉浏览器自动化的开发者,但其强大的功能值得投入时间学习。

性能

  • html-pdf:

    html-pdf在处理简单文档时性能良好,但在处理复杂文档时可能会出现性能瓶颈。

  • pdfkit:

    pdfkit在生成复杂PDF时表现出色,能够有效处理大量数据和复杂布局。

  • puppeteer:

    puppeteer的性能依赖于Chrome浏览器的渲染能力,适合生成高保真度的PDF,但可能在资源消耗上较高。

灵活性

  • html-pdf:

    html-pdf的灵活性较低,主要依赖于HTML内容的结构,无法进行复杂的自定义。

  • pdfkit:

    pdfkit提供高度的灵活性,允许开发者自定义PDF的每个部分,适合需要精确控制的项目。

  • puppeteer:

    puppeteer的灵活性极高,能够处理动态内容和复杂的网页结构,适合需要高保真度和交互性的PDF生成。

社区支持

  • html-pdf:

    html-pdf的社区支持相对较小,文档和示例较少,可能在遇到问题时难以找到解决方案。

  • pdfkit:

    pdfkit拥有活跃的社区和丰富的文档,开发者可以轻松找到支持和资源。

  • puppeteer:

    puppeteer是一个流行的库,拥有广泛的社区支持和大量的学习资源,适合需要社区帮助的开发者。

如何选择: html-pdf vs pdfkit vs puppeteer

  • html-pdf:

    选择html-pdf如果你需要快速将HTML内容转换为PDF,且不需要复杂的PDF布局或样式。它简单易用,适合快速生成基本的PDF文档。

  • pdfkit:

    选择pdfkit如果你需要更高的灵活性和控制,能够创建复杂的PDF文档。pdfkit允许你直接绘制图形、文本和其他元素,适合需要自定义布局的场景。

  • puppeteer:

    选择puppeteer如果你需要一个强大的工具来生成PDF,同时需要进行网页抓取或自动化测试。puppeteer可以渲染完整的网页,包括CSS和JavaScript,适合需要高保真度的PDF生成。

html-pdf的README

node-html-pdf

HTML to PDF converter that uses phantomjs

image
Example Business Card
-> and its Source file

Example Receipt

Changelog

Have a look at the releases page: https://github.com/marcbachmann/node-html-pdf/releases

Installation

Install the html-pdf utility via npm:

$ npm install -g html-pdf

Command-line example

$ html-pdf test/businesscard.html businesscard.pdf

Code example

var fs = require('fs');
var pdf = require('html-pdf');
var html = fs.readFileSync('./test/businesscard.html', 'utf8');
var options = { format: 'Letter' };

pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
  if (err) return console.log(err);
  console.log(res); // { filename: '/app/businesscard.pdf' }
});

API

var pdf = require('html-pdf');
pdf.create(html).toFile([filepath, ]function(err, res){
  console.log(res.filename);
});

pdf.create(html).toStream(function(err, stream){
  stream.pipe(fs.createWriteStream('./foo.pdf'));
});

pdf.create(html).toBuffer(function(err, buffer){
  console.log('This is a buffer:', Buffer.isBuffer(buffer));
});


// for backwards compatibility
// alias to pdf.create(html[, options]).toBuffer(callback)
pdf.create(html [, options], function(err, buffer){});

Footers and Headers

html-pdf can read the header or footer either out of the footer and header config object or out of the html source. You can either set a default header & footer or overwrite that by appending a page number (1 based index) to the id="pageHeader" attribute of a html tag.

You can use any combination of those tags. The library tries to find any element, that contains the pageHeader or pageFooter id prefix.

<div id="pageHeader">Default header</div>
<div id="pageHeader-first">Header on first page</div>
<div id="pageHeader-2">Header on second page</div>
<div id="pageHeader-3">Header on third page</div>
<div id="pageHeader-last">Header on last page</div>
...
<div id="pageFooter">Default footer</div>
<div id="pageFooter-first">Footer on first page</div>
<div id="pageFooter-2">Footer on second page</div>
<div id="pageFooter-last">Footer on last page</div>

Options

config = {

  // Export options
  "directory": "/tmp",       // The directory the file gets written into if not using .toFile(filename, callback). default: '/tmp'

  // Papersize Options: http://phantomjs.org/api/webpage/property/paper-size.html
  "height": "10.5in",        // allowed units: mm, cm, in, px
  "width": "8in",            // allowed units: mm, cm, in, px
  - or -
  "format": "Letter",        // allowed units: A3, A4, A5, Legal, Letter, Tabloid
  "orientation": "portrait", // portrait or landscape

  // Page options
  "border": "0",             // default is 0, units: mm, cm, in, px
  - or -
  "border": {
    "top": "2in",            // default is 0, units: mm, cm, in, px
    "right": "1in",
    "bottom": "2in",
    "left": "1.5in"
  },

  paginationOffset: 1,       // Override the initial pagination number
  "header": {
    "height": "45mm",
    "contents": '<div style="text-align: center;">Author: Marc Bachmann</div>'
  },
  "footer": {
    "height": "28mm",
    "contents": {
      first: 'Cover page',
      2: 'Second page', // Any page number is working. 1-based index
      default: '<span style="color: #444;">{{page}}</span>/<span>{{pages}}</span>', // fallback value
      last: 'Last Page'
    }
  },


  // Rendering options
  "base": "file:///home/www/your-asset-path/", // Base path that's used to load files (images, css, js) when they aren't referenced using a host

  // Zooming option, can be used to scale images if `options.type` is not pdf
  "zoomFactor": "1", // default is 1

  // File options
  "type": "pdf",           // allowed file types: png, jpeg, pdf
  "quality": "75",         // only used for types png & jpeg

  // Script options
  "phantomPath": "./node_modules/phantomjs/bin/phantomjs", // PhantomJS binary which should get downloaded automatically
  "phantomArgs": [], // array of strings used as phantomjs args e.g. ["--ignore-ssl-errors=yes"]
  "localUrlAccess": false, // Prevent local file:// access by passing '--local-url-access=false' to phantomjs
                           // For security reasons you should keep the default value if you render arbritary html/js.
  "script": '/url',        // Absolute path to a custom phantomjs script, use the file in lib/scripts as example
  "timeout": 30000,        // Timeout that will cancel phantomjs, in milliseconds

  // Time we should wait after window load
  // accepted values are 'manual', some delay in milliseconds or undefined to wait for a render event
  "renderDelay": 1000,

  // HTTP Headers that are used for requests
  "httpHeaders": {
    // e.g.
    "Authorization": "Bearer ACEFAD8C-4B4D-4042-AB30-6C735F5BAC8B"
  },

  // To run Node application as Windows service
  "childProcessOptions": {
    "detached": true
  }

  // HTTP Cookies that are used for requests
  "httpCookies": [
    // e.g.
    {
      "name": "Valid-Cookie-Name", // required
      "value": "Valid-Cookie-Value", // required
      "domain": "localhost",
      "path": "/foo", // required
      "httponly": true,
      "secure": false,
      "expires": (new Date()).getTime() + (1000 * 60 * 60) // e.g. expires in 1 hour
    }
  ]

}

The full options object gets converted to JSON and will get passed to the phantomjs script as third argument.
There are more options concerning the paperSize, header & footer options inside the phantomjs script.