PDF 编辑器:JavaScript 库

基于 PDFium(WebAssembly)的 PDF 编辑:在保持版式的前提下原地修改文字,添加文字、图片、高亮和手绘,遮盖内容,旋转、删除、重排和合并页面。

  • @openlibsdev/pdf-editor
  • v1.0.0
  • ES module
  • MIT

安装

使用打包工具或 Node 风格的导入:

npm install @openlibsdev/pdf-editor
import PdfEditor from "@openlibsdev/pdf-editor";

或直接在页面中使用,无需构建(jsDelivr 会解析依赖):

<script type="module">
import PdfEditor from "https://cdn.jsdelivr.net/npm/@openlibsdev/pdf-editor@1.0.0/+esm";
</script>

用法

import PdfEditor from "@openlibsdev/pdf-editor";

const doc = await PdfEditor.create();               // or await PdfEditor.open(bytes)
await doc.addText(0, 72, 770, "Hello, PDF", { family: "DejaVu Sans", size: 18 });
const page = doc.render(0, 1.5);                    // { width, height, data: RGBA }
const bytes = doc.save();                           // Uint8Array
doc.close();

API

PdfEditor.open(bytes)

从字节打开 PDF 并返回文档。

PdfEditor.create(width, height)

创建空白文档(默认 A4)。

PdfEditor.ready()

提前加载 PDFium。

PdfEditor.setup(o)

设置引擎和字体的地址。

PdfEditor.addFontBytes(name, bytes)

从字节添加用于新文字的字体。

PdfEditor.pdfFamilies()

可用于文字的字体家族。

PdfEditor.A4

A4 页面尺寸(磅)。

PdfEditor.APP

库的名称、版本和发布日期:{name, version, created}。

PdfEditor.PdfDocument()

已打开文档的类(open 和 create 返回的对象)。

PdfEditor.SUBSTITUTE

当文档自带字体无法编辑时代替使用的字体:sans, serif, mono → DejaVu。

PdfEditor.findNamedColor(text, lang)

按指定语言的颜色名称查找命名颜色(“深蓝色”→ #00008b)。

PdfEditor.fontFor(family, bold, italic)

用于新文本的某字体族和样式的字体,已加载并可嵌入 PDF。

PdfEditor.listNamedColors(lang)

列出命名颜色及其在指定语言中的名称。

PdfEditor.namedColorTexts(lang)

指定语言的颜色字段标签(取色器的文本),缺少该语言时为英文。

PdfEditor.pdfTextFontData

PDF 编辑器文本和字体区块的数据(来自 font-forge,仅限可嵌入 PDF 的字体)。

PdfEditor.textFontData

文本和字体区块的数据:字体列表、加载字形样式、添加字体文件、某种语言的区块标签以及默认值。

实例:doc = await PdfEditor.open(bytes)

doc.pageCount

页数。

doc.pageSize(i)

页面宽度和高度(磅)。

doc.render(i, scale)

按比例绘制页面:{width, height, data}。

doc.paragraphs(i)

页面上的文字段落及其位置和字体。

doc.paragraphAt(i, x, y)

页面上某点下方的段落。

doc.editParagraph(i, para, newText, style)

替换段落文字,保留其位置和换行。

doc.addText(i, x, yTop, text, style)

在某点添加文字。

doc.insertImage(i, img, r)

在矩形中插入图片。

doc.highlight(i, r, color)

高亮矩形区域。

doc.addInk(i, strokes, color, width)

添加手绘笔迹。

doc.redact(i, r, fill)

涂盖矩形并删除其下的内容。

doc.rotatePage(i, quarterTurns)

按 90 度为单位旋转页面。

doc.deletePage(i)

删除页面。

doc.movePage(from, to)

移动页面。

doc.insertBlankPage(at, width, height)

插入空白页。

doc.merge(bytes, at)

添加另一个 PDF 的页面。

doc.save()

以字节返回文档。

doc.close()

释放内存。

doc.annotationCount(i)

页面上注释(高亮、绘图)的数量。

doc.duplicatePage(i)

在页面之后紧接着插入其副本。

doc.importPages(src, at, indexes)

将另一个已打开文档的页面(全部或指定索引)复制到某个位置。

doc.rectToView(i, view, r)

页面空间中的矩形 {x0, y0, x1, y1} → 渲染页面上的矩形 {left, top, width, height}。

doc.textObjects(i)

页面上的所有文本对象及其文本、位置、大小和字体。

doc.toPage(i, view, x, y)

渲染页面上的点 → 页面空间中的点(磅,y 向上)。

doc.toView(i, view, x, y)

页面空间中的点 → 渲染页面上的点。

doc.wrap(font, size, text, maxWidth)

按字体的真实字形宽度把文本分成能放入指定宽度的行。

依赖

库加载的内容及其许可证(全部列在“许可证”页面)。

名称版本许可证文件
@openlibsdev/font-forge1.0.0MITGitHub
@openlibsdev/image-forge1.0.0MITGitHub
@embedpdf/pdfium2.15.1MIT 许可证 · 源代码
@embedpdf/pdfium2.15.1MIT 许可证 · 源代码
@imagemagick/magick-wasm0.0.35Apache-2.0 许可证 · 源代码
@imagemagick/magick-wasm0.0.35Apache-2.0 许可证 · 源代码
@simonwep/pickr1.10.2MIT 许可证 · 源代码
@simonwep/pickr1.10.2MIT 许可证 · 源代码
dejavu-fonts-ttf2.37.3Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain 许可证 · 源代码
imagetracerjs1.2.6Unlicense 许可证 · 源代码
jspdf2.5.2MIT 许可证 · 源代码