由 lemon 出售

pdf-lib lets you create, modify, and manipulate PDF documents in any JavaScript environment including Node, Browser, Deno, and React Native. Supports forms, fonts, images, SVG paths, metadata, and PDF 2.0 features.
pdf-lib is a full-featured PDF creation and modification library that runs in any JavaScript environment (Node.js, browsers, Deno, React Native). It supports creating documents from scratch, editing existing PDFs, embedding fonts and images, managing interactive forms, and setting document metadata. The typical buyer is a backend or fullstack developer who needs programmatic PDF generation or manipulation without a headless browser.
api/ - High-level user-facing API: documents, pages, fonts, images, forms, colors, operatorsapi/form/ - Form field classes: PDFButton, PDFCheckBox, PDFDropdown, PDFField, PDFForm, PDFOptionList, PDFRadioGroup, PDFSignature, PDFTextField, and appearance helpersapi/image/ - Image alignment utilitiesapi/text/ - Text alignment and layout utilitiesapi/PDFDocument.ts - Root document class; entry point for all create/load/save operationsapi/PDFPage.ts - Page-level drawing API: text, images, shapes, SVG pathsapi/PDFFont.ts - Embedded font wrapper with text measurementapi/PDFImage.ts - Embedded image wrapperapi/PDFEmbeddedPage.ts - Embedded PDF page (for XObject reuse)api/PDFJavaScript.ts - JavaScript action attachmentapi/StandardFonts.ts - Enum of the 14 standard PDF fontsapi/colors.ts - Color constructors: rgb, cmyk, grayscaleapi/rotations.ts - Rotation helpersapi/sizes.ts - Standard page size constantsapi/operators.ts / api/operations.ts / api/objects.ts - Low-level PDF operator helpersapi/errors.ts - Public error typescore/ - Low-level PDF object model, parser, writer, streams, and syntaxtypes/ - Shared TypeScript type declarationsutils/ - Internal utility functionsindex.ts - Barrel re-export of every public symbolnpm install pdf-lib
npm install @pdf-lib/standard-fonts @pdf-lib/upng pako tslib
No native modules, no pod install, no Android linking, no Expo prebuild required. The library is pure JavaScript/TypeScript and works without any build-time native steps.
启动隔离沙箱并在服务器端直接运行 — 无需本地配置。
此版本的 Tetrees AI Review
This TypeScript cli / script completed archive review. Structure, dependency manifests, documentation, functional source, and common risk patterns were checked by the Tetrees verification pipeline; runtime phases are stated separately.
Deterministic AVCP artifact review
管道 avcp-2026-08-04.1 · SHA-256 6106df09481a659e…
This version-scoped review deterministically inspects the submitted archive for structure, dependencies, documentation, functional source, and common malicious or high-risk signals. Build and test phases are reported as passed only after an isolated sandbox audition. It is not a guarantee of perfect security.
审查日期 2026年8月4日
将此产品直接导入你的 AI IDE、网页构建器或云端 IDE。
将 Tetrees 连接到兼容的 AI IDE,列出你拥有的产品并获取已验证 ZIP,同时不会开放卖家上传权限。
暂无评价。
Sign in to join the discussion
Loading discussion…
Copy the source/ directory into your project, e.g. src/vendor/pdf-lib/.
In tsconfig.json, add a path alias so imports resolve correctly:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"src/*": ["src/vendor/pdf-lib/src/*"]
}
}
}
babel-plugin-module-resolver:{
"plugins": [
["module-resolver", {
"alias": { "src": "./src/vendor/pdf-lib/src" }
}]
]
}
import { PDFDocument, StandardFonts, rgb } from './vendor/pdf-lib/src/index';
class PDFDocument {
static create(options?: PDFDocumentOptions): Promise<PDFDocument>;
static load(pdf: string | Uint8Array | ArrayBuffer, options?: PDFDocumentOptions): Promise<PDFDocument>;
addPage(size?: [number, number] | PageSizes): PDFPage;
insertPage(index: number, size?: [number, number] | PageSizes): PDFPage;
removePage(index: number): void;
getPage(index: number): PDFPage;
getPageCount(): number;
embedFont(font: StandardFonts | string | Uint8Array | ArrayBuffer, options?: EmbedFontOptions): Promise<PDFFont>;
embedPng(png: string | Uint8Array | ArrayBuffer): Promise<PDFImage>;
embedJpg(jpg: string | Uint8Array | ArrayBuffer): Promise<PDFImage>;
getForm(): PDFForm;
save(options?: SaveOptions): Promise<Uint8Array>;
saveAsBase64(options?: Base64SaveOptions): Promise<string>;
}
The central class. Use PDFDocument.create() to start a new document and PDFDocument.load() to modify an existing one. Call save() to get the final Uint8Array for writing to disk or sending over HTTP.
class PDFPage {
drawText(text: string, options?: PDFPageDrawTextOptions): void;
drawImage(image: PDFImage, options?: PDFPageDrawImageOptions): void;
drawRectangle(options?: PDFPageDrawRectangleOptions): void;
drawLine(options?: PDFPageDrawLineOptions): void;
drawSvgPath(path: string, options?: PDFPageDrawSVGOptions): void;
setFont(font: PDFFont): void;
setFontSize(size: number): void;
getSize(): { width: number; height: number };
setSize(width: number, height: number): void;
moveTo(x: number, y: number): void;
}
Obtained from PDFDocument.addPage() or PDFDocument.getPage(). Use it to draw text, images, vector shapes, and SVG paths. The coordinate origin is the bottom-left corner.
class PDFForm {
getTextField(name: string): PDFTextField;
getCheckBox(name: string): PDFCheckBox;
getDropdown(name: string): PDFDropdown;
getRadioGroup(name: string): PDFRadioGroup;
createTextField(name: string): PDFTextField;
createCheckBox(name: string): PDFCheckBox;
createDropdown(name: string): PDFDropdown;
createRadioGroup(name: string): PDFRadioGroup;
flatten(options?: FlattenOptions): void;
}
Accessed via pdfDoc.getForm(). Use it to read, fill, create, or flatten interactive form fields. Call flatten() to bake field values into the page content permanently.
Creates a new single-page PDF, draws a colored rectangle and a line of text, then writes the result to disk.
import { PDFDocument, StandardFonts, rgb, PageSizes } from './vendor/pdf-lib/src/index';
import { writeFileSync } from 'fs';
async function createPDF() {
const doc = await PDFDocument.create();
const page = doc.addPage(PageSizes.A4);
const font = await doc.embedFont(StandardFonts.Helvetica);
page.drawRectangle({
x: 50,
y: 700,
width: 200,
height: 40,
color: rgb(0.2, 0.4, 0.8),
opacity: 0.8,
});
page.drawText('Hello, pdf-lib!', {
x: 55,
y: 712,
size: 18,
font,
color: rgb(1, 1, 1),
});
const bytes = await doc.save();
writeFileSync('output.pdf', bytes);
}
createPDF();
Loads an existing PDF that contains form fields, fills them, then saves a flattened copy.
import { PDFDocument } from './vendor/pdf-lib/src/index';
import { readFileSync, writeFileSync } from 'fs';
async function fillForm() {
const existingPdfBytes = readFileSync('form_template.pdf');
const doc = await PDFDocument.load(existingPdfBytes);
const form = doc.getForm();
const nameField = form.getTextField('full_name');
nameField.setText('Jane Doe');
const agreeBox = form.getCheckBox('agree_terms');
agreeBox.check();
const roleDropdown = form.getDropdown('role');
roleDropdown.select('Engineer');
// Bake values into page content - no longer editable
form.flatten();
const bytes = await doc.save();
writeFileSync('filled_form.pdf', bytes);
}
fillForm();
Embeds a PNG image on a page and copies a page from a second PDF into the main document.
import { PDFDocument, PageSizes } from './vendor/pdf-lib/src/index';
import { readFileSync, writeFileSync } from 'fs';
async function embedAndCopy() {
const mainDoc = await PDFDocument.create();
const page = mainDoc.addPage(PageSizes.Letter);
// Embed PNG
const pngBytes = readFileSync('logo.png');
const pngImage = await mainDoc.embedPng(pngBytes);
const { width, height } = pngImage.scale(0.5);
page.drawImage(pngImage, { x: 50, y: 600, width, height });
// Copy first page from another PDF
const secondPdfBytes = readFileSync('report.pdf');
const secondDoc = await PDFDocument.load(secondPdfBytes);
const [copiedPage] = await mainDoc.copyPages(secondDoc, [0]);
mainDoc.addPage(copiedPage);
const bytes = await mainDoc.save();
writeFileSync('combined.pdf', bytes);
}
embedAndCopy();
index.ts - Top-level barrel; re-exports everything from api/, core/, types/, and utils/.api/ - All user-facing classes and helpers. Start here for application code.api/form/ - Form field classes and appearance stream helpers for interactive AcroForm widgets.api/image/ - Image alignment enum used by PDFPage.drawImage positioning helpers.api/text/ - Text alignment enum and multi-line text layout engine used by drawText.api/PDFDocument.ts - The root document class; owns the PDF context and all embed/save logic.api/PDFPage.ts - Page drawing surface; wraps a content stream with a friendly drawing API.api/PDFFont.ts - Font wrapper; exposes widthOfTextAtSize and heightAtSize for layout.api/PDFImage.ts - Image wrapper; exposes width, height, and scale.api/PDFEmbeddedPage.ts - Wraps a foreign page as a reusable XObject for drawPage.api/PDFJavaScript.ts - Represents a document-level JavaScript action attachment.api/StandardFonts.ts - Enum of the 14 built-in PDF fonts (no embedding needed).api/colors.ts - Factory functions rgb(), cmyk(), grayscale() for color values.api/rotations.ts - degrees() and radians() rotation value constructors.api/sizes.ts - Named page size tuples like PageSizes.A4, PageSizes.Letter.api/operators.ts / api/operations.ts - Low-level PDF content stream operator wrappers.api/objects.ts - PDF object literal helpers used internally by operators.api/errors.ts - Exported error classes for type-safe catch blocks.core/ - PDF specification internals: object model, parser, cross-reference tables, stream codecs.types/ - Shared TypeScript interfaces and type aliases consumed across api/ and core/.utils/ - Pure utility functions (array helpers, type guards, encoding) used throughout.src/* alias in tsconfig.json is compile-time only; ensure tsconfig-paths or babel-plugin-module-resolver is configured for Jest and Node execution.copyPages is async and must be awaited: PDFDocument.copyPages() returns a Promise<PDFPage[]>; forgetting await results in adding a Promise object instead of a page.y: 0 is the bottom of the page; large y values place content near the top - the opposite of CSS/canvas.PDFDocument.load rejects encrypted PDFs by default: Pass { ignoreEncryption: true } as the second argument to load encrypted documents (fields will be read-only).StandardFonts do not support non-Latin characters: For UTF-8 or UTF-16 text, embed a custom TTF/OTF font via embedFont(fontBytes) instead.save() returns Uint8Array, not a Buffer: In Node.js, wrap with Buffer.from(bytes) before passing to fs.writeFileSync or an HTTP response if a Buffer is explicitly required.I have a local copy of the pdf-lib source (upstream npm: user@example.com) at
`src/vendor/pdf-lib/`. I also have USAGE.md in the same directory as this prompt.
Please help me integrate pdf-lib into my existing TypeScript/Node.js project
step by step:
1. Read USAGE.md and the file layout under `src/vendor/pdf-lib/source/`.
2. Add the required tsconfig path alias so `src/*` resolves correctly.
3. Install runtime dependencies: @pdf-lib/standard-fonts, @pdf-lib/upng, pako, tslib.
4. Create a utility module at `src/lib/pdf.ts` that exports helper functions
using PDFDocument, PDFPage, PDFFont, PDFForm, and PDFImage from
`src/vendor/pdf-lib/src/index`.
5. Integrate the helpers into my existing Express route handlers to
generate and serve PDFs over HTTP.
6. Show me how to fill and flatten an AcroForm template loaded from disk.
Use only the real exports visible in USAGE.md. Do not invent API methods.
pdf-lib is released under the MIT License. See source/LICENSE if present, or refer to the official repository. Upstream package: user@example.com by Andrew Dillon (Hopding).
This product’s unique contribution is clean room implementation of valuable code block into AVCP compatible standard.
完整安装指南与集成提示词将在购买后解锁。
Automation, Utilities & Developer Tools
免费