Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added FastAPI documentation #760

Merged
merged 7 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/),
and [PEP 440](https://www.python.org/dev/peps/pep-0440/).

### Added
- Documentation on how to use `fpdf2` with [FastAPI](https://fastapi.tiangolo.com/): <https://pyfpdf.github.io/fpdf2/UsageInWebAPI.html#FastAPI> thanks to @KamarulAdha

## Displaying deprecation warnings
`DeprecationWarning` messages are not displayed by Python by default.

Expand Down
56 changes: 56 additions & 0 deletions docs/UsageInWebAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,59 @@ Check [tutorial/notebook.ipynb](https://github.com/PyFPDF/fpdf2/blob/master/tuto
Usage of the original PyFPDF lib with [web2py](http://www.web2py.com/) is described here: <https://github.com/reingart/pyfpdf/blob/master/docs/Web2Py.md>

`v1.7.2` of PyFPDF is included in `web2py` since release `1.85.2`: <https://github.com/web2py/web2py/tree/master/gluon/contrib/fpdf>


## FastAPI ##
[FastAPI](https://fastapi.tiangolo.com/) is:
> a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.

The following code shows how to generate a PDF file via a POST endpoint that receives a JSON object. The JSON object can be used to write into the PDF file. The generated PDF file will be returned back to the user/frontend as the response.


```python
from fastapi import FastAPI, Request, Response

from fpdf import FPDF

import warnings
warnings.filterwarnings("ignore")
Lucas-C marked this conversation as resolved.
Show resolved Hide resolved


app = FastAPI()


@app.post("/send_data")
async def create_pdf(request: Request):
"""
POST endpoint that accepts a JSON object
This endpoint returns a PDF file as the response
"""
try:
# data will read the JSON object and can be accessed like a Python Dictionary
# The contents of the JSON object can be used to write into the PDF file (if needed)
data = await request.json()


# Create a sample PDF file
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=24)
pdf.cell(txt="hello world")
# pdf.cell(txt=data["content"]) # Using the contents of the JSON object to write into the PDF file
# Use str(data["content"]) if the content is non-string type


# Prepare the filename and headers
filename = "<file_name_here>.pdf"
headers = {
"Content-Disposition": f"attachment; filename={filename}"
}


# Return the file as a response
return Response(content=bytes(pdf.output()), media_type="application/pdf", headers=headers)


except Exception as e:
return {"error": str(e)}
Lucas-C marked this conversation as resolved.
Show resolved Hide resolved
```