PDFKit 叉线 
一个用于节点和浏览器的JavaScript PDF生成库.
说明
PDFKit 是一个用于节点和浏览器的 PDF 文件生成库,它使创建复杂、多页、可打印 文档。 API包含可连锁性,既包括低级功能,也包括对更高层次的抽象 级别功能。 PDFKit API的设计很简单,因此生成复杂的文档往往像 几个函数调用.
检查一些 文献和实例 亲眼看看! 您也可以将指南读作 自生成的 PDF 中显示实例输出. 如果你想看看它是如何生成的, 请检查 README在 文档 文件夹.
您也可以尝试 PDFKit 的交互式浏览演示 这里。 。 。 .
为什么是叉子?
因为PDFKit需要最新的这些依赖:
- foliojs-fork/fontkit (法语) (见 为什么是叉子?(中文(简体) )
- foliojs-fork/ 断线 (见 为什么是叉子?(中文(简体) )
安装
安装使用 无 软件包管理器。 安装 npm 后只需输入以下命令 .
npm install @foliojs-fork/pdfkit
特征
- 矢量图形
- HTML5 类似画布的 API
- 路径操作
- 简单路径创建的 SVG 路径解析器
- 转换
- 线性梯度和射线梯度
- 文本
- 行包( 有软连字符识别)
- 文本对齐
- 项目列表
- 字体嵌入
- 支持 TrueType (.ttf), OpenType (.otf), WOFF, WOFF2, TrueType Collections (.ttc), 和 Datafork TrueType (.dfont) 字体
- 字体子设置
- 见 字体kit 需要更多关于高级 glyph 版式支持的详情.
- 图像嵌入
- 支持JPEG和PNG文件(包括有索引的PNG文件,以及透明化的PNG文件)
- 说明
- 链接
- 页:1
- 要点
- 2. 强调
- 包括:.
- 缩写
- 大纲
- PDF 安全
- 加密
- 访问权限(打印、复制、修改、注释、表格填充、内容可访问性、文件组装)
- 无障碍支持(标记内容、逻辑结构、标记的PDF、PDF/UA)
马上就来!
- 图案填充
- 创建表格和设置内容的高级API
- 更多性能优化
- 更棒的,也许你写的! 请叉开这个寄存器 并发送我拉请求.
示例
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();
此示例的 PDF 输出 (加上几个添加) 显示 PDFKit 的能动性——生产
代码量很小的复杂文档。 更多,请看 demo 文件夹和
PDFKit 编程指南。 。 。 .
浏览器使用
在浏览器中使用PDFKit有三种方法:
- 使用 浏览器演示 源代码 财务报告和审定财务报表 构建脚本
- 使用 网络软件包。见 完整实例。 。 。 .
- 使用预建版本 。 分配为
pdfkit.standalone.js文档中 释放 或包件中js文件夹.
除了PDFKit,你还需要一个地方将输出流到. HTML5 有一个 块 可用于存储二进制数据的对象,以及 获取此数据的 URL 以显示 Iframe 中的 PDF 输出, 或上传到服务器等 。 为了 从 PDFKit 的输出中获取 Blob, 您可以使用 双流 模块.
以下示例使用浏览器或网络包来加载 PDFKit 财务报告和审定财务报表 blob-stream。见 这里 财务报告和审定财务报表 这里 实例
预建版本的使用.
// 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;
});
您可以看到 PDFKit 的交互式浏览演示 这里。 。 。 .
请注意,为了浏览使用 PDFKit 的工程,您需要安装 brfs 模块为 npm,
用于将内置字体数据装入软件包。 它被列为一个 devDependency 内
PDFKit 软件 package.json,所以它不是默认为节点用户安装的.
如果您忘记安装, 浏览会打印出一个错误消息 .
文档
关于完整的API文档和更多实例,请参见: PDFKit 网站。 。 。 .
许可证
PDFKit根据麻省理工学院的许可提供.