How it works
Every edit is one v2 document operation with toolId "pdf-edit" and one editor command in parameters.edit. Edits never turn a page into an image or draw a cover over it: the PDF's own objects are rewritten and the result is checked before it becomes the document's next version.
- Upload the PDF with POST
/api/v2/documents(scope documents.write). - Run an INSPECT operation with POST
/api/v2/document-operations(scope documents.process). When it completes, GET/api/v2/document-operations/{operationId}returns the report in operation.result.metadata.inspection. - Send an edit command with a selector copied unchanged from the inspection. A selector names the exact object and its expected content, so an edit against a document that has changed since is refused instead of landing in the wrong place.
- Poll the operation, then download
operation.outputVersionIdwith GET/api/v2/documents/{documentId}/versions/{versionId}/content. Send further edits against the new version ID. - Each operation costs one request unit and takes an idempotency key; retrying with the same key returns the original operation.
Text keeps its font and size
REPLACE_TEXTwrites the new text with the run's own font, size, colour, spacing and position. Nothing is drawn over the page.- PDFs usually embed only the characters they use. When the embedded font cannot draw a new character, the full font of the same family is embedded for that run, at the same size: the exact font when it is available, or the metric-compatible Liberation font for the Arial, Helvetica, Times and Courier families.
- If no font of the same family can draw the text, the edit is refused with
PDF_FONT_GLYPH_UNAVAILABLEinstead of switching to an unrelated typeface. To choose a font yourself, sendtext_style, for example {"font": "LiberationSans-Bold", "fontSize": 12, "color": "#1a1a1a"}. - For longer text,
REFLOW_TEXTwraps it inside a region you give, using the font's own metrics, and is refused if the text would overflow or overlap other content. - Scanned pages have no text objects. Run OCR first, or use the image commands.
Editor commands
| parameters.edit.operation | Fields | Result |
|---|---|---|
| INSPECT | text_page (optional page number) | Editable text runs (with font, size, matrix and bounding box), images and placements, vector paths, links, annotations, metadata, bookmarks, form fields and page geometry, each with the selector to send back. |
REPLACE_TEXT | selector, replacement_text; optional text_style {font, fontSize, color} | Rewrites the run in place in its own font and size. If the embedded font lacks a character, the full font of the same family is embedded at the same size; without one the edit is refused with PDF_FONT_GLYPH_UNAVAILABLE. |
REFLOW_TEXT | selector, replacement_text, region {left, bottom, right, top}, alignment LEFT|CENTER|RIGHT, line_spacing | Wraps longer text inside one region with the run's font; overflow and overlaps are refused. |
REPLACE_IMAGE | selector, replacement_image (base64 JPEG) | Replaces one image's pixels; its placement stays. |
MOVE_IMAGE / TRANSFORM_IMAGE | selector plus destination_x, destination_y or destination_matrix {a,b,c,d,e,f} | Moves, scales, rotates or skews one placement. |
CROP_IMAGE / SET_IMAGE_OPACITY | selector plus destination_crop {left,bottom,right,top} (0..1) or destination_opacity (0..1) | Crops or fades one placement. |
EDIT_VECTOR_PATH | selector, mutation {kind: TRANSFORM|RECOLOR|DELETE, ...} | Moves, resizes, recolours or deletes one recognised vector path. |
EDIT_LINK / EDIT_ANNOTATION / EDIT_BOOKMARK | mutation {kind: CREATE|UPDATE|DELETE (and MOVE for bookmarks), ...} | Link annotations; text, highlight, underline, strikeout, ink, rectangle, arrow and stamp annotations; bookmarks. |
EDIT_METADATA | selector, values, xmp_mode | Document information and XMP for the allowed fields. |
EDIT_FORM_FIELD_VALUE / _GEOMETRY / _LABEL / _OPTIONS | selector plus value, widget and rectangle, label, or options and value | Fills or reshapes one form field. |
CREATE_FORM_FIELD / DELETE_FORM_FIELD / REPAIR_FORM_FIELD_APPEARANCE | selector (from formCreation or editableFormFields) and the field definition | Text, checkbox, radio, dropdown and signature fields. |
REORDER_PAGES / EDIT_PAGE_GEOMETRY / MUTATE_PAGES | page_order; selector, media_box, crop_box, rotation; mutation {kind: DELETE|DUPLICATE|INSERT_BLANK|IMPORT, ...} | IMPORT takes mutation.import_pdf (base64 PDF) and source_pages. |
Errors
- 400
OPERATION_INPUT_INVALID: parameters.edit is missing, names an unknown command, or sets a file path (input and output files are supplied for you). - A refused edit completes the operation as failed with the editor's errorCode and errorMessage, for example
PDF_TEXT_RUN_STALE(the selector no longer matches; inspect the current version again),PDF_FONT_GLYPH_UNAVAILABLE,REFLOW_UNSAFEorPDF_SIGNATURE_CONFIRMATION_REQUIRED. - Signed PDFs: an edit invalidates existing signatures, so it is refused unless the command sets
allow_signature_invalidation: true. EDIT_INSPECTION_TOO_LARGE: inspect one page at a time withtext_page.- A request body can be up to 12 MB, including base64 file content (
replacement_image,mutation.import_pdf).
Inspect, replace text and download (cURL and jq)
#!/usr/bin/env bash
set -euo pipefail
: "${WELLFRIEND_API_KEY:?Set WELLFRIEND_API_KEY to a wf_v2_test or wf_v2_live key}"
API="https://wellfriend.dev/api/v2"
AUTH="Authorization: Bearer ${WELLFRIEND_API_KEY}"
RUN_ID="$(date +%s)"
# Runs one document operation and prints it once it is final.
run_operation() {
local id state operation deadline
id=$(curl --fail-with-body --max-time 60 -X POST "$API/document-operations" \
-H "$AUTH" -H "Content-Type: application/json" --data "$1" | jq -r '.operation.id')
deadline=$(( $(date +%s) + 600 ))
while :; do
operation=$(curl --fail-with-body --max-time 30 "$API/document-operations/$id" -H "$AUTH")
state=$(jq -r '.operation.state' <<<"$operation")
case "$state" in
queued|running|cancelling) ;;
completed) printf '%s' "$operation"; return 0 ;;
*) jq -c '.operation | {state, errorCode, errorMessage}' <<<"$operation" >&2; return 1 ;;
esac
[ "$(date +%s)" -lt "$deadline" ] || { echo "Timed out waiting for $id" >&2; return 1; }
sleep 2
done
}
# 1. Ingest the PDF and keep its immutable version ID.
DOCUMENT=$(curl --fail-with-body --max-time 120 -X POST "$API/documents" \
-H "$AUTH" -H "Content-Type: application/pdf" \
-H "X-Wellfriend-Filename: contract.pdf" -H "Idempotency-Key: ingest-$RUN_ID" \
--data-binary @./contract.pdf)
DOCUMENT_ID=$(jq -r '.document.id' <<<"$DOCUMENT")
VERSION_ID=$(jq -r '.document.currentVersion.id' <<<"$DOCUMENT")
# 2. Inspect page 1: every editable text run with its exact selector, font and size.
INSPECTION=$(run_operation "$(jq -n --arg d "$DOCUMENT_ID" --arg v "$VERSION_ID" --arg k "inspect-$RUN_ID" \
'{documentId:$d, versionId:$v, toolId:"pdf-edit", idempotencyKey:$k,
parameters:{edit:{operation:"INSPECT", text_page:1}}}')")
SELECTOR=$(jq -c '.operation.result.metadata.inspection.editableTextRuns[]
| select(.selector.expectedText | contains("Acme Corporation")) | .selector' <<<"$INSPECTION" | head -1)
# 3. Replace that run. The new text keeps the run's font, size, colour and position.
EDITED=$(run_operation "$(jq -n --arg d "$DOCUMENT_ID" --arg v "$VERSION_ID" --arg k "edit-$RUN_ID" \
--argjson s "$SELECTOR" \
'{documentId:$d, versionId:$v, toolId:"pdf-edit", idempotencyKey:$k,
parameters:{edit:{operation:"REPLACE_TEXT", selector:$s,
replacement_text:($s.expectedText | sub("Acme Corporation"; "Wellfriend Group"))}}}')")
# 4. Download the edited PDF: the document's next immutable version.
OUTPUT_VERSION_ID=$(jq -r '.operation.outputVersionId' <<<"$EDITED")
curl --fail-with-body --max-time 120 "$API/documents/$DOCUMENT_ID/versions/$OUTPUT_VERSION_ID/content" \
-H "$AUTH" -o ./contract-edited.pdfCommand examples
{
"replaceText": {
"edit": {
"operation": "REPLACE_TEXT",
"selector": "<editableTextRuns[i].selector>",
"replacement_text": "Wellfriend Group"
}
},
"reflowText": {
"edit": {
"operation": "REFLOW_TEXT",
"selector": "<editableTextRuns[i].selector>",
"replacement_text": "A longer paragraph that wraps inside the region.",
"region": {
"left": 72,
"bottom": 600,
"right": 520,
"top": 680
},
"alignment": "LEFT",
"line_spacing": 1.2
}
},
"replaceImage": {
"edit": {
"operation": "REPLACE_IMAGE",
"selector": "<editableImages[i].selector>",
"replacement_image": "<base64 JPEG>"
}
},
"moveImage": {
"edit": {
"operation": "MOVE_IMAGE",
"selector": "<editableImagePlacements[i].selector>",
"destination_x": 72,
"destination_y": 500
}
},
"fillField": {
"edit": {
"operation": "EDIT_FORM_FIELD_VALUE",
"selector": "<editableFormFields[i].selector>",
"value": "Jane Doe"
}
},
"deletePage": {
"edit": {
"operation": "MUTATE_PAGES",
"mutation": {
"kind": "DELETE",
"selector": "<editablePages[i].selector>"
}
}
}
}