#!/usr/bin/env bash # PDFHQ end-to-end task example: curl + jq, Bash 4+, server-side only. # Requires PDFHQ_API_KEY, PDFHQ_FILE. PDFHQ_APPROVE_QUOTE=yes authorizes processing. # Optional PDFHQ_TOOL (default compress) and PDFHQ_OPTIONS_JSON (default {}). # No automatic retry of uncertain billable submissions. No redirect following. set -Eeuo pipefail : "${PDFHQ_API_KEY:?Set a server-side PDFHQ_API_KEY}" : "${PDFHQ_FILE:?Set PDFHQ_FILE=/path/to/report.pdf}" [[ -f "$PDFHQ_FILE" && -s "$PDFHQ_FILE" ]] || { echo 'PDF not found or empty' >&2; exit 2; } for binary in curl jq stat; do command -v "$binary" >/dev/null || { echo "Missing $binary" >&2; exit 2; }; done PDFHQ_API_BASE="${PDFHQ_API_BASE:-https://api.pdfhq.io/v1}" [[ "$PDFHQ_API_BASE" == https://*/v1 ]] || { echo 'Expected HTTPS PDFHQ v1 API URL' >&2; exit 2; } PDFHQ_TOOL="${PDFHQ_TOOL:-compress}" if [[ -n "${PDFHQ_TASK_ID:-}" && ! "$PDFHQ_TASK_ID" =~ ^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$ ]]; then echo 'PDFHQ_TASK_ID must be a UUID' >&2; exit 2 fi PDFHQ_OPTIONS_JSON="${PDFHQ_OPTIONS_JSON:-{}}" jq -e 'type=="object"' <<<"$PDFHQ_OPTIONS_JSON" >/dev/null || { echo 'Options must be JSON object' >&2; exit 2; } # Curl -q ignores .curlrc; --proto HTTPS and --max-redirs 0 prevent credential leakage. api() { curl -q --fail-with-body --silent --show-error --proto '=https' --max-redirs 0 \ --connect-timeout 10 --max-time 90 -H "x-api-key: $PDFHQ_API_KEY" "$@" } if [[ -n "${PDFHQ_TASK_ID:-}" ]]; then task_id="$PDFHQ_TASK_ID" echo "Reusing task: $task_id" >&2 else task_id="$(api -X POST "$PDFHQ_API_BASE/tasks" -H 'Content-Type: application/json' \ --data "$(jq -nc --arg tool "$PDFHQ_TOOL" '{tool:$tool}')" | jq -er '.task_id')" echo "TASK_ID=$task_id (save this ID)" >&2 # Create signed storage upload policy; API never carries the file contents. if stat -c '%s' "$PDFHQ_FILE" >/dev/null 2>&1; then size="$(stat -c '%s' "$PDFHQ_FILE")" # Linux else size="$(stat -f '%z' "$PDFHQ_FILE")" # macOS fi if command -v sha256sum >/dev/null; then sha256="$(sha256sum "$PDFHQ_FILE" | cut -d' ' -f1)" else command -v shasum >/dev/null || { echo 'Missing SHA-256 hashing utility' >&2; exit 2; } sha256="$(shasum -a 256 "$PDFHQ_FILE" | cut -d' ' -f1)" fi ticket="$(api -X POST "$PDFHQ_API_BASE/tasks/$task_id/uploads" -H 'Content-Type: application/json' \ --data "$(jq -nc --arg filename "$(basename "$PDFHQ_FILE")" --argjson size_bytes "$size" \ --arg sha256 "$sha256" '{filename:$filename, content_type:"application/pdf",size_bytes:$size_bytes,sha256:$sha256}')")" upload_id="$(jq -er '.upload_id' <<<"$ticket")" target="$(jq -er '.url' <<<"$ticket")" expected="/v1/tasks/$task_id/uploads/$upload_id/complete" [[ "$(jq -er '.method' <<<"$ticket")" == 'POST' ]] || { echo 'Unexpected storage method' >&2; exit 2; } [[ "$(jq -er '.next' <<<"$ticket")" == "$expected" ]] || { echo 'Untrusted completion URL' >&2; exit 2; } [[ "$target" == https://* ]] || { echo 'Storage must be HTTPS' >&2; exit 2; } # Construct literal -F fields; do not transmit PDFHQ_API_KEY to storage. fields=() while IFS= read -r field; do key="$(jq -er '.key' <<<"$field")" value="$(jq -er '.value' <<<"$field")" [[ "$key" =~ ^[a-zA-Z0-9_-]+$ ]] || { echo 'Invalid field name' >&2; exit 2; } fields+=(--form-string "$key=$value") done < <(jq -cr '.fields | to_entries[]' <<<"$ticket") curl -q --fail-with-body --silent --show-error --proto '=https' --max-redirs 0 \ --connect-timeout 10 --max-time 300 -X POST "${fields[@]}" -F "file=@$PDFHQ_FILE;type=application/pdf" "$target" >/dev/null api -X POST "$PDFHQ_API_BASE$expected" >/dev/null fi quote="$(api "$PDFHQ_API_BASE/tasks/$task_id/quote")" echo "$quote" | jq '{operation,estimated_units,estimated_hq_credits,estimate_only}' >&2 jq -e '.estimate_only == true and .price_source == "hqtools" and (.estimated_hq_credits | (type == "string") and (test("^[0-9]+([.][0-9]+)?$")))' <<<"$quote" >/dev/null || { echo 'Quote must contain a valid HQTools credit estimate; refusing processing' >&2; exit 2; } if [[ "${PDFHQ_APPROVE_QUOTE:-no}" != 'yes' ]]; then echo "No charge authorized; resume with PDFHQ_TASK_ID=$task_id PDFHQ_APPROVE_QUOTE=yes" >&2 exit 0 fi api -X POST "$PDFHQ_API_BASE/tasks/$task_id/process" -H 'Content-Type: application/json' \ --data "$(jq -nc --argjson options "$PDFHQ_OPTIONS_JSON" '{options:$options}')" >/dev/null for ((i=0; i<300; i++)); do status="$(api "$PDFHQ_API_BASE/tasks/$task_id" | jq -er '.status')" case "$status" in succeeded) echo 'Success. To retrieve a short-lived output link, call GET /v1/tasks/TASK_ID/download-url.'; exit 0;; failed|cancelled|expired|deleted) echo "Task ended: $status" >&2; exit 1;; esac sleep 2 done echo "Polling timed out for $task_id; do not resubmit, inspect existing task." >&2 exit 2