pdfkit vs pdfmake vs react-pdf
PDF 生成库
pdfkitpdfmakereact-pdf类似的npm包:

PDF 生成库

PDF 生成库是用于在 web 应用程序中创建和生成 PDF 文档的工具。这些库提供了丰富的功能,能够让开发者以编程方式构建复杂的 PDF 文件,包括文本、图像、表格和图形等元素。选择合适的 PDF 生成库可以极大地影响开发效率和最终文档的质量。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
pdfkit010,6368.45 MB4001 个月前MIT
pdfmake012,25815.3 MB2351 个月前MIT
react-pdf011,041309 kB242 个月前MIT

功能对比: pdfkit vs pdfmake vs react-pdf

生成方式

  • pdfkit:

    PDFKit 允许开发者通过流式 API 生成 PDF 文档,适合处理大文件和动态内容。它支持在 Node.js 环境中直接生成 PDF,并可以将生成的内容直接发送到客户端。

  • pdfmake:

    pdfmake 使用声明式语法,开发者可以通过 JSON 对象定义文档结构。它支持在浏览器和 Node.js 中生成 PDF,适合快速构建和修改文档。

  • react-pdf:

    react-pdf 允许开发者将 PDF 文档作为 React 组件进行构建,支持在客户端渲染和打印 PDF。它提供了一种直观的方式来创建和管理 PDF 文档,适合与 React 应用无缝集成。

样式和布局

  • pdfkit:

    PDFKit 提供了丰富的样式和布局选项,包括字体、颜色、图形和图像等。开发者可以通过编程方式精确控制每个元素的样式,适合需要高度自定义的文档。

  • pdfmake:

    pdfmake 支持复杂的布局和样式定义,开发者可以使用内置的样式和主题,快速创建美观的文档。它还支持表格、列表和多列布局,适合需要多样化布局的场景。

  • react-pdf:

    react-pdf 允许使用 CSS 样式来控制 PDF 文档的外观,开发者可以利用 React 的组件化特性,轻松管理和复用样式。它适合需要快速迭代和修改样式的应用。

支持的功能

  • pdfkit:

    PDFKit 支持多种功能,如文本、图像、路径、表格和链接等。它还支持流式生成和压缩,适合处理复杂的 PDF 文档。

  • pdfmake:

    pdfmake 提供了丰富的功能,包括表格、列表、图像和多种字体支持。它还支持国际化和多种语言,适合需要多语言支持的应用。

  • react-pdf:

    react-pdf 提供了基本的 PDF 生成功能,支持文本、图像和样式。它适合与 React 生态系统中的其他库结合使用,扩展功能。

学习曲线

  • pdfkit:

    PDFKit 的学习曲线相对较陡,开发者需要熟悉其 API 和流式生成概念。适合有一定编程经验的开发者。

  • pdfmake:

    pdfmake 的学习曲线较平缓,开发者可以快速上手,通过 JSON 定义文档结构,适合初学者和快速开发。

  • react-pdf:

    react-pdf 的学习曲线较低,特别是对于熟悉 React 的开发者。它的组件化设计使得开发者能够快速构建和修改 PDF 文档。

社区和支持

  • pdfkit:

    PDFKit 拥有活跃的社区和丰富的文档支持,开发者可以轻松找到示例和解决方案。

  • pdfmake:

    pdfmake 也有良好的社区支持,提供了详细的文档和示例,适合快速学习和使用。

  • react-pdf:

    react-pdf 在 React 社区中受到广泛使用,拥有丰富的资源和示例,开发者可以轻松找到帮助和支持。

如何选择: pdfkit vs pdfmake vs react-pdf

  • pdfkit:

    选择 PDFKit 如果你需要一个强大的 Node.js 库来生成 PDF 文档,支持流式生成和复杂布局。它适合需要自定义样式和内容的应用,尤其是在服务器端生成 PDF 时。

  • pdfmake:

    选择 pdfmake 如果你需要一个灵活且易于使用的库,能够在浏览器和 Node.js 中运行,支持声明式文档定义。它适合快速生成具有复杂结构和样式的 PDF 文档,尤其是在客户端生成时。

  • react-pdf:

    选择 react-pdf 如果你正在使用 React 并希望在客户端渲染 PDF 文档。它允许你将 PDF 文档作为 React 组件进行构建,适合需要与 React 应用紧密集成的场景。

pdfkit的README

PDFKit

A JavaScript PDF generation library for Node and the browser.

Description

PDFKit is a PDF document generation library for Node and the browser that makes creating complex, multi-page, printable documents easy. The API embraces chainability, and includes both low level functions as well as abstractions for higher level functionality. The PDFKit API is designed to be simple, so generating complex documents is often as simple as a few function calls.

