IBM watsonx Orchestrate

IBM watsonx Orchestrate

Connect with experts and peers to elevate technical expertise, solve problems and share insights.

 View Only

Streaming Dynamic Files & PDF Generation in watsonx Orchestrate: Delivering On-Demand Reports (Part 6 of 15)

By Markus van Kempen posted 10/08/26 09:50 AM

  

Why This Pattern Matters

Executives often require formal PDF briefing packets with corporate headers and timestamps. Rather than sending static templates, our agent synthesizes live conversational data and renders a downloadable binary PDF on demand.


📋 Prerequisites & Setup

Ensure your environment has the required dependencies:

pip install ibm-watsonx-orchestrate

Verify active environment:

orchestrate env list

🛠️ Step 1: Implement the Logic (pdf_generator.py)

from ibm_watsonx_orchestrate import tool
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
import tempfile
import os
from datetime import datetime

@tool
def generate_report_pdf(report_title: str, report_body: str) -> str:
    """
    Generate a formatted binary PDF document and return a download URI.
    """
    temp_dir = tempfile.gettempdir()
    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
    filename = f"WXO_Report_{timestamp}.pdf"
    filepath = os.path.join(temp_dir, filename)

    c = canvas.Canvas(filepath, pagesize=letter)
    width, height = letter

    # Header branding
    c.setFont("Helvetica-Bold", 18)
    c.setFillColorRGB(0.06, 0.38, 0.99) # IBM Blue
    c.drawString(50, height - 50, "IBM watsonx Orchestrate")

    c.setFont("Helvetica-Bold", 14)
    c.setFillColorRGB(0.1, 0.1, 0.1)
    c.drawString(50, height - 85, report_title)

    c.setFont("Helvetica", 9)
    c.setFillColorRGB(0.4, 0.4, 0.4)
    c.drawString(50, height - 105, f"Generated: {datetime.now().strftime('%B %d, %Y at %H:%M:%S UTC')}")

    # Body
    c.setFont("Helvetica", 11)
    c.setFillColorRGB(0.1, 0.1, 0.1)
    text_object = c.beginText(50, height - 140)
    for line in report_body.split("\n"):
        text_object.textLine(line)
    c.drawText(text_object)

    c.save()
    filesize = os.path.getsize(filepath)

    return f"""
📄 PDF Document Generated Successfully
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Title:    {report_title}
File:     {filename} ({filesize:,} bytes)
Location: {filepath}
Status:   Ready for download
"""

📦 Step 2: Declare Dependencies (requirements.txt)

ibm-watsonx-orchestrate>=2.9.0
reportlab>=4.0.0

🤖 Step 3: Configure the AI Agent (agent.yaml)

spec_version: v1
kind: native
name: report_publisher_agent
title: "Report Publisher"
description: "Generates formatted PDF executive briefings on demand."
model: ibm/granite-3-8b-instruct
instructions: |
  You are an executive publishing assistant. Format summaries into structured
  briefings and call `generate_report_pdf` to create the document.
tools:
  - generate_report_pdf

🚀 Step 4: Import & Test Live in Terminal

Import tool and test conversationally:

orchestrate chat ask -n report_publisher_agent "Generate a PDF report on Q3 AI Adoption"

Live Response:

╭─ 🤖 report_publisher_agent ───────────────────────────────────────────╮
│                                                                        │
│  📄 PDF Document Generated Successfully                                │
│  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━                                  │
│  Title:    Q3 AI Adoption Briefing                                     │
│  File:     WXO_Report_20260924_201500.pdf (4,812 bytes)                │
│  Status:   Ready for download                                          │
╰────────────────────────────────────────────────────────────────────────╯

💡 Pro Tips & Gotchas

  • Cloud Ephemeral Storage: On local dev or container sandboxes, `/tmp` works directly. For production cloud deployments, stream the generated bytes to IBM Cloud Object Storage (COS) or AWS S3 and return a presigned HTTPS URL.
  • ReportLab Fonts: Stick to built-in PostScript fonts (`Helvetica`, `Times-Roman`, `Courier`) to avoid bundle size bloat and missing TTF font errors.

🏷️ Recommended Community Tags

When publishing this blog post, select or enter the following tags in the community editor:

watsonx Orchestrate, PDF Generation, File Streaming, ReportLab, AI Documents, Python


🗺️ Series Roadmap (15 Hands-On Labs)

This tutorial is Part 6 of 15 in the complete enterprise hands-on series:

  • 🟢 Part 1: Building Your First Custom Python Tool & AI Agent in 15 Minutes
  • 🟢 Part 2: Input Defaults & UI Patterns (Pydantic Schemas)
  • 🟢 Part 3: File Upload & Spreadsheet Analytics (Pandas Pipeline)
  • 🟡 Part 4: Ambient User Context & Identity Injection
  • 🟡 Part 5: Asynchronous Non-Blocking Workers & Polling
  • 🟡 Part 6: Dynamic PDF Generation & File Streaming
  • 🟡 Part 7: Model Context Protocol (MCP) STDIO Basics
  • 🔴 Part 8: MCP Enterprise Security & Bearer Token Injection
  • 🔴 Part 9: Role-Based Access Control (RBAC) Pre-Invoke Plugins
  • 🔴 Part 10: Microsoft Entra ID (Azure AD) SSO & SAML Federation
  • 🔴 Part 11: Agent CI/CD Lifecycle & Automated Multi-Env Promotion
  • 🏆 Part 12: Tamper-Proof Audit Vault & Conversation Archiving
  • 🏆 Part 13: Real-Time Telemetry & Token Observability Dashboard
  • 🏆 Part 14: Quantitative RAG Pipeline Evaluation with Ragas
  • 🔴 Part 15: Remote Containerized MCP Gateways on IBM Cloud Code Engine

Explore the interactive curriculum and code repository:

0 comments
8 views

Permalink