Why PDF coordinates flip on the Y axis
A PDF measures from the bottom-left corner in points, a screen measures from the top-left in pixels. Here is the one-line conversion, and the edge cases that break it.
Manoj Mondal · · 3 min read
- #canvas
The first time I placed a signature box on a PDF, it landed in the right spot horizontally and in the wrong spot vertically. Drag it near the top of the page, and the saved file put it near the bottom. Nothing was broken. The PDF and the screen just disagree about which way is up.
Two coordinate systems
A PDF page describes positions in user space. Its origin is the bottom-left corner of the page, x grows to the right, and y grows upward. The unit is the point, 1/72 of an inch, so an A4 page is about 595 × 842 points.
A screen, a <canvas> and CSS all start from the top-left corner, x grows to the right, and y grows downward. The unit is the CSS pixel.
The difference is history. PDF inherited its imaging model from PostScript, which used the mathematician's convention: the origin sits at the bottom-left, like a graph you draw in school. Displays draw rows of pixels from the top of the screen to the bottom, so screen coordinates start at the top.
So x only needs scaling. Y needs scaling and flipping.
The conversion
First work out the scale: how many screen pixels one PDF point takes up at the current zoom.
const s = renderedPageWidthPx / pageWidthPt; // e.g. 300 / 595 ≈ 0.50
A box in a PDF is usually stored by its bottom-left corner (x, y) and its size (w, h), in points. On screen you want its top-left corner in pixels:
function pdfToScreen(box: { x: number; y: number; w: number; h: number }, pageHeightPt: number, s: number) {
return {
left: box.x * s,
top: (pageHeightPt - box.y - box.h) * s,
width: box.w * s,
height: box.h * s,
};
}
The - box.h is the part people forget. pageHeightPt - box.y finds the box's bottom edge measured from the top; subtracting the height moves you up to its top edge, which is what CSS and canvas expect.
Going back, from a drag on screen to the numbers you save in the PDF, is the same formula turned around:
const x = left / s;
const y = pageHeightPt - top / s - h;
Keep the PDF numbers as the truth
In the signing tool I built, the box's position is stored only in PDF points. Pixels are worked out from it every time the view changes. When the user zooms or scrolls, only the scale and the offset change; the PDF numbers stay put, like a street address on a moving map. You can see it in the coordinate demo on my canvas: drag the box, then zoom, and watch which numbers move.
Doing it the other way round, storing pixels and converting on save, means every zoom level and screen size gives you a slightly different answer once rounding creeps in.
Where the simple formula breaks
- The page box doesn't start at 0, 0. A page's
MediaBoxorCropBoxis usually[0 0 612 792], but it can just as well be[36 36 648 828]. Subtract the box's lower-left corner first. - Rotated pages. A page with
/Rotate 90is stored sideways and displayed upright, so x and y swap roles. Handle rotation before the flip. - High-density screens. A canvas on a 2× display has twice as many backing pixels as CSS pixels. Keep layout in CSS pixels and multiply by
devicePixelRatioonly when drawing.
If you render with PDF.js, its viewport already knows the scale, rotation and offset. viewport.convertToViewportPoint(x, y) goes from PDF to screen and viewport.convertToPdfPoint(x, y) comes back, which covers all three cases.
The short version
PDF: origin bottom-left, y up, points. Screen: origin top-left, y down, pixels. Scale x, and for y take the page height, subtract the y and the height, then scale.