fix(print): align actual-size paint to physical paper origin

Windows drivers often report paperRect with a hard-margin inset; drawing
there shifted the Letter page and clipped label edges at 100% scale.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Michael-A-SloanandCursor committed 2026-07-28 18:36:04 -04:00
1 parent 5dcc298f88
commit 7105678226
1 file changed
+102 -6
+102 -6
View File
@@ -82,8 +82,11 @@ def _force_actual_size_layout(printer: QPrinter) -> None:
QPageLayout.Unit.Inch, QPageLayout.Unit.Inch,
) )
layout.setMode(QPageLayout.Mode.FullPageMode) layout.setMode(QPageLayout.Mode.FullPageMode)
layout.setMargins(QMarginsF(0, 0, 0, 0))
printer.setPageLayout(layout) printer.setPageLayout(layout)
printer.setFullPage(True) printer.setFullPage(True)
# Keep software margins at zero even if the dialog changed them.
printer.setPageMargins(QMarginsF(0, 0, 0, 0), QPageLayout.Unit.Inch)
# QPrinter has no "fit to page" API; drivers may still offer it in Preferences. # QPrinter has no "fit to page" API; drivers may still offer it in Preferences.
# Our paint path never scales to the printable area. # Our paint path never scales to the printable area.
@@ -100,6 +103,91 @@ def _printer_dpi(printer: QPrinter) -> tuple[float, float]:
return dpi_x, dpi_y return dpi_x, dpi_y
def _win_hard_margin_device(printer: QPrinter) -> tuple[float, float] | None:
"""Windows PHYSICALOFFSETX/Y scaled into QPrinter device pixels."""
if sys.platform != "win32":
return None
name = printer.printerName()
if not name:
return None
try:
import win32ui
except ImportError:
return None
# DEVMODE caps: PHYSICALOFFSETX/Y = printable origin inset from paper edge.
physical_offset_x = 112
physical_offset_y = 113
log_pixels_x = 88
log_pixels_y = 90
hdc = win32ui.CreateDC()
try:
hdc.CreatePrinterDC(name)
ox = float(hdc.GetDeviceCaps(physical_offset_x))
oy = float(hdc.GetDeviceCaps(physical_offset_y))
dc_dpi_x = float(hdc.GetDeviceCaps(log_pixels_x)) or 1.0
dc_dpi_y = float(hdc.GetDeviceCaps(log_pixels_y)) or 1.0
except Exception:
return None
finally:
try:
hdc.DeleteDC()
except Exception:
pass
dpi_x, dpi_y = _printer_dpi(printer)
return ox * (dpi_x / dc_dpi_x), oy * (dpi_y / dc_dpi_y)
def _paper_origin_in_paint_coords(printer: QPrinter) -> tuple[float, float]:
"""
Device-pixel position of physical paper (0,0) in the QPainter space.
Qt documents that with fullPage=True, painter (0,0) is the paper corner.
With fullPage=False, painter (0,0) is the printable-area corner — Avery
geometry must then be shifted by -hardMargin so sheet-edge measurements
still land on the physical page.
Never use paperRect.left()/top() alone as the draw origin: on Windows those
values are often the hard-margin inset, which double-shifts the job and
clips the opposite edge (the incomplete print on Avery sheets).
"""
paper = printer.paperRect(QPrinter.Unit.DevicePixel)
page = printer.pageRect(QPrinter.Unit.DevicePixel)
layout = printer.pageLayout()
res = int(float(printer.resolution()) or _PRINT_DPI_FALLBACK)
try:
full = layout.fullRectPixels(res)
paint = layout.paintRectPixels(res)
if full.isValid() and paint.isValid():
paper = QRectF(full)
page = QRectF(paint)
except (AttributeError, TypeError, ValueError):
pass
same_size = (
abs(page.width() - paper.width()) < 2.0
and abs(page.height() - paper.height()) < 2.0
)
if printer.fullPage() and same_size:
# True full-page mode: paper corner is already painter (0,0).
return 0.0, 0.0
# Painter (0,0) tracks the printable/paint rect. Map paper (0,0) into that space.
origin_x = paper.left() - page.left()
origin_y = paper.top() - page.top()
# Some drivers report both rects at (0,0) with only a smaller page size.
# Fall back to the Windows hard-margin inset so we still align to the sheet.
if abs(origin_x) < 1.0 and abs(origin_y) < 1.0 and not same_size:
hard = _win_hard_margin_device(printer)
if hard is not None:
return -hard[0], -hard[1]
return origin_x, origin_y
def _remember_printer(printer: QPrinter) -> None: def _remember_printer(printer: QPrinter) -> None:
global _last_printer_name global _last_printer_name
name = printer.printerName() name = printer.printerName()
@@ -136,7 +224,11 @@ def _paint_pages_on_printer(
_force_actual_size_layout(printer) _force_actual_size_layout(printer)
dpi_x, dpi_y = _printer_dpi(printer) dpi_x, dpi_y = _printer_dpi(printer)
paper = printer.paperRect(QPrinter.Unit.DevicePixel) origin_x, origin_y = _paper_origin_in_paint_coords(printer)
# Prefer template Letter size so rounding in the raster cannot drift scale.
page_w_in = float(TEMPLATE.page_width_in) or 8.5
page_h_in = float(TEMPLATE.page_height_in) or 11.0
painter = QPainter(printer) painter = QPainter(printer)
try: try:
@@ -148,15 +240,19 @@ def _paint_pages_on_printer(
for i, image in enumerate(images): for i, image in enumerate(images):
if i > 0: if i > 0:
printer.newPage() printer.newPage()
# Drivers can reset layout on newPage — keep actual-size + origin.
_force_actual_size_layout(printer)
origin_x, origin_y = _paper_origin_in_paint_coords(printer)
# Physical inches represented by this raster (from PDF Letter page). width_in = image.width() / source_dpi if source_dpi > 0 else page_w_in
width_in = image.width() / source_dpi height_in = image.height() / source_dpi if source_dpi > 0 else page_h_in
height_in = image.height() / source_dpi # Keep 100% actual size; only fall back to template Letter if raster is wild.
if abs(width_in - page_w_in) > 0.05 or abs(height_in - page_h_in) > 0.05:
width_in, height_in = page_w_in, page_h_in
# Exact device pixels for those inches — NOT paperRect width/height.
dest_w = width_in * dpi_x dest_w = width_in * dpi_x
dest_h = height_in * dpi_y dest_h = height_in * dpi_y
dest = QRectF(paper.left(), paper.top(), dest_w, dest_h) dest = QRectF(origin_x, origin_y, dest_w, dest_h)
painter.drawImage(dest, image, QRectF(image.rect())) painter.drawImage(dest, image, QRectF(image.rect()))
finally: finally: