Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Azure Function PDF Splitter

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.

Step 1: Azure Portal Setup

1. Create Storage Account (The Hard Drive)

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.

2. Create Containers (The Folders)

Go to your new Storage Account resource. Sidebar: Data storage -> Containers. Click + Container twice to create these two "EXACT“ folders:

  • input-pdfs
  • output-chunks

Get Connection String: Sidebar: Security + networking -> Access keys. Show and Copy the Connection string for key1.

3. Create Function App

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.

Step 2: Local Project Setup (VS Code)

1. Folder Structure

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

2. File Contents (Copy-Paste )

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"
  }
}

3. The Code (function_app.py)

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}")

Step 3: Deployment

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.

Step 4: Verification

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.

Step 5 (optional) : Fixes if still not working !

Go to the Azure Portal -> Function App -> Settings -> Environment variables.

1. Verify Storage Connection

Ensure AzureWebJobsStorage exists and contains the long connection string starting with DefaultEndpointsProtocol=https....

2. Enable V2 Python Model (Fixes "Missing Function" / 404)

In Sidebar : Go to settings : go to environment variables . Click + Add. Name: AzureWebJobsFeatureFlags Value: EnableWorkerIndexing Click Apply.

3. Save & Restart

Click Apply at the bottom of the page. Go to Overview -> Restart.

Final Note :

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.

About

An event-driven Azure Function designed to optimize heavy OCR workloads. By automatically chunking 500+ page PDFs upon upload, this serverless pipeline bypasses the 20-minute bottleneck of sequential Marker OCR processing. It enables simultaneous, parallel batch processing to deliver text extraction in seconds.

Resources

Stars

Watchers

Forks

Contributors