Skip to content

[BUG] PDF search highlights use raw PDF coordinate space units interpreted as CSS pixels #750

Description

@vipul674

Description of the Bug

In frontend/src/hooks/usePdfSearch.ts, the search text positions are extracted from raw PDF coordinate space:

item.transform[4]  // x position in PDF units (~1/72 inch)
item.transform[5]  // y position in PDF units (~1/72 inch)

In frontend/src/components/chat/PDFViewer.tsx, these values are used directly as CSS pixel values for the highlight overlay:

left: ${r.left}px   // raw PDF unit → used as CSS px
top: ${r.top}px     // raw PDF unit → used as CSS px

PDF coordinate space is measured in points (1/72 inch), while CSS uses pixels (1/96 inch on standard displays). Additionally, the PDF page may be rendered at a different scale factor (e.g., "fit width" or zoom level). The actual coordinates can be orders of magnitude off from the rendered page pixels.

Furthermore, the highlight width and height are hardcoded:

width: 50,
height: 10

These hardcoded values are unrelated to the actual text bounding box.

The result is that search highlights appear in completely wrong positions on the PDF page, making the search feature misleading.

Steps to Reproduce

  1. Open a PDF document in the viewer
  2. Use the search feature to find a word
  3. Observe that the highlight rectangle appears in the wrong position on the page
  4. The highlight size is also incorrect (fixed 50x10px regardless of the text)

Expected Behavior

Search highlight positions should be transformed from PDF coordinate space to CSS pixel space using the page's viewport transform and current zoom level.

Affected Files

  • frontend/src/hooks/usePdfSearch.ts (lines ~40-46)
  • frontend/src/components/chat/PDFViewer.tsx (highlight overlay)

Suggested Fix

Transform PDF coordinates to CSS pixel values:

const viewport = page.getViewport({ scale: currentScale });
const transformed = viewport.convertToViewportPoint(
    item.transform[4], item.transform[5]
);
// Now use transformed coordinates as pixel values

GSSoC '26

  • Yes, I am participating in GirlScript Summer of Code and would like to fix this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    gssocGirlScript Summer of Code 2026 issue/PR

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions