Skip to content

getMediaBox()/getCropBox() return raw x2/y2 as width/height for non-zero-origin boxes #81

Description

@alfikiafan

Environments

LibPDF version: 0.4.0
Node version: v24.11.1

Minimal reproduction

import { PDF, PdfArray, PdfNumber } from "@libpdf/core";

const pdf = PDF.create();
const page = pdf.addPage({ width: 400, height: 600 });

// Simulate a box with a non-zero origin (e.g. a page that has already been cropped once)
page.dict.set("MediaBox", new PdfArray([
  PdfNumber.of(10),
  PdfNumber.of(25),
  PdfNumber.of(380),
  PdfNumber.of(550),
]));

console.log(page.getMediaBox());
// { x: 10, y: 25, width: 380, height: 550 }

Expected behavior

width/height should be the box's actual dimensions: width = x2 - x1 = 370, height = y2 - y1 = 525.

Actual behavior

width/height are the raw x2/y2 array values (380, 550), not the computed box size. This only manifests when the box's origin isn't (0, 0) — e.g. a page that has already been cropped once — so it's easy to miss in testing, since x2 - 0 === x2.

Likely cause

getBox() in dist/index.mjs (~line 28265) returns { x: x1.value, y: y1.value, width: x2.value, height: y2.value } without subtracting the origin. This affects getMediaBox(), getCropBox(), and their fallbacks getBleedBox()/getTrimBox()/getArtBox().

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions