|
| 1 | +--- |
| 2 | +title: "PDF viewer" |
| 3 | +--- |
| 4 | + |
| 5 | +The `Pdf` class allows you to display a PDF hosted remotely or locally in the chatbot UI. This class either takes a URL of a PDF hosted online, or the path of a local PDF. |
| 6 | + |
| 7 | +### Attributes |
| 8 | + |
| 9 | +<ParamField path="name" type="str"> |
| 10 | + The name of the PDF to be displayed in the UI. |
| 11 | +</ParamField> |
| 12 | + |
| 13 | +<ParamField path="display" type="ElementDisplay" optional> |
| 14 | + Determines how the PDF element should be displayed in the UI. Choices are |
| 15 | + "side" (default), "inline", or "page". |
| 16 | +</ParamField> |
| 17 | + |
| 18 | +<ParamField path="url" type="str"> |
| 19 | + The remote URL of the PDF file. Must provide url for a remote PDF (or either path or content for a local PDF). |
| 20 | +</ParamField> |
| 21 | + |
| 22 | +<ParamField path="path" type="str" optional> |
| 23 | + The local file path of the PDF. Must provide either path or content for a local PDF (or url for a remote PDF). |
| 24 | +</ParamField> |
| 25 | + |
| 26 | +<ParamField path="content" type="bytes" optional> |
| 27 | + The file content of the PDF in bytes format. Must provide either path or |
| 28 | + content for a local PDF (or url for a remote PDF). |
| 29 | +</ParamField> |
| 30 | + |
| 31 | +### Usage with message scope |
| 32 | + |
| 33 | +```python Code Example |
| 34 | +import chainlit as cl |
| 35 | + |
| 36 | +# Sending a pdf with the local file path |
| 37 | +elements = [ |
| 38 | + cl.Pdf(name="pdf1", display="inline", path="./pdf1.pdf") |
| 39 | +] |
| 40 | + |
| 41 | +cl.Message(content="Look at this local pdf!", elements=elements).send() |
| 42 | +``` |
| 43 | + |
| 44 | +### Usage without scope |
| 45 | + |
| 46 | +```python Code Example |
| 47 | +import chainlit as cl |
| 48 | + |
| 49 | +# Sending a pdf with the remote url |
| 50 | +pdf1 = cl.Pdf(name="pdf1", display="inline", url="https://example.com/example.pdf") |
| 51 | +pdf1.send() |
| 52 | + |
| 53 | +# Sending a pdf with file content as bytes |
| 54 | +with open("./pdf2.pdf", "rb") as f: |
| 55 | + pdf_content = f.read() |
| 56 | + pdf2 = cl.Pdf(name="pdf2", display="inline", content=pdf_content) |
| 57 | + pdf2.send() |
| 58 | +``` |
0 commit comments