← Tools

Citation Screenshots with LiteParse

You have a quote from a PDF (say, from an LLM's answer) and want to show where it came from. LiteParse finds the words on the page and renders it, so you can crop a screenshot of the passage. Runs in your browser; the PDF is never uploaded.

1. PDF

Open a PDF, or try the sample report.

2. Quote

Paste all or part of the passage. It doesn't need to be exact: punctuation, quote marks, hyphenation and a few changed words are fine.

How it works

  1. Parse once. LiteParse (a Rust PDF parser compiled to WebAssembly) reads the PDF's text layer. emitWordBoxes adds a box for every word and extractBlocks groups lines into paragraphs, tables and headings in reading order, so a two-column page reads down each column.
  2. Find the words. The quote and the page are reduced to lower-case words without punctuation, then aligned (a Smith-Waterman local alignment over words). The best run of page words becomes the citation; its word boxes, merged per line, are the highlight.
  3. Render one page and crop. A second parse with targetPages and extractScreenshots renders just that page to PNG at 200 dpi. Boxes are in PDF points (1/72 inch), so pixels = points × 200/72.
import init, { LiteParse } from "@llamaindex/liteparse-wasm";
await init();

// 1. Text, word boxes and reading order
const { pages } = await new LiteParse({ emitWordBoxes: true, extractBlocks: true }).parse(pdfBytes);
// pages[i].textItems[j].words[k] = { text, x, y, width, height }   (points, top-left origin)
// pages[i].blocks[b].textItemIndices = the items of one paragraph/table, in reading order

// 2. Your matcher returns the page and the boxes of the quoted words
const { pageNum, bbox } = findQuote(quote, pages)[0];

// 3. Screenshot just that page and crop to the box
const shot = (await new LiteParse({ targetPages: String(pageNum), extractScreenshots: true, dpi: 200 })
  .parse(pdfBytes)).screenshots[0];                    // { imageBytes: PNG, width, height }
const img = await createImageBitmap(new Blob([new Uint8Array(shot.imageBytes)]));
const k = 200 / 72;
ctx.drawImage(img, bbox.x * k, bbox.y * k, bbox.width * k, bbox.height * k, 0, 0, bbox.width * k, bbox.height * k);
  • Exact quotes: LiteParse's own searchItems(items, { phrase }) may be all you need. It returns whole-line boxes and follows the page's top-to-bottom line order, so it misses a quote that wraps within one column of a multi-column page.
  • Scanned PDFs have no text layer. LiteParse can OCR them if you pass an ocrEngine (for example Tesseract.js in a worker); its boxes then work the same way. This demo doesn't include one.
  • In production you'd store the page and box with each citation when the answer is generated, and crop on display.