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 授權 · 原始碼