Check out some of the documentation and examples to see for yourself! You can also read the guide as a self-generated PDF with example output displayed inline. If you'd like to see how it was generated, check out the README in the docs folder.

You can also try out an interactive in-browser demo of PDFKit here.

Installation

Use npm or yarn package manager. Just type the following command:

# with npm
npm install pdfkit

# with yarn
yarn add pdfkit

Features

  • Vector graphics
    • HTML5 canvas-like API
    • Path operations
    • SVG path parser for easy path creation
    • Transformations
    • Linear and radial gradients
  • Text
    • Line wrapping (with soft hyphen recognition)
    • Text alignments
    • Bulleted lists
  • Font embedding
    • Supports TrueType (.ttf), OpenType (.otf), WOFF, WOFF2, TrueType Collections (.ttc), and Datafork TrueType (.dfont) fonts
    • Font subsetting
    • See fontkit for more details on advanced glyph layout support.
  • Image embedding
    • Supports JPEG and PNG files (including indexed PNGs, and PNGs with transparency)
  • Tables
  • Annotations
    • Links
    • Notes
    • Highlights
    • Underlines
    • etc.
  • AcroForms
  • Outlines
  • PDF security
    • Encryption
    • Access privileges (printing, copying, modifying, annotating, form filling, content accessibility, document assembly)
  • Accessibility support (marked content, logical structure, Tagged PDF, PDF/UA)

Coming soon!

  • Patterns fills
  • Higher level APIs for laying out content
  • More performance optimizations
  • Even more awesomeness, perhaps written by you! Please fork this repository and send me pull requests.

Example

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

// Create a document
const doc = new PDFDocument();

// Pipe its output somewhere, like to a file or HTTP response
// See below for browser usage
doc.pipe(fs.createWriteStream('output.pdf'));

// Embed a font, set the font size, and render some text
doc
  .font('fonts/PalatinoBold.ttf')
  .fontSize(25)
  .text('Some text with an embedded font!', 100, 100);

// Add an image, constrain it to a given size, and center it vertically and horizontally
doc.image('path/to/image.png', {
  fit: [250, 300],
  align: 'center',
  valign: 'center'
});

// Add another page
doc
  .addPage()
  .fontSize(25)
  .text('Here is some vector graphics...', 100, 100);

// Draw a triangle
doc
  .save()
  .moveTo(100, 150)
  .lineTo(100, 250)
  .lineTo(200, 250)
  .fill('#FF3300');

// Apply some transforms and render an SVG path with the 'even-odd' fill rule
doc
  .scale(0.6)
  .translate(470, -380)
  .path('M 250,75 L 323,301 131,161 369,161 177,301 z')
  .fill('red', 'even-odd')
  .restore();

// Add some text with annotations
doc
  .addPage()
  .fillColor('blue')
  .text('Here is a link!', 100, 100)
  .underline(100, 100, 160, 27, { color: '#0000FF' })
  .link(100, 100, 160, 27, 'http://google.com/');

// Finalize PDF file
doc.end();

The PDF output from this example (with a few additions) shows the power of PDFKit — producing complex documents with a very small amount of code. For more, see the demo folder and the PDFKit programming guide.

Browser Usage

There are three ways to use PDFKit in the browser:

In addition to PDFKit, you'll need somewhere to stream the output to. HTML5 has a Blob object which can be used to store binary data, and get URLs to this data in order to display PDF output inside an iframe, or upload to a server, etc. In order to get a Blob from the output of PDFKit, you can use the blob-stream module.

The following example uses Browserify or webpack to load PDFKit and blob-stream. See here and here for examples of prebuilt version usage.

// require dependencies
const PDFDocument = require('pdfkit');
const blobStream = require('blob-stream');

// create a document the same way as above
const doc = new PDFDocument();

// pipe the document to a blob
const stream = doc.pipe(blobStream());

// add your content to the document here, as usual

// get a blob when you are done
doc.end();
stream.on('finish', function() {
  // get a blob you can do whatever you like with
  const blob = stream.toBlob('application/pdf');

  // or get a blob URL for display in the browser
  const url = stream.toBlobURL('application/pdf');
  iframe.src = url;
});

You can see an interactive in-browser demo of PDFKit here.

Note that in order to Browserify a project using PDFKit, you need to install the brfs module, which is used to load built-in font data into the package. It is listed as a devDependency in PDFKit's package.json, so it isn't installed by default for Node users. If you forget to install it, Browserify will print an error message.

Documentation

For complete API documentation and more examples, see the PDFKit website.

License

PDFKit is available under the MIT license.