Examples
Below are example scripts that demonstrate how to submit a PDF generation job to Influxion, poll for its completion, and download the resulting PDF file.
All examples define configuration parameters (such as BASE_URL and polling intervals)
at the top of the script for easy customization.
Python (httpx) |
|
Bash (curl & jq) |
|
PowerShell (Invoke-RestMethod) |
Python example
#!/usr/bin/env python3
"""
Example script to generate a PDF via the Influxion API, poll until it's ready,
and download the resulting PDF file.
This script performs the following steps:
1. Get a OAuth2 token (POST /auth/token).
2. Submits a PDF generation job (POST /pdf/generate).
3. Polls the job status (GET /pdf/{job_id}) until completion.
4. Downloads the generated PDF (GET /pdf/{job_id}/download) to a local file.
"""
import os
import re
import time
import httpx
# Configuration
BASE_URL = "https://influxion.nedomkull.com"
TIMEOUT = 10.0 # seconds for HTTP requests
POLL_INTERVAL = 1.0 # seconds between status checks
CLIENT_ID = os.environ.get("INFLUXION_AZURE_CLIENT_ID", "Or set it here")
CLIENT_SECRET = os.environ.get("INFLUXION_AZURE_CLIENT_SECRET", "Or set it here")
PDF_STANDARD = "pdf/ua-1"
SIGN_KEY_ID = "B1B9EA5014C096A99CFA462A00AC24ED40507BCB"
def get_token(client: httpx.Client) -> str:
data = {
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"scope": "api://d88ae758-e67d-492d-bcc6-63f560ee1f04/.default"
}
response = client.post("/auth/token", data=data)
response.raise_for_status()
return response.json()["access_token"]
def main() -> None:
client = httpx.Client(base_url=BASE_URL, timeout=TIMEOUT)
token = get_token(client)
client.headers.update({"Authorization": f"Bearer {token}"})
# Define the payload for PDF generation
payload = {
"data": {
"message": "Hello World!",
},
"document_id": None, # None here means "generate an UUID id for me"
"pdf_standard": PDF_STANDARD,
"sign_key_id": SIGN_KEY_ID,
"html_template": {"id": "1"}, # Use the HTML template with id 1, which is the test page
"css_template": {"id": "1"}, # Use the CSS template with id 1, which accompanies the test page
}
# Submit a generate request
response = client.post("/pdf/generate", json=payload)
response.raise_for_status()
job_id = response.json().get("job_id")
if job_id is None:
raise RuntimeError(f"No job_id returned: {response.text}")
print(f"Job created with ID: {job_id}")
# Poll for job completion
while True:
response = client.get(f"/pdf/{job_id}")
response.raise_for_status()
data = response.json()
status = data.get("status")
print(f"Job status: {status}")
if status == "Done":
break
if status == "Error":
error = data.get("error")
raise RuntimeError(f"PDF generation failed: {error}")
time.sleep(POLL_INTERVAL)
# Download the resulting PDF
response = client.get(f"/pdf/{job_id}/download")
response.raise_for_status()
content_disp = response.headers.get("Content-Disposition", "")
match = re.search(r'filename="(.+)"', content_disp)
if match:
output_file = match.group(1)
else:
output_file = f"{job_id}.pdf"
with open(output_file, "wb") as f:
f.write(response.content)
print(f"PDF downloaded to {output_file}")
if __name__ == "__main__":
main()
Bash example
#!/usr/bin/env bash
# Example: Generate a PDF via the Influxion API with OAuth2, poll until done, and download it.
# Requires: curl, jq
# Prerequisites:
# export INFLUXION_AZURE_TENANT_ID=<tenant-id>
# export INFLUXION_AZURE_CLIENT_ID=<client-id>
# export INFLUXION_AZURE_CLIENT_SECRET=<client-secret>
# export BASE_URL=https://influxion.nedomkull.com # or your API URL
# Configuration
POLL_INTERVAL=1
PDF_STANDARD="pdf/ua-1"
SIGN_KEY_ID="B1B9EA5014C096A99CFA462A00AC24ED40507BCB"
# OAuth2 credentials
CLIENT_ID="$INFLUXION_AZURE_CLIENT_ID"
CLIENT_SECRET="$INFLUXION_AZURE_CLIENT_SECRET"
TENANT_ID="$INFLUXION_AZURE_TENANT_ID"
SCOPE="https://graph.microsoft.com/.default"
# Obtain access token
get_token() {
curl -sS -X POST "$BASE_URL/auth/token" \
-d "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&scope=$SCOPE" \
| jq -r .access_token
}
TOKEN=$(get_token)
AUTH_HEADER="Authorization: Bearer $TOKEN"
# Submit a generation request and return the job ID
generate_pdf() {
jq -n \
--argjson data '{"message":"Hello, world!"}' \
--arg std "$PDF_STANDARD" \
--arg sig "$SIGN_KEY_ID" \
'{data: $data, document_id: null, pdf_standard: $std, sign_key_id: $sig, html_template: {id: "1"}, css_template: {id: "1"}}' | \
curl -sS -X POST "$BASE_URL/pdf/generate" \
-H "Content-Type: application/json" \
-H "$AUTH_HEADER" \
--data @- | jq -r .job_id
}
# Poll until the job finishes or errors out
poll_until_done() {
local job_id=$1
while true; do
status=$(curl -sS -X GET "$BASE_URL/pdf/$job_id" -H "$AUTH_HEADER" | jq -r .status)
echo "Job status: $status"
if [[ "$status" == "Done" ]]; then
break
elif [[ "$status" == "Error" ]]; then
error=$(curl -sS -X GET "$BASE_URL/pdf/$job_id" -H "$AUTH_HEADER" | jq -r .error)
echo "PDF generation failed: $error" >&2
exit 1
fi
sleep $POLL_INTERVAL
done
}
# Download the resulting PDF, preserving the filename
download_pdf() {
local job_id=$1
content_disp=$(curl -sSI -X GET "$BASE_URL/pdf/$job_id/download" -H "$AUTH_HEADER" | grep -i Content-Disposition)
filename=$(echo "$content_disp" | sed -n 's/.*filename="\([^\"]*\)".*/\1/p')
[[ -z "$filename" ]] && filename="${job_id}.pdf"
curl -sS -X GET "$BASE_URL/pdf/$job_id/download" -H "$AUTH_HEADER" --output "$filename"
echo "PDF downloaded to $filename"
}
# Main workflow
job_id=$(generate_pdf)
echo "Job created with ID: $job_id"
poll_until_done "$job_id"
download_pdf "$job_id"
PowerShell example
#!/usr/bin/env pwsh
<#
Example: Generate a PDF via the Influxion API with OAuth2, poll until done, and download it.
Prerequisites:
- Set environment variables:
$env:INFLUXION_AZURE_TENANT_ID
$env:INFLUXION_AZURE_CLIENT_ID
$env:INFLUXION_AZURE_CLIENT_SECRET
- Point $BaseUrl at your Influxion API server.
#>
# Configuration
$BaseUrl = "https://influxion.nedomkull.com" # Change to your API URL
$PollInterval = 1 # Seconds between status checks
$PdfStandard = "pdf/ua-1"
$SignKeyId = "B1B9EA5014C096A99CFA462A00AC24ED40507BCB"
# OAuth2 credentials (from environment)
$TenantId = $env:INFLUXION_AZURE_TENANT_ID
$ClientId = $env:INFLUXION_AZURE_CLIENT_ID
$ClientSecret = $env:INFLUXION_AZURE_CLIENT_SECRET
$Scope = "https://graph.microsoft.com/.default"
# Obtain an access token
function Get-Token {
$body = @{ client_id = $ClientId; client_secret = $ClientSecret; scope = $Scope }
$resp = Invoke-RestMethod -Method Post -Uri "$BaseUrl/auth/token" `
-Body $body -ContentType "application/x-www-form-urlencoded"
return $resp.access_token
}
$token = Get-Token
$headers = @{ Authorization = "Bearer $token" }
# Prepare the PDF generation payload
$payload = @{ data = @{ message = "Hello, world!" }
document_id = $null
pdf_standard = $PdfStandard
sign_key_id = $SignKeyId
html_template = @{ id = "1" }
css_template = @{ id = "1" } } | ConvertTo-Json -Depth 4
# Submit the PDF generation job
$response = Invoke-RestMethod -Method Post -Uri "$BaseUrl/pdf/generate" `
-Headers $headers -Body $payload -ContentType "application/json"
$jobId = $response.job_id
if (-not $jobId) { Write-Error "No job_id returned"; exit 1 }
Write-Host "Job created with ID: $jobId"
# Poll until the job is done or errors out
do {
Start-Sleep -Seconds $PollInterval
$statusResp = Invoke-RestMethod -Method Get -Uri "$BaseUrl/pdf/$jobId" -Headers $headers
$status = $statusResp.status
Write-Host "Job status: $status"
if ($status -eq 'Error') {
Write-Error "PDF generation failed: $($statusResp.error)"
exit 1
}
} until ($status -eq 'Done')
# Download the resulting PDF
$head = Invoke-WebRequest -Method Head -Uri "$BaseUrl/pdf/$jobId/download" -Headers $headers -UseBasicParsing
$contentDisp = $head.Headers['Content-Disposition']
if ($contentDisp -match 'filename="(.+)"') { $filename = $matches[1] } else { $filename = "$jobId.pdf" }
Invoke-RestMethod -Method Get -Uri "$BaseUrl/pdf/$jobId/download" `
-Headers $headers -OutFile $filename
Write-Host "PDF downloaded to $filename"