pdf-preview.tsx


Overview

The pdf-preview.tsx file defines a React functional component named PdfPreview designed to render and interact with PDF documents within a web application. It leverages the third-party library react-pdf-highlighter to provide PDF rendering, text and area highlighting, and interactive popups for annotations.

This component enables users to view a PDF file loaded from a URL, display existing highlights on the document, and visualize comments associated with those highlights. It also reports the dimensions of the first PDF page to the parent component for layout or other purposes.

Key features:


Detailed Explanation

Imports and Dependencies


Interface: IProps

export interface IProps {
  highlights: IHighlight[];
  setWidthAndHeight: (width: number, height: number) => void;
  url: string;
}

Purpose: Defines props for the PdfPreview component.

Prop

Type

Description

highlights

IHighlight[]

Array of highlight objects to render on the PDF.

setWidthAndHeight

(width: number, height: number) => void

Callback to report PDF page dimensions.

url

string

URL of the PDF document to load and display.


Component: HighlightPopup

const HighlightPopup = ({
  comment,
}: {
  comment: { text: string; emoji: string };
}) =>
  comment.text ? (
    <div className="Highlight__popup">
      {comment.emoji} {comment.text}
    </div>
  ) : null;

Purpose: Renders a small popup containing a comment associated with a highlight.


Main Component: PdfPreview

const PdfPreview = ({ highlights: state, setWidthAndHeight, url }: IProps) => {
  ...
};

Purpose: Loads and displays a PDF document with interactive highlights and popups.

Internal Variables and Hooks

Important Methods

Effects

Render Logic


Parameters and Return Values

Element

Parameters

Return Value

Notes/Usage

PdfPreview component

IProps object

React element

Renders the PDF viewer with highlights and popups.

HighlightPopup

comment: { text: string; emoji: string }

JSX element or null

Displays a small comment popup for highlights.

highlightTransform

highlight, index, setTip, hideTip, viewportToScaled, screenshot, isScrolledTo

JSX element (Popup with Highlight)

Custom highlight rendering function passed to PdfHighlighter.


Usage Example

import PdfPreview, { IHighlight } from './pdf-preview';

const highlights: IHighlight[] = [
  {
    content: { text: 'Example highlight' },
    position: { boundingRect: { x1: 0, y1: 0, x2: 10, y2: 10 }, rects: [], pageNumber: 1 },
    comment: { text: 'Important note', emoji: '💡' },
    id: '1',
  },
  // more highlights...
];

const MyPdfContainer = () => {
  const [pdfWidth, setPdfWidth] = useState(0);
  const [pdfHeight, setPdfHeight] = useState(0);

  return (
    <PdfPreview
      highlights={highlights}
      url="https://example.com/sample.pdf"
      setWidthAndHeight={(width, height) => {
        setPdfWidth(width);
        setPdfHeight(height);
      }}
    />
  );
};

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Component Diagram

componentDiagram
    component PdfPreview {
        +highlights: IHighlight[]
        +setWidthAndHeight(width: number, height: number)
        +url: string
        -- Internal --
        +ref: React.Ref<(highlight: IHighlight) => void>
        +error: string | null
        +resetHash(): void
        +useEffect() // scrolls to first highlight on update
    }
    component PdfLoader {
        +url: string
        +beforeLoad: JSX.Element
        +workerSrc: string
        +errorMessage: JSX.Element
        +children: (pdfDocument) => JSX.Element
    }
    component PdfHighlighter {
        +pdfDocument: any
        +enableAreaSelection(event): boolean
        +onScrollChange(): void
        +scrollRef(scrollTo): void
        +onSelectionFinished(): void
        +highlightTransform(...)
        +highlights: IHighlight[]
    }
    component HighlightPopup {
        +comment: { text: string; emoji: string }
    }
    component Highlight
    component AreaHighlight
    component Popup

    PdfPreview --> PdfLoader : loads PDF
    PdfLoader --> PdfHighlighter : renders PDF and highlights
    PdfHighlighter --> Popup : wraps highlights with popups
    Popup --> HighlightPopup : renders comment content
    Popup --> Highlight : for text highlights
    Popup --> AreaHighlight : for area/image highlights

Summary

The pdf-preview.tsx file provides a highly reusable and interactive PDF viewer component with annotation capabilities. By integrating with react-pdf-highlighter, it supports complex highlighting features while maintaining responsiveness and error handling. Its design allows easy integration into larger applications requiring document preview and annotation functionality.