MCPcopy Create free account
hub / github.com/LibPDF-js/core / create

Method create

src/api/pdf.ts:595–663  ·  view source on GitHub ↗

* Create a new empty PDF document. * * @returns A new PDF document with no pages * * @example * ```typescript * const pdf = PDF.create(); * pdf.addPage({ size: "letter" }); * const bytes = await pdf.save(); * ```

()

Source from the content-addressed store, hash-verified

593 * ```
594 */
595 static create(): PDF {
596 // Create minimal PDF structure
597 const registry = new ObjectRegistry(new Map());
598
599 // Create empty pages tree
600 const pagesDict = PdfDict.of({
601 Type: PdfName.of("Pages"),
602 Kids: new PdfArray([]),
603 Count: PdfNumber.of(0),
604 });
605 const pagesRef = registry.register(pagesDict);
606
607 // Create catalog
608 const catalogDict = PdfDict.of({
609 Type: PdfName.of("Catalog"),
610 Pages: pagesRef,
611 });
612 const catalogRef = registry.register(catalogDict);
613
614 // Create trailer pointing to catalog
615 const trailer = PdfDict.of({
616 Root: catalogRef,
617 });
618
619 // Set resolver (returns from registry)
620 registry.setResolver((ref: PdfRef) => registry.getObject(ref));
621
622 const pdfCatalog = new PDFCatalog(catalogDict, registry);
623
624 const pages = PDFPageTree.fromRoot(pagesRef, pagesDict, ref => {
625 const obj = registry.getObject(ref);
626
627 if (obj instanceof PdfDict) {
628 return obj;
629 }
630
631 return null;
632 });
633
634 // Create document info for a new document
635 const info: DocumentInfo = {
636 version: "1.7",
637 securityHandler: null,
638 isEncrypted: false,
639 isAuthenticated: true,
640 trailer,
641 };
642
643 const ctx = new PDFContext(registry, pdfCatalog, pages, info);
644
645 const pdf = new PDF(ctx, new Uint8Array(0), 0, {
646 isNewlyCreated: true,
647 recoveredViaBruteForce: false,
648 isLinearized: false,
649 usesXRefStreams: false,
650 });
651
652 // Set default metadata for new documents

Callers

nothing calls this directly

Calls 6

registerMethod · 0.95
setResolverMethod · 0.95
getObjectMethod · 0.95
setMetadataMethod · 0.95
fromRootMethod · 0.80
ofMethod · 0.45

Tested by

no test coverage detected