@pdfa-lab/core / PDFALab
Class: PDFALab ​
Defined in: pdfa-lab.ts:99
Accessors ​
pdfDocument ​
Get Signature ​
get pdfDocument():
PDFDocument
Defined in: pdfa-lab.ts:112
Returns the internally used PDFDocument. Changing the structure of this document, and then calling other PDFALab methods, may result in undefined behaviour and is strongly discouraged.
Returns ​
PDFDocument
the internally used PDFDocument
Methods ​
collectFonts() ​
collectFonts():
Map<string,FontInfo>
Defined in: pdfa-lab.ts:352
Collects all fonts used in the PDF.
Returns ​
Map<string, FontInfo>
a Map with keys as PDFRef (reference of the font) and values as FontInfo
embedFonts() ​
embedFonts(
fontkit,options?,references?):Promise<void>
Defined in: pdfa-lab.ts:232
Embed multiple fonts, but only if they are not already embedded.
If no references were passed, all currently missing fonts are embedded.
Parameters ​
| Parameter | Type | Description |
|---|---|---|
fontkit | FontkitAPI | a fontkit instance |
options | FontEmbedOptions | control the font embedding |
references? | PDFRef[] | can be determined by collectFonts() |
Returns ​
Promise<void>
extractText() ​
extractText():
Promise<TextBlock[]>
Defined in: pdfa-lab.ts:369
Extract all tags from the PDF.
Returns ​
Promise<TextBlock[]>
an array of TextBlock objects.
extractXmp() ​
extractXmp(
format?,baseIRI?,options?):string|null
Defined in: pdfa-lab.ts:389
Extract XMP meta information from the PDF.
Parameters ​
| Parameter | Type | Default value | Description |
|---|---|---|---|
format | string | 'application/rdf+xml' | the desired serialisation format (default: application/rdf+xml) |
baseIRI | string | DEFAULT_BASE_IRI | the base IRI (default: urn:xmp:doc) |
options | RdfSerialisationOptions | {} | serialisation options for certain formats, see RdfSerialisationOptions |
Returns ​
string | null
the serialised XMP or null if no meta information available
makePDFA() ​
makePDFA(
options?):Promise<void>
Defined in: pdfa-lab.ts:213
Convert a document to PDF/A.
Parameters ​
| Parameter | Type | Description |
|---|---|---|
options | PDFAConversionOptions | - |
Returns ​
Promise<void>
save() ​
save():
Promise<Uint8Array<ArrayBufferLike>>
Defined in: pdfa-lab.ts:128
Serialises the internally used PDF into a sequence of bytes. As a side effect, it also updates internal structures of the PDFDocument into a stable version.
The method is a thin wrapper around the save() method of PDFDocument. Fine-tuning the saving process can be achieved by getting the pdfDocument, and then calling the save() method with the desired options.
Returns ​
Promise<Uint8Array<ArrayBufferLike>>
setXmpMetadataPacket() ​
setXmpMetadataPacket(
rdfxml):void
Defined in: pdfa-lab.ts:429
Parameters ​
| Parameter | Type |
|---|---|
rdfxml | string |
Returns ​
void
from() ​
staticfrom(input):Promise<PDFALab>
Defined in: pdfa-lab.ts:176
Creates a PDFALab instance from a variety of PDF-like inputs and normalizes them into a single canonical @cantoo/pdf-lib document representation.
This method performs a pre-processing step to ensure that the returned PDFDocument has a consistent internal object graph.
Why ​
PDF libraries such as pdf-lib (and forks like @cantoo/pdf-lib) rely heavily on runtime identity checks such as instanceof PDFDict, instanceof PDFArray, etc.
In bundled or duplicated dependency scenarios (e.g. monorepos, multiple builds, or linked packages), the same logical PDF object type may exist in multiple constructor instances. This causes instanceof checks to fail even though the objects are structurally identical, leading to weird runtime errors such as:
- "Expected instance of PDFDict but got instance of PDFDict"
To prevent this, the document is always serialised and reloaded. This forces all internal objects to be reconstructed using the active @cantoo/pdf-lib runtime, ensuring consistent prototype chains and reliable instanceof behaviour.
Behaviour ​
- If a raw PDF input is provided (base 64 encoded string, data URI, or binary), it is directly loaded.
- If a
PDFDocumentfrom another runtime instance is detected, it is first serialized viasave()and then reloaded viaPDFDocument.load(). - This process guarantees a canonical internal representation of the PDF.
This step is intentionally non-trivial and may be expensive, but is required for correctness in environments where multiple copies or builds of pdf-lib may exist.
Parameters ​
| Parameter | Type | Description |
|---|---|---|
input | string | ArrayBuffer | Uint8Array<ArrayBufferLike> | PDFDocument | A PDF source: raw bytes, base64 string, data URI, or a PDFDocument instance. |
Returns ​
Promise<PDFALab>
A normalized PDFALab instance backed by a canonical PDF document.