Skip to content

PDF

The PDF widget displays a PDF document inside a Mercury App using an embedded iframe. It is useful for showing reports, invoices, slides, and generated documents directly in your app.

The widget reads a local PDF file and embeds it as a data: URL, so the document can be rendered reliably in the Mercury UI.

import mercury as mr
mr.PDF(
file_path="report.pdf",
label="Monthly report",
height="900"
)

Use position to control where the PDF viewer is displayed:

  • "inline" — in the notebook flow (default)
  • "sidebar" — in the sidebar
  • "bottom" — after all notebook cells
import mercury as mr
mr.PDF(
file_path="slides.pdf",
position="bottom",
label="Slides"
)
import mercury as mr
mr.PDF(
file_path="report.pdf",
width="100%"
)

Default: "100%"

import mercury as mr
mr.PDF(
file_path="report.pdf",
height="600"
)

Default: "800"

A common workflow is to generate a PDF first (for example with ReportLab), save it to disk, then display it with mr.PDF().

import mercury as mr
from reportlab.pdfgen import canvas
pdf_path = "hello.pdf"
c = canvas.Canvas(pdf_path)
c.drawString(72, 720, "Hello from Mercury ✅")
c.save()
mr.PDF(
file_path=pdf_path,
label="Generated PDF"
)

type: string | None

Path to a local PDF file.

If None, the widget is created without a document (empty iframe).

Default: None


type: string

Optional label displayed above the viewer.

Default: ""


type: string

CSS width of the iframe (for example "100%", "800px").

Default: "100%"


type: string

Height of the iframe. Usually a pixel value like "800" or "900".

Default: "800"


type: "sidebar" | "inline" | "bottom"

Controls where the widget is rendered.

Default: "inline"


type: string

Unique identifier used to distinguish widgets with identical arguments.


  • The PDF is embedded using an iframe with a data:application/pdf;base64,... URL.
  • Very large PDFs may increase memory usage because they are encoded into base64.
  • The widget is best used for local PDF files. If you want to show remote PDFs, download them locally first.