pdf-engine发布

npm地址: @zouchengxin/pdf-engine - npm

demo地址: pdf-engine

介绍

pdf-engine内部使用pdfium编译的Webassembly二进制文件。

目前已支持功能:

  • 解析XObject和Annotation对象
  • 页面预览
  • 编辑保存(创建Link)

安装

bash 复制代码
# yarn add @zouchengxin/pdf-engine
# pnpm install @zouchengxin/pdf-engine
npm install @zouchengxin/pdf-engine

使用

初始化

javascript 复制代码
import { PdfEngine } from '@zouchengxin/pdfium-engine';

const pdfEngine = new PdfEngine();
// API_KEY not provided, validity period until 2026-01-01, You can adjust the system time for testing.
// Or contact the developer to obtain the API key.
await pdfEngine.init(API_KEY);

解析

javascript 复制代码
// Load PDF documents, parameter Uint8Array
const pdfDoc = pdfEngine.loadPdf(data);
// Get the number of pages
const count = pdfDoc.getPageCount();
// Retrieves PDF metadata.
// returning the fields Title, Author, Subject, Keywords, Creator, Producer, CreationDate, and ModDate.
const meta = pdfDoc.getMetaData();
console.log('Page Count:', count);
console.log('Pdf Meta:', meta);
pdfPages.value = [];
for (let i = 0; i < count; i++) {
  // Obtain the page proxy object and perform operations such as parsing and editing.
  const page = pdfDoc.getPageProxy(i);
  // Get page width
  const width = page.getPageWidth();
  // Get page height
  const height = page.getPageHeight();
  // Retrieves all xobject objects on the page.
  // including those of type TEXT, PATH, IMAGE, SHADING, and FORM.
  const objs = page.getObjects();
  // Retrieves all annotation objects on the page.
  // including those of type TEXT, LINK, FREETEXT, LINE, SQUARE, CIRCLE, HIGHLIGHT, UNDERLINE, STAMP, INK etc.
  const annots = page.getAnnotions();
  console.log('Page Size:', width, height);
  console.log('Page Objects:', objs);
  console.log('Page Annotions:', annots);
}

渲染

javascript 复制代码
// Retrieve the bitmap after page rendering; render only the xobject object, excluding annotations.
// Return value: ImageData object
const data = page.getBitmap();
// Retrieve page thumbnail; return empty if not stored.
// Return value: ImageData object
const data = page.getThumbnail();

编辑

javascript 复制代码
// Create a link annotation.
// rect: a rectangular area.
// url: the redirect link.
page.createLinkAnno({
  rect: [100, 100, 160, 27],
  url: 'https://www.baidu.com',
});

// More features are under development.

// Save the PDF data and return a Uint8Array.
const uint8Arr = pdfDoc.savePdf();

注意

  • color: r, g, b, a, 红绿蓝透明通道组成的数组。
  • rect: x, y, w, h, 矩形区域左下角x/y坐标,w/h代表宽度高度。
  • 坐标系: 页面左下角为原点,水平方向为x轴。
  • API_KEY: 联系开发者获取,或者修改系统时间至2026/01/01之前进行测试。
相关推荐
SamChan9013 分钟前
WebSocket实现PDF翻译进度实时推送:Go后端+React前端的完整方案
前端·websocket·pdf·c#·react·机器翻译
森叶20 小时前
拆解 PDF 翻译引擎:表格纹丝不动、长语言自动缩放、图片文字 AI 重绘背后的工程
人工智能·pdf
AI创界者1 天前
MinerU v3.4.4 离线部署实战:PDF/PPT/图片一键转 Markdown,低至 4G 显存/纯 CPU 可用
pdf·powerpoint
cosinmz1 天前
图片转PDF:手机合同照片如何整理成规范PDF文件
pdf·pdf转换·图片转pdf·图片转pdf小程序
a1117761 天前
深入理解 AI Agent PDF(设计原理与工程实践 )
pdf
CCC65A8780D2C1 天前
PaddleOCR-VL-1.6图片PDF文档解析识别成文本工具,解压就能用,完全本地离线
pdf
桐桐桐1 天前
技术文档翻译实践:保留PDF表格与版式的几种方案
pdf·自动化
王莎莎-MinerU1 天前
别让 Agent 重读 PDF:文档解析要交付可缓存资产
网络·数据库·人工智能·驱动开发·缓存·pdf·ocr
SamChan902 天前
在Web应用中集成PDF多语言翻译功能:PDFTranslator API实战指南
前端·python·ai·pdf·yapi·机器翻译
CCC65A8780D2C2 天前
OvisOCR2图片PDF识别精准识别成文字工具
pdf