Creating an Azure Function that automatically triggers when a PDF is uploaded, splits it into 50-page chunks, and saves them back to blob storage.
Log in to the Azure Portal. Search for "Storage accounts" -> + Create.
Basics:
- Subscription: Azure for Students.
- Resource Group: Create new
- Name: Unique name (bookstorage1001).
- Region: Korea Central (Korea Central hi krna vrna error aayega ).
- Performance: Standard.
- Redundancy: LRS (Locally-redundant storage) Click Review + create -> Create.
Go to your new Storage Account resource. Sidebar: Data storage -> Containers. Click + Container twice to create these two "EXACT“ folders:
input-pdfsoutput-chunks
Get Connection String: Sidebar: Security + networking -> Access keys. Show and Copy the Connection string for key1.
Use —> "App Service Plan" Search for "Function App" -> + Create.
Basics:
- Resource Group: Select Prev one
- Name: pdf-splitter
- Runtime stack: Python
- Version: 3.11
- Region: Korea Central
- Operating System: Linux
- Plan type: Functions Premium / App Service Plan
Plans Tab: Click Create new plan. Pricing Tier: Click "Change size". Select Basic B1 (100 total ACU, 1.75 GB memory, 1 vCPU)
Storage Tab: Select the storage account created in Step 1. Click Review + create -> Create.
Create a folder named PdfProject and open it in VS Code. It should look like this:
SplitPdf/
├── .funcignore
├── function_app.py
├── host.json
├── local.settings.json
└── requirements.txt
requirements.txt
azure-functions
azure-storage-blob
pypdf
.funcignore (CRITICAL Step) This prevents your local Mac/Windows libraries from crashing the Linux server.
.git
.vscode
.venv
__pycache__
local.settings.json
host.json This ensures the Blob Trigger extension is installed.
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}local.settings.json. Paste storage account Connection String from Step 1
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsStorage": "PASTE_YOUR_CONNECTION_STRING_HERE"
}
}note : naam function_app.py hi rakhna !!, Code :
import logging
import io
import os
import azure.functions as func
from azure.storage.blob import BlobServiceClient
from pypdf import PdfReader, PdfWriter
app = func.FunctionApp()
@app.blob_trigger(arg_name="myblob", path="input-pdfs/{name}", connection="AzureWebJobsStorage")
def PdfChunker(myblob: func.InputStream):
filename = myblob.name.split('/')[-1]
file = myblob.read()
try :
input_stream = io.BytesIO(file)
reader = PdfReader(input_stream)
total_pg = len(reader.pages)
chunk_s = 50
connect_str = os.environ["AzureWebJobsStorage"]
blob_client = BlobServiceClient.from_connection_string(conn_str=connect_str)
OutputContainer = blob_client.get_container_client("output-chunks")
for i in range (0,total_pg, chunk_s):
writer = PdfWriter()
st = i
end = min(i+chunk_s, total_pg)
for page in range(st, end):
writer.add_page(reader.pages[page])
output_stream = io.BytesIO()
writer.write(output_stream)
output_stream.seek(0) #to reset cursor at 0 again
out_file = f"{filename.replace('.pdf', '')}_part_{(i // chunk_s) + 1}.pdf"
OutputContainer.upload_blob(name=out_file, data=output_stream, overwrite=True)
logging.info("Uploaded and Stored Splitted Pdf's in output-pdfs folder !")
except Exception as e:
logging.error(f"error : {e}")Open VS Code in the PdfProject folder. Install Azure Resource and Azure Function Extension in Vs Code. Press Cmd + Shift + P (Mac) or Ctrl + Shift + P (Win). Type: Azure Functions: Deploy to Function App. Select your subscription. Select the Function App created. Deploy.
In the Portal, click Log Stream (under Monitoring). Open a new tab, go to Storage Account -> Containers -> input-pdfs. Upload a PDF file. Watch the Log Stream: You should see Python Blob trigger function processing file.... Check output-chunks: Your split files will appear there.
Go to the Azure Portal -> Function App -> Settings -> Environment variables.
Ensure AzureWebJobsStorage exists and contains the long connection string starting with DefaultEndpointsProtocol=https....
In Sidebar : Go to settings : go to environment variables . Click + Add. Name: AzureWebJobsFeatureFlags Value: EnableWorkerIndexing Click Apply.
Click Apply at the bottom of the page. Go to Overview -> Restart.
we are running on a Basic (B1) Plan, it’s not free! This keeps the server "Always On" for instant processing. Action: When you are done with this project, DELETE the App Service Plan resource group to stop the billing clock.