Skip to content

Generate and download a PDF document

Generate a PDF from a saved record with an explicit layout button and a synchronous webhook.

Complete example: acme_record_pdf

Purpose (translated from upstream README)

Records. A layout button invokes a synchronous workflow to offer a saved record as PDF. A single Twig view supplies header/content/footer through view. The business test checks download_offered; the PHPUnit test checks rendered blocks with a fake PDF transport.
Adapt (translated from upstream README)
Rename all acme_* module, workflow, event and resource identifiers together; update references, layout paths, filters, translation keys and tests. Add fields to the schema, layouts and both languages. Copy files relative to the system root. Add native modules from fixtures/ only when missing; merge existing definitions. Menu entries and preview data are optional. Check variants separately instead of applying them alongside the main definition. Check with php bakery plan <system>, php bakery apply <system>, then php bakery system test-run <system>. In the API repository, AcmeFeatureExamplesTest runs the same plan/apply steps and workflow tests in a disposable PHPUnit system database. These checks do not verify UI rendering or client-only actions. views/record.twig contains all three blocks; views/record.view.bake.json registers it. action/export ignores content, header and footer options: use view. Adapt the name, filename and template together. The button requires a saved record. action/downloadFile is used because download_offered checks Content-Disposition. Production PDF conversion requires the configured Brezel export infrastructure (brezel.export.url). CI calls no service: AcmeFeatureExamplesTest fakes only the HTTP transport, checks the rendered header/content/footer blocks and returns fixtures/rendered.pdf. The normal business test runner supplies an empty generic response for Laravel HTTP; its http_fakes do not reach this export transport. The workflow/download test alone therefore proves neither PDF validity nor wkhtmltopdf rendering. This existing test gap is documented in the audit.

Files relative to the system root:

acme_record_pdf
├── README.md
├── fixtures/
│ ├── native.module.bake.json
│ └── rendered.pdf
├── menu.bake.json
├── modules/
│ └── acme_records/
│ ├── acme_records.detail.json
│ ├── acme_records.index.json
│ └── acme_records.module.bake.json
├── preview/
│ └── sample.bake.json
├── tests/
│ └── acme_record_download.test.workflow.json
├── translations/
│ ├── de.json
│ ├── en.json
│ └── translations.bake.json
├── views/
│ ├── record.twig
│ └── record.view.bake.json
└── workflows/
└── acme_record_pdf.workflow.bake.json
README.md
README.md
# acme_record_pdf
Dokumente / Records. Ein gespeicherter Datensatz wird per Layout-Knopf und synchronem Workflow als PDF angeboten. Eine einzige Twig-View liefert header/content/footer über view. Der Business-Test prüft download_offered; der PHPUnit-Test prüft die gerenderten Blöcke mit gefälschtem PDF-Transport.
## Dateien
- `modules/`: Schema sowie Detail- und Index-Layouts.
- `menu.bake.json`: Navigation (in ein vorhandenes Menü übernehmen).
- `translations/`: deutsche/englische Labels und Bakery-Ressource.
- `workflows/`, `tests/`: ausführbare Graphen und isolierte Business-Tests mit Fixtures.
- `preview/`: optionale Bakery-Beispieldaten; nur in einer Vorschau anwenden.
## Anpassen
Alle `acme_*`-Modul-, Workflow-, Event- und Ressourcen-Identifier gemeinsam umbenennen; Referenzen, Layoutpfade, Filter, Übersetzungsschlüssel und Tests mitändern. Weitere Felder in Schema, Layout und beiden Sprachen ergänzen. Dateien relativ zur Systemwurzel kopieren. Native Module aus `fixtures/` nur ergänzen, wenn sie fehlen; bestehende Definitionen zusammenführen. Menüeinträge und Vorschaudaten sind optional. Varianten separat prüfen, nicht zusätzlich zur Hauptdefinition anwenden.
Prüfen: `php bakery plan <system>`, `php bakery apply <system>`, dann `php bakery system test-run <system>`. In diesem Repository führt `AcmeFeatureExamplesTest` dieselben Plan-/Apply-Schritte und die Workflow-Tests in der wegwerfbaren PHPUnit-Systemdatenbank aus. UI-Darstellung und reine Client-Aktionen sind damit nicht geprüft.
`views/record.twig` enthält alle drei Blöcke; `views/record.view.bake.json` registriert sie. `action/export` ignoriert die Optionen `content`, `header` und `footer`: ausschließlich `view` benutzen. Name, Dateiname und Vorlage gemeinsam ändern. Der Knopf setzt einen gespeicherten Datensatz voraus. `action/downloadFile` ist gewählt, da `download_offered` den Content-Disposition-Header prüft.
Die produktive PDF-Konvertierung benötigt die konfigurierte Brezel-Export-Infrastruktur (`brezel.export.url`). CI ruft keinen Dienst auf: `AcmeFeatureExamplesTest` fälscht nur den HTTP-Transport, prüft die gerenderten header/content/footer-Blöcke und liefert `fixtures/rendered.pdf` zurück. Der normale Business-Test-Runner liefert für Laravel-HTTP derzeit eine leere generische Antwort; seine `http_fakes` erreichen diesen Exporttransport nicht. Der Workflow-/Download-Test allein belegt deshalb keine PDF-Gültigkeit und kein wkhtmltopdf-Rendering. Diese bestehende Testlücke ist im Bericht dokumentiert.
fixtures/native.module.bake.json
fixtures/native.module.bake.json
[
{
"resource_module": "views",
"import": true,
"resource": {
"identifier": "views",
"type": "views",
"title": "name",
"fields": [
{
"identifier": "name",
"type": "text"
},
{
"identifier": "type",
"type": "text"
},
{
"identifier": "code",
"type": "code"
}
]
}
},
{
"resource_module": "files",
"import": true,
"resource": {
"identifier": "files",
"type": "files",
"title": "name",
"fields": [
{
"identifier": "name",
"type": "text"
},
{
"identifier": "path",
"type": "text"
},
{
"identifier": "mime_type",
"type": "text"
},
{
"identifier": "size",
"type": "number"
}
]
}
}
]
fixtures/rendered.pdf
fixtures/rendered.pdf
Binary file, base64 encoded (584 bytes):
JVBERi0xLjQKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZyAvUGFnZXMgMiAwIFIgPj4KZW5kb2JqCjIgMCBvYmoKPDwgL1R5cGUgL1BhZ2VzIC9LaWRzIFszIDAgUl0gL0NvdW50IDEgPj4KZW5kb2JqCjMgMCBvYmoKPDwgL1R5cGUgL1BhZ2UgL1BhcmVudCAyIDAgUiAvTWVkaWFCb3ggWzAgMCAyMDAgMjAwXSAvUmVzb3VyY2VzIDw8IC9Gb250IDw8IC9GMSA0IDAgUiA+PiA+PiAvQ29udGVudHMgNSAwIFIgPj4KZW5kb2JqCjQgMCBvYmoKPDwgL1R5cGUgL0ZvbnQgL1N1YnR5cGUgL1R5cGUxIC9CYXNlRm9udCAvSGVsdmV0aWNhID4+CmVuZG9iago1IDAgb2JqCjw8IC9MZW5ndGggNDEgPj4Kc3RyZWFtCkJUIC9GMSAxMiBUZiAyMCAxNTAgVGQgKEFDTUUgSW5jLikgVGogRVQKZW5kc3RyZWFtCmVuZG9iagp4cmVmCjAgNgowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMDkgMDAwMDAgbiAKMDAwMDAwMDA1OCAwMDAwMCBuIAowMDAwMDAwMTE1IDAwMDAwIG4gCjAwMDAwMDAyNDEgMDAwMDAgbiAKMDAwMDAwMDMxMSAwMDAwMCBuIAp0cmFpbGVyCjw8IC9TaXplIDYgL1Jvb3QgMSAwIFIgPj4Kc3RhcnR4cmVmCjQwMQolJUVPRgo=
menu.bake.json
menu.bake.json
[
{
"resource_menu": "acme_record_pdf",
"resource": {
"name": "ACME Inc.",
"elements": [
"acme_records"
]
}
}
]
modules/acme_records/acme_records.detail.json
modules/acme_records/acme_records.detail.json
{
"tabs": [
{
"identifier": "general",
"rows": [
{
"cols": [
{
"span": 24,
"components": [
{
"type": "field_group",
"identifier": "general",
"options": {
"fields": [
"name",
"content"
]
}
},
{
"type": "buttons",
"identifier": "export",
"options": {
"buttons": [
{
"identifier": "acme_export_record",
"title": "buttons.acme_export_record",
"style": "primary",
"event": {
"action": "workflow",
"event": "acme_export_record",
"module": "acme_records"
}
}
]
}
}
]
}
]
}
]
}
]
}
modules/acme_records/acme_records.index.json
modules/acme_records/acme_records.index.json
{
"tabs": [
{
"identifier": "general",
"rows": [
{
"cols": [
{
"span": 24,
"components": [
{
"type": "resource_table",
"identifier": "records",
"options": {
"module": "acme_records"
}
}
]
}
]
}
]
}
]
}
modules/acme_records/acme_records.module.bake.json
modules/acme_records/acme_records.module.bake.json
[
{
"resource_module": "acme_records",
"resource": {
"identifier": "acme_records",
"title": "name",
"fields": [
{
"identifier": "name",
"type": "text"
},
{
"identifier": "content",
"type": "textarea"
}
],
"layouts": {
"detail": "/modules/acme_records/acme_records.detail",
"index": "/modules/acme_records/acme_records.index"
}
}
}
]
preview/sample.bake.json
preview/sample.bake.json
[
{
"resource_entity": "acme_preview_record",
"resource": {
"module": "acme_records",
"fields": {
"name": "ACME Inc. record",
"content": "Example record for preview."
}
}
}
]
tests/acme_record_download.test.workflow.json
tests/acme_record_download.test.workflow.json
{
"identifier": "acme_record_download",
"workflow": "acme_record_pdf",
"fixtures": [
{
"type": "entity",
"module": "acme_records",
"alias": "record",
"fields": {
"name": "ACME Test Record",
"content": "Example content <safe>"
}
}
],
"trigger": {
"type": "dispatch_sync",
"entry": "acme_export_record",
"data": {
"prototype": "fixture:record"
}
},
"assertions": [
{
"type": "download_offered",
"filename_contains": "acme-record.pdf"
},
{
"type": "entity_exists",
"module": "files",
"where": {
"name": "acme-record.pdf",
"mime_type": "application/pdf"
}
}
]
}
translations/de.json
translations/de.json
{
"modules": {
"acme_records": {
"title": "Dokumente",
"fields": {
"name": "Name",
"content": "Inhalt"
},
"tabs": {
"general": "Allgemein"
}
}
},
"buttons": {
"acme_export_record": "PDF erzeugen"
}
}
translations/en.json
translations/en.json
{
"modules": {
"acme_records": {
"title": "Records",
"fields": {
"name": "Name",
"content": "Content"
},
"tabs": {
"general": "General"
}
}
},
"buttons": {
"acme_export_record": "Generate PDF"
}
}
translations/translations.bake.json
translations/translations.bake.json
[
{
"resource_translations": "de",
"resource": {
"identifier": "de",
"code": "de_DE",
"name": "Deutsch",
"dir": "ltr",
"provider": "deDE",
"data": "${file('translations/de.json')}"
}
},
{
"resource_translations": "en",
"resource": {
"identifier": "en",
"code": "en_US",
"name": "English",
"dir": "ltr",
"provider": "enUS",
"data": "${file('translations/en.json')}"
}
}
]
views/record.twig
views/record.twig
{% block header %}<html><body><strong>ACME Inc.</strong></body></html>{% endblock %}
{% block content %}<html><body><h1>{{ record.name|e }}</h1><p>{{ record.content|e }}</p></body></html>{% endblock %}
{% block footer %}<html><body>ACME Inc. — record export</body></html>{% endblock %}
views/record.view.bake.json
views/record.view.bake.json
[
{
"resource_view": "acme_record_pdf",
"resource": {
"module": "views",
"fields": {
"name": "acme_record_pdf",
"type": "twig",
"code": "${file('views/record.twig')}"
}
}
}
]
workflows/acme_record_pdf.workflow.bake.json
workflows/acme_record_pdf.workflow.bake.json
[
{
"resource_workflow": "acme_record_pdf",
"resource": {
"identifier": "acme_record_pdf",
"title": "ACME Inc. — acme_record_pdf",
"active": true,
"async": false,
"entry": "acme_export_record",
"elements": [
{
"name": "acme_export_record",
"type": "event/webhook",
"options": {
"module": "acme_records",
"public": false
},
"set": {
"record": "prototype:"
},
"to": {
"default": {
"makePdf": [
"default"
]
}
}
},
{
"name": "makePdf",
"type": "action/export",
"options": {
"type": "pdf",
"view": "acme_record_pdf",
"filename": "acme-record.pdf",
"view_data": {
"record": "$record"
}
},
"in": {
"default": "$record"
},
"to": {
"default": {
"download": [
"default"
]
}
}
},
{
"name": "download",
"type": "action/downloadFile",
"options": {},
"to": {}
}
],
"events": [
{
"identifier": "acme_export_record",
"type": "webhook",
"module": "acme_records"
}
]
}
}
]

Source: brezel/api@572da72238ad1aab7b60287c898eaede2c08a876

Agents: system_authoring_command reference kind=feature query=acme_record_pdf

The button uses title, style and event: {action: "workflow", module, event}. The graph entry and events[] registration use the same event identifier. async: false lets action/downloadFile return the file response. One named Twig view supplies header/content/footer blocks to action/export through view.

Copy the example files relative to your system root. Merge existing native modules, menus and roles; rename all related identifiers together. Apply preview data only in a disposable preview. Run the plan, apply and tests against that preview:

Terminal window
php bakery plan <system>
php bakery apply <system>
php bakery system test-run <system>

acme_record_download binds a saved fixture to the webhook and asserts download_offered. That checks Content-Disposition, not PDF validity. The upstream PHPUnit test separately checks rendered blocks with a fake PDF transport. Production needs brezel.export.url; test a real PDF and its page layout there.

  • Recommended for printing a record without workflow logic: module print_view. Its exact reference syntax remains unverified; check it before applying.
  • Recommended for Word-authored templates: action/renderDocx then action/convert. The complete PDF conversion chain remains unverified.
  • Recommended for physical printer output: action/print through CUPS; it is not the PDF-download path.
  • Not recommended: direct content, header, footer or page_setup action/export options. They are ignored. Use the single view’s Twig blocks, with an optional JSON setup block.

Buttons schema · Export · UI integration · Workflow tests · Assertions