I am new to Python and I came up with a requirement for created PDF files with data available in SQL server using Python script. As I researched, there are many libraries which can be used for this purpose but most of their approach is to generate HTML string then convert it into PDF file in a local directory. But none of them suggested how to do it on Azure storage.
I am having an Azure function app which connects to DB and reads data, now I need to create PDF using this data. Since Azure function app is a serverless source and can't relay on a physical directory at all.
So I should be able to create a string with data and convert them into PDF and upload them directly to Azre storage.
import logging
import azure.functions as func
from fpdf import FPDF
def generate_PDF():
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", 12)
pdf.cell(w=0,h=0,txt="This is sample pdf",align="L")
pdf.output('demo.pdf')
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
c_pdf = req_body.get('cpdf')
if c_pdf == 'Y':
generate_PDF()
if name:
return func.HttpResponse(f"Hello {name}!, create PDF (cpdf) is set to {c_pdf}")
else:
return func.HttpResponse("Please pass a name on the query string or in the request body", status_code=400)
I am using FPDF library, and it creates pdf file in the current working directory.
Suggest me the best way for my approach.
Thanks in advance.
Regarding the issue, please refer to the following code
sdk
pip install azure-storage-blob fpdf aiohttp
code
from azure.storage.blob import ContentSettings
from azure.storage.blob.aio import BlobServiceClient
from fpdf import FPDF
async def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
connection_string = 'DefaultEndpointsProtocol=https;AccountName=jimtestdiag924;AccountKey=uxz4AtF0A4tWBcPHwgbFAfdinvLEZpJtAu1MYVWD/xYCYDcLLRb8Zhp5lxR2/2rQ2P1OrxZwWarEoWyDSZ7Q+A==;EndpointSuffix=core.windows.net'
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(40, 10, 'Hello World!')
s = pdf.output(dest='S').encode('latin-1')
logging.info(s)
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
async with blob_service_client:
container_client = blob_service_client.get_container_client('testupload')
try:
# Create new Container in the Service
await container_client.create_container()
except Exception as ex:
pass
# Get a new BlobClient
blob_client = container_client.get_blob_client('demo.pdf')
await blob_client.upload_blob(s, blob_type="BlockBlob",content_settings=ContentSettings( content_type='application/pdf'))
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(f"Hello {name}!")
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)