通过开放源码创建和编辑

开放源JavaScript图书馆、创建和修改PDF个文件、增加和复制页面、并通过JavaScript个插入图像PDF。

PDF-Lib是一个开放源码JavaScript图书馆、使软件专业人员能够开发用于PDF个文件工作的强大应用程序。 图书馆特色丰富、设计在现代JavaScript个运行时工作。 它包括支持新的PDF个文件的创建、编辑现有的PDF个文件、增加或删除PDF页、复印页、绘制文本和图像、绘制和合。

图书馆以Typescript本书写成、编译成JavaScript本、没有其他依赖性。 创建PDF-Lib图书馆的一个重要目的是解决JavaScript个生态系统缺乏强有力的支持PDF个编辑或修改。 有JavaScript个很好的图书馆支持创建PDF个文件、但很少有人支持PDF个修改。 PDF-Lib包括完全支持PDF个修改和在所有JavaScript个环境中工作(不仅仅是Node或浏览器)。

Previous Next

从DF-Lib开始

启动和安装DF-Lib的最简单方法是通过数字和纱线、下面是命令。

通过数字安装DF-Lib

 npm install --save pdf-lib 

通过纱线安装DF-Lib

 yarn add pdf-lib 

PDF通过JavaScript创建和修改文件

开放源码PDF-Lib库包括了PDF个文档创建和修改的完整功能。 软件开发者可以在自己的应用程序中创建一个新的PDF个文档、其中只有JavaScript行代码。 一旦创建的开发者可以插入文本、绘制图像或矢量图形、从其他PDFs页中嵌入它们的字体、复制和嵌入页面、更多。

如何通过JavaScript创建PDF个文件

import { PDFDocument, StandardFonts, rgb } from 'pdf-lib'
// Create a new PDFDocument
const pdfDoc = await PDFDocument.create()
// Embed the Times Roman font
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman)
// Add a blank page to the document
const page = pdfDoc.addPage()
// Get the width and height of the page
const { width, height } = page.getSize()
// Draw a string of text toward the top of the page
const fontSize = 30
page.drawText('Creating PDFs in JavaScript is awesome!', {
  x: 50,
  y: height - 4 * fontSize,
  size: fontSize,
  font: timesRomanFont,
  color: rgb(0, 0.53, 0.71),
})
// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await pdfDoc.save()

S图书馆PDF份文件的复印页

使用现有页面、而不是在PDF个文档中创建一个新页面、往往非常有益。 开放源代码PDF-Lib库使计算机程序员能够从各PDF个文档中复制页面、并将其添加到所需的PDF个文档中、而无任何外部依赖。 首先、您需要加载PDF个文件、在此之后、您可以使用copyPages()命令复制所需的页面、然后使用addPage()命令在所需位置。

使用 JavaScript 将文本添加到现有 PDF

import { PDFDocument } from 'pdf-lib'
// Create a new PDFDocument
const pdfDoc = await PDFDocument.create()
const firstDonorPdfBytes = ...
const secondDonorPdfBytes = ...
// Load a PDFDocument from each of the existing PDFs
const firstDonorPdfDoc = await PDFDocument.load(firstDonorPdfBytes)
const secondDonorPdfDoc = await PDFDocument.load(secondDonorPdfBytes)
// Copy the 1st page from the first donor document, and
// the 743rd page from the second donor document
const [firstDonorPage] = await pdfDoc.copyPages(firstDonorPdfDoc, [0])
const [secondDonorPage] = await pdfDoc.copyPages(secondDonorPdfDoc, [742])
// Add the first copied page
pdfDoc.addPage(firstDonorPage)
// Insert the second copied page to index 0, so it will be the
// first page in `pdfDoc`
pdfDoc.insertPage(0, secondDonorPage)
// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await pdfDoc.save()

从PDF个文档读取元数据

PDF-Lib库完全支持访问和阅读PDF份文件的元数据。 元数据是PDF份文件的一个非常重要的组成部分、其中包括有关标题、主题、作者、版权信息、创作日期、修改日期等。 使用PDF-Lib库软件开发人员可以很容易地从PDF个文档中解析和提取元数据、只需要JavaScript行代码。

使用 JavaScript 将图像添加到 PDF


import { PDFDocument, StandardFonts } from 'pdf-lib'
// Create a new PDFDocument
const pdfDoc = await PDFDocument.create()
// Embed the Times Roman font
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman)
// Add a page and draw some text on it
const page = pdfDoc.addPage([500, 600])
page.setFont(timesRomanFont)
page.drawText('The Life of an Egg', { x: 60, y: 500, size: 50 })
page.drawText('An Epic Tale of Woe', { x: 125, y: 460, size: 25 })
// Set all available metadata fields on the PDFDocument. Note that these fields
// are visible in the "Document Properties" section of most PDF readers.
pdfDoc.setTitle('🥚 The Life of an Egg 🍳')
pdfDoc.setAuthor('Humpty Dumpty')
pdfDoc.setSubject('📘 An Epic Tale of Woe 📖')
pdfDoc.setKeywords(['eggs', 'wall', 'fall', 'king', 'horses', 'men'])
pdfDoc.setProducer('PDF App 9000 🤖')
pdfDoc.setCreator('pdf-lib (https://github.com/Hopding/pdf-lib)')
pdfDoc.setCreationDate(new Date('2018-06-24T01:58:37.228Z'))
pdfDoc.setModificationDate(new Date('2019-12-21T07:00:11.000Z'))
// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await pdfDoc.save()

将附件增加到PDF至JavaScript

有时我们需要提供更详细的信息关于PDF个文件、因此我们可以将另一个文件附加到该文件。 现在、该文件的好处是、如果将其移到不同的位置、附件将与PDF。 开放源码PDF-Lib库为软件开发者提供了将其他文件附加到其JavaScript个应用程序中的PDF个文件的能力。 将各种类型的文件附加到PDF个文件、例如MicrosoftWord、Excel、图像、视频甚至其他PDF个。

使用JavaScript增加PDF个附件

const jpgAttachmentBytes = ...
const pdfAttachmentBytes = ...
// Create a new PDFDocument
const pdfDoc = await PDFDocument.create()
// Add the JPG attachment
await pdfDoc.attach(jpgAttachmentBytes, 'cat_riding_unicorn.jpg', {
  mimeType: 'image/jpeg',
  creationDate: new Date('2019/12/01'),
  modificationDate: new Date('2020/04/19'),
})
// Add the PDF attachment
await pdfDoc.attach(pdfAttachmentBytes, 'us_constitution.pdf', {
  mimeType: 'application/pdf',
  creationDate: new Date('1787/09/17'),
  modificationDate: new Date('1992/05/07'),
})
// Add a page with some text
const page = pdfDoc.addPage();
page.drawText('This PDF has two attachments', { x: 135, y: 415 })
// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await pdfDoc.save()
 中国人