1. Produkte
  2.   Pdf
  3.   JavaScript
  4.   PDF-Lib
 
  

PDF Creation & Editing via Open Source JavaScript API

Open Source JavaScript Bibliothek zum Erstellen und Modifizieren von PDF Dateien, Hinzufügen & Kopieren von Seiten & Einfügen von Bildern zu PDF via JavaScript.

PDF-Lib ist eine Open Source JavaScript Bibliothek, die es Software-Profis ermöglicht, leistungsstarke Anwendungen für die Arbeit mit PDF Dateien zu entwickeln. Die Bibliothek ist sehr funktional und für jeden modernen JavaScript-Laufzeitbetrieb konzipiert. Es beinhaltet Unterstützung für die Erstellung neuer PDF Dateien, Bearbeitung bestehender PDF Dokumente, Erstellung von Formularen, Hinzufügen oder Entfernen von PDF Seiten, Kopieren von Seiten zwischen PDFs, Zeichnen von Text und Bild.

Die Bibliothek ist Typescript geschrieben und nach rein JavaScript kompiliert, ohne andere Abhängigkeiten. Ein großer Zweck der Schaffung von Bibliotheken mit PDF Bibliotheken war es, das Fehlen einer robusten Unterstützung für PDF Bearbeitungen oder Modifikationen des JavaScript Ökosystems zu beheben. Es gibt verschiedene gute JavaScript Bibliotheken, die die Erstellung von PDF Dateien unterstützen, aber nur sehr wenige unterstützten PDF Änderungen. Die PDF-Lib hat eine komplette Unterstützung für PDF Änderungen sowie die Arbeit in allen JavaScript Umgebungen (nicht nur Knoten oder Browser) enthalten.

Previous Next

Getting Start mit der DF-Lib

Der empfohlene und einfachste Weg zum Starten und Installieren von DF-Lib ist via num sowie in yarn, unten ist der Befehl.

Installation DF-Lib via num

 npm install --save pdf-lib 

Installation DF-Lib über yarn

 yarn add pdf-lib 

PDF Document Creation & Modification über JavaScript

Die Open-Source-Bibliothek PDF-Lib hat vollständige Funktionalität für die Erstellung von PDF Dokumenten sowie Modifikationen enthalten. Software-Entwickler können ein neues PDF Dokument von Grund auf mit nur ein paar Zeilen von JavaScript Code innerhalb ihrer eigenen Anwendungen erstellen. Einmal erstellte Entwickler können Text einfügen, Bilder oder Vektorgrafiken zeichnen, Schriften einbinden, Seiten aus anderen PDFs kopieren und einbinden, Formatierung und Stile ihrer Wahl anwenden und vieles mehr.

How to Create PDF Documents via JavaScript

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()

Kopieren von Seiten zwischen PDF Dokumenten über S Library

Es ist oft sehr nützlich, eine vorhandene Seite zu verwenden, anstatt eine neue Seite in einem PDF Dokument zu erstellen. Die Open Source PDF-Lib Bibliothek ermöglicht es Computerprogrammierern, Seiten aus verschiedenen PDF Dokumenten zu kopieren und sie ohne externe Abhängigkeiten zu ihren gewünschten PDF Dokumenten hinzuzufügen. Zuerst müssen Sie beide PDF Dateien laden, danach können Sie den Befehl copyPages() verwenden, um die gewünschten Seiten zu kopieren, und dann den Befehl addPage() verwenden, um die Seite an der gewünschten Stelle innerhalb der PDF Dokumente hinzuzufügen.

Fügen Sie mithilfe von JavaScript Text zu einer vorhandenen PDF-Datei hinzu

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()

Parke & Lesen Metadata aus PDF Dateien

Die Bibliothek mit PDF Bibliotheken unterstützt vollständig den Zugriff auf und das Lesen von Metadaten von PDF Dokumenten. Metadata ist ein sehr wichtiger Teil von PDF Dokumenten und hat sehr wichtige Informationen über die PDF und ihre Inhalte wie Titel, Thema, Urheber, Copyright-Informationen, Urheber, Datum der Erstellung oder Veränderung usw. enthalten. Die Verwendung von PDF-Lib Bibliotheks-Software-Entwicklern kann Metadaten aus einem PDF Dokument mit nur ein paar Zeilen JavaScript Codes verarbeiten und extrahieren.

Bild mit JavaScript zu PDF hinzufügen


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()

Fügen Sie Anhänge bis PDF über JavaScript API hinzu.

Manchmal müssen wir detailliertere Informationen über eine PDF Datei liefern, damit wir eine andere Datei an diese Datei anhängen können. Der Vorteil dieser Datei besteht nun darin, dass das Anhang mit den PDF geladen wird, wenn Sie es an einen anderen Ort verschieben. Die Open Source PDF-Lib-Bibliothek gibt Software-Entwicklern die Möglichkeit, andere Dateien an ihre PDF Dokumente innerhalb ihrer JavaScript Anwendungen anzuhängen. Es ist möglich, verschiedene Arten von Dateien an PDF anzuhängen, wie zum Beispiel Microsoft Word, Excel, Bilder, Video oder sogar andere PDFer.

Fügen Sie Anhänge zu PDF mit JavaScript

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()
 Deutsch