Skip to content

Commit 557f02f

Browse files
AntikodeAntikode
authored andcommitted
fix crop tool: use page MediaBox origin, not (0,0), when setting CropBox
Pages whose MediaBox doesn't start at (0,0) (common in scanned/print-shop PDFs) were cropped at the wrong absolute position, since setCropBox(x,y,w,h) takes page-space absolute coordinates, not margin offsets.
1 parent f7024e5 commit 557f02f

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

crop.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,14 @@ async function cropPdf() {
166166
}
167167

168168
pages.forEach((page) => {
169-
const { width, height } = page.getSize();
170-
const cropWidth = width - left - right;
171-
const cropHeight = height - top - bottom;
172-
page.setCropBox(left, bottom, cropWidth, cropHeight);
169+
// setCropBox(x, y, w, h) takes an absolute page-space origin, not a
170+
// (0,0)-relative one — pages whose MediaBox doesn't start at (0,0)
171+
// (common in scanned/print-shop PDFs) need the margins offset by the
172+
// MediaBox's own x/y, or the crop lands in the wrong place on the page.
173+
const box = page.getMediaBox();
174+
const cropWidth = box.width - left - right;
175+
const cropHeight = box.height - top - bottom;
176+
page.setCropBox(box.x + left, box.y + bottom, cropWidth, cropHeight);
173177
});
174178

175179
const bytes = await doc.save();

0 commit comments

Comments
 (0)