Skip to content

Commit

Permalink
Added FastAPI documentation. Send PDF via SMTP (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
KamarulAdha authored Apr 12, 2023
1 parent 469fd0a commit a3cf95d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
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
53 changes: 53 additions & 0 deletions docs/UsageInWebAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,56 @@ 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, HTTPException, status
from fpdf import FPDF


app = FastAPI()


@app.post("/send_data", status_code=status.HTTP_200_OK)
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:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))

```

0 comments on commit a3cf95d

Please sign in to comment.