REST: PDF documents
Overview
A form's PDF documents are managed with commands on the form, outside its draft: each command changes the live form at once. A document prints for one of two usages:
| Usage | What it is |
|---|---|
admin_print | The copy your team prints from the admin |
customer_print | The copy the submitter downloads |
A binding connects a document to a usage. A document prints when it is switched on and its binding for that usage is active. A form created from a template starts with two documents, Customer Copy (bound to customer_print) and Office Copy (bound to admin_print), both switched on. In the admin the same settings are described in PDF documents.
Documents and bindings are addressed by uuid. Both appear in the form's document (GET /V1/webforms/forms/{formId}/document, members pdf_documents and pdf_bindings), but only once a form's documents differ from the presets they were made from: until then both lists are empty, even after install-preset. The uuids that install-preset and add return can be used at once; once one document has been changed, the form document lists all of them, the two starting documents included.
Commands
POST /V1/webforms/forms/{formId}/pdf-documents/commands/{name}. Every command takes request_id. pdf_document_json is a JSON string. The answer has form_id, changed and payload_json.
| Command | Request keys | Payload |
|---|---|---|
install-preset | code: customer_copy, office_copy or standard_set (both) | added: per document uuid, document_id, name, usage_type, is_default, removed |
add | pdf_document_json: a document | uuid, document_id, removed |
update | pdf_document_ref, pdf_document_json: the keys to change | uuid, document_id, revision_id, removed |
set-content | pdf_document_ref, pdf_document_json: the slots for one store view, or null to remove that store view's own text; optional store_code. Without it the default text is written (store_id 0), which store views without their own text use; their own texts stay | as update |
duplicate | pdf_document_ref | uuid, document_id, removed of the copy, named "… Copy" and switched off |
set-bindings | pdf_document_json: the complete list of the form's bindings | bindings as saved, uuid_map |
activate, deactivate | pdf_document_ref | uuid, is_active |
remove | pdf_document_ref | removed. A document that is switched on and bound is refused: switch it off first |
A document added with add, duplicate or install-preset starts switched off. install-preset also adds a binding for each new document; that binding is active and the usage's default only when the usage has no default yet. A preset name already in use gets a number ("Customer Copy 2").
removed lists markup the sanitiser took out of the text, for example <p onclick>. Tags such as <script>, <style>, <iframe> or <form> are refused instead.
The document
| Key | Meaning |
|---|---|
name | Required, at most 255 characters, unique within the form (ignoring case) |
template_ref, template_contract | The PDF template and its version; standard with contract 1 is the built-in one |
contents | Per store view: store_id (0 for all store views) and slots. The standard template has the slots intro, outro and footer_note (HTML); the html template has body, footer and css. See PDF templates |
page_policy | Page layout: format, orientation, margins, and the header and footer switches such as brand_bar, page_numbers, footer_reference |
filename_pattern | File name of the PDF; empty for the default |
content_mode | managed, the only mode |
sort_order, uuid | Position and uuid |
is_active is read-only and changes only through activate and deactivate; sending it in update is refused with 403. In the form document each document carries its text as slots (all store views) and store_slots (keyed by store code) instead of contents.
Bindings
| Key | Meaning |
|---|---|
uuid | The binding's uuid; leave it out for a new binding |
document_uuid | The document |
usage_type | admin_print or customer_print |
is_active | Whether the binding is used |
is_default | The document that prints when no conditional one matches |
sort_order | Order within the usage |
condition_aggregation, conditions | When it prints: all or any, and conditions with field_code, operator and values (at most 20) |
set-bindings replaces all bindings of the form and checks them together:
- A
customer_printbinding cannot have conditions: the download would reveal a hidden answer. - The default binding cannot have conditions.
- A document can be bound to each usage once.
- Per usage, conditional bindings are kept ahead of the others, and the saved
sort_orderis renumbered 10, 20 … in that order.
activate is refused when the form would then print two documents for a usage without a condition.
Authentication
OAuth 1.0a integration credentials, as described on REST API. The commands route needs forms_edit (Edit and Publish Forms). WebForms checks every PDF document command again on its side, and an integration holding forms_edit passes that check: no separate PDF permission exists. The editor and full presets include forms_edit. An integration without it is refused by Magento with 401 (The consumer isn't authorized to access …), before WebForms sees the request.
Operations
| Method | Path | Operation | ACL resource |
|---|---|---|---|
| POST | /V1/webforms/forms/{formId}/pdf-documents/commands/{name} | install-preset with an unknown code answers 404 NOT_FOUND, as does any command on a form that does not exist. | MageMe_WebForms::api_v1_forms_edit |
Errors
| Code | HTTP | When |
|---|---|---|
VALIDATION_FAILED | 400 | The document or the bindings are not valid (The PDF document is not valid: …, The PDF bindings are not valid: …); a name the form already uses; code or pdf_document_ref missing; an unknown store_code in set-content; activate that would print two unconditional documents; remove on a document that is switched on and bound |
ACTOR_NOT_ALLOWED | 403 | is_active sent in update. Without forms_edit Magento refuses the route with 401 |
NOT_FOUND | 404 | No such form or document uuid on this form; a form outside the integration's form scope; an unknown command name |
LICENSE_NOT_COVERED | 403 | The store's WebForms license does not cover this version; every command is refused |
A <script> tag in a text is refused by Magento before WebForms sees it, in Magento's own shape (400, "%field_name" does not contains valid value.). The refusals every route shares are listed on REST API.
Examples
One sequence on form 995589. Add the standard set (a Customer Copy and an Office Copy) next to the two documents the form already has:
curl -X POST https://yourdomain.com/rest/all/V1/webforms/forms/995589/pdf-documents/commands/install-preset \
-H "Authorization: <signed-header>" -H "Content-Type: application/json" \
-d '{"request": {"request_id": "erp-pdf-0001", "code": "standard_set"}}'
{"form_id": 995589, "changed": true, "payload_json": "{\"added\":[{\"uuid\":\"1e6b8ecb-c866-42ff-a4f8-73acc4f90bfb\",\"document_id\":1395,\"removed\":[],\"name\":\"Customer Copy 2\",\"usage_type\":\"customer_print\",\"is_default\":false},{\"uuid\":\"4d3b717f-9fe7-4b5d-a50c-dc9fe95e5ce3\",\"document_id\":1396,\"removed\":[],\"name\":\"Office Copy 2\",\"usage_type\":\"admin_print\",\"is_default\":false}]}"}
Rename the new Office Copy (update):
{"request": {"request_id": "erp-pdf-0002", "pdf_document_ref": "4d3b717f-9fe7-4b5d-a50c-dc9fe95e5ce3", "pdf_document_json": "{\"name\":\"Showroom Visit Sheet\"}"}}
{"form_id": 995589, "changed": true, "payload_json": "{\"uuid\":\"4d3b717f-9fe7-4b5d-a50c-dc9fe95e5ce3\",\"document_id\":1396,\"revision_id\":1492,\"removed\":[]}"}
Set its default text (set-content without store_code). The onclick attribute is taken out and reported:
{"request": {"request_id": "erp-pdf-0003", "pdf_document_ref": "4d3b717f-9fe7-4b5d-a50c-dc9fe95e5ce3", "pdf_document_json": "{\"intro\":\"<p onclick=\\\"go()\\\">Showroom visit request. Bring this sheet to the visit.</p>\",\"outro\":\"<p>Northstar Supply showroom team</p>\"}"}}
{"form_id": 995589, "changed": true, "payload_json": "{\"uuid\":\"4d3b717f-9fe7-4b5d-a50c-dc9fe95e5ce3\",\"document_id\":1396,\"revision_id\":1495,\"removed\":[\"<p onclick>\"]}"}
Print the sheet for the team only for product questions: bind it to admin_print with a condition and keep the other bindings as they are (set-bindings, the whole list):
{
"request": {
"request_id": "erp-pdf-0004",
"pdf_document_json": "[{\"uuid\":\"6f6fe465-2bc5-44fe-9b06-58c1f728277d\",\"document_uuid\":\"9935a259-7519-48b7-897b-2715dfcd62d8\",\"usage_type\":\"customer_print\",\"is_active\":true,\"is_default\":true,\"sort_order\":10,\"condition_aggregation\":\"all\",\"conditions\":[]},{\"uuid\":\"dd12abf9-7151-4ed0-9538-7173ffb6c606\",\"document_uuid\":\"27c0572c-0364-42bc-95c0-6aebf05f30fe\",\"usage_type\":\"admin_print\",\"is_active\":true,\"is_default\":true,\"sort_order\":10,\"condition_aggregation\":\"all\",\"conditions\":[]},{\"uuid\":\"76cfdcc4-d1bc-4633-959b-ef5c33662fbb\",\"document_uuid\":\"1e6b8ecb-c866-42ff-a4f8-73acc4f90bfb\",\"usage_type\":\"customer_print\",\"is_active\":false,\"is_default\":false,\"sort_order\":20,\"condition_aggregation\":\"all\",\"conditions\":[]},{\"uuid\":\"6fb69b48-bdb3-4802-9362-026db3277725\",\"document_uuid\":\"4d3b717f-9fe7-4b5d-a50c-dc9fe95e5ce3\",\"usage_type\":\"admin_print\",\"is_active\":true,\"is_default\":false,\"sort_order\":20,\"condition_aggregation\":\"all\",\"conditions\":[{\"field_code\":\"subject\",\"operator\":\"is\",\"values\":[\"product\"]}]}]"
}
}
In the saved bindings of the answer the conditional binding of the sheet comes first in admin_print, with sort_order 10, and the Office Copy follows with 20.
Switch the sheet on:
{"request": {"request_id": "erp-pdf-0005", "pdf_document_ref": "4d3b717f-9fe7-4b5d-a50c-dc9fe95e5ce3"}}
{"form_id": 995589, "changed": true, "payload_json": "{\"uuid\":\"4d3b717f-9fe7-4b5d-a50c-dc9fe95e5ce3\",\"is_active\":true}"}
A condition on a customer_print binding:
{"message": "The PDF bindings are not valid: pdf_bindings[2].conditions: a customer download document cannot be conditional (its presence would reveal a hidden answer).", "parameters": {"code": "VALIDATION_FAILED", "details_schema_version": "1", "details_json": "{}"}}
A later set-bindings, the same list with the binding of Customer Copy 2 (76cfdcc4-…) set to "is_active": true, is accepted. Switching Customer Copy 2 on is then refused, because Customer Copy already prints for customer_print without a condition:
{"message": "\"Customer Copy\" already prints when no condition matches. Switch it off first, or give this document a condition.", "parameters": {"code": "VALIDATION_FAILED", "details_schema_version": "1", "details_json": "{}"}}
A customer_print binding cannot take a condition, so here the way out is to switch Customer Copy off first, or to set the binding of Customer Copy 2 inactive again.