#!/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"