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

Method load

src/api/pdf.ts:384–472  ·  view source on GitHub ↗
(bytes: Uint8Array, options?: LoadOptions)

Source from the content-addressed store, hash-verified

382 */
383 // oxlint-disable-next-line typescript/require-await
384 static async load(bytes: Uint8Array, options?: LoadOptions): Promise<PDF> {
385 const scanner = new Scanner(bytes);
386 const parser = new DocumentParser(scanner, options);
387
388 const parsed = parser.parse();
389
390 // Create registry from xref
391 const registry = new ObjectRegistry(parsed.xref);
392
393 // Detect linearization by checking first object
394 let isLinearized = false;
395
396 try {
397 // Find the first object (lowest object number)
398 const firstObjNum = min(parsed.xref.keys(), 0);
399
400 if (firstObjNum > 0) {
401 const firstObj = parsed.getObject(PdfRef.of(firstObjNum, 0));
402
403 if (firstObj instanceof PdfDict && isLinearizationDict(firstObj)) {
404 isLinearized = true;
405 }
406 }
407 } catch {
408 // Ignore errors during linearization detection
409 }
410
411 // Find original xref offset and detect format
412 const xrefParser = new XRefParser(scanner);
413 let originalXRefOffset: number;
414 let usesXRefStreams = false;
415
416 try {
417 originalXRefOffset = xrefParser.findStartXRef();
418 // Detect if the original uses XRef streams
419 const format = xrefParser.detectXRefFormat(originalXRefOffset);
420 usesXRefStreams = format === true;
421 } catch {
422 originalXRefOffset = 0;
423 }
424 // Set up resolver on registry before loading anything
425 registry.setResolver(ref => parsed.getObject(ref));
426
427 // Load catalog through registry so it's tracked for changes
428 const rootRef = parsed.trailer.getRef("Root");
429
430 if (!rootRef) {
431 throw new Error("Document has no catalog (missing /Root in trailer)");
432 }
433
434 const catalogDict = registry.resolve(rootRef);
435
436 if (!catalogDict || !(catalogDict instanceof PdfDict)) {
437 throw new Error("Document has no catalog");
438 }
439
440 const pdfCatalog = new PDFCatalog(catalogDict, registry);
441 const pagesRef = catalogDict.getRef("Pages");

Calls 12

parseMethod · 0.95
findStartXRefMethod · 0.95
detectXRefFormatMethod · 0.95
setResolverMethod · 0.95
resolveMethod · 0.95
minFunction · 0.90
isLinearizationDictFunction · 0.90
keysMethod · 0.80
emptyMethod · 0.80
getObjectMethod · 0.65
ofMethod · 0.45
getRefMethod · 0.45

Tested by

no test coverage detected