Skip to content

MelisDocumentGenerator

PDF document builder that assembles versioned sections and fills $VARIABLE$ placeholders from MelisFormCreator answers. Package melisplatform/melis-document-generator.

Purpose

MelisDocumentGenerator builds PDF documents from reusable, versioned sections whose rich-text content contains $VARIABLE$ placeholders. At generation time those placeholders are filled from a linked MelisFormCreator form answer and/or from external fields provided by the companion melis-document-generator-variables package. The assembled HTML is converted to a PDF with Dompdf. Design a document once (sections + variables), then generate a filled-in PDF per form submission.

Enable it

Add to config/melis.module.load.php:

php
return [
    'MelisDocumentGenerator',
];

Required dependencies: melisplatform/melis-core (^5.3), melisplatform/melis-document-generator-variables (^5.3), dompdf/dompdf (^3.1), spipu/html2pdf (^5.2). PHP ^8.1|^8.3.

Key services

Service aliasRole
MelisDocumentGeneratorDocumentAssemblerServiceAssemble a document version, substitute variables, and render to PDF.
MelisDocumentsServiceDocuments CRUD/list; retrieve associated form.
MelisDocumentsVersionsServiceDocument versions CRUD and date-overlap validation.
MelisDocumentsVersionsSectionsServiceLinks between a document version and its ordered sections.
MelisSectionsServiceSections CRUD/list and usage lookups.
MelisSectionsVersionsServiceSection versions CRUD; getSectionsWithinDate / checkSectionHasVersion.
MelisDocumentTypesServiceDocument type classifications (CRUD + uniqueness checks).
MelisSectionTypesServiceSection type classifications (CRUD + code checks).
MelisDocumentGeneratorExternalFieldsServiceLoad external/available variables for the Available variables tab.
MelisDocumentGeneratorFormServiceLanguage-ordered form utilities.

All services extend MelisCore's general service and fire melisdocumentgenerator_*_start / _end events.

Backoffice

Accessible via Left menu → MelisMarketing → Document Generator (fa-file-text-o).

Tab / PageDescription
DocumentsList, add, edit, preview and duplicate documents. Each document has a linked form (mdd_form_id) and one or more versions.
SectionsList, add, edit, preview and duplicate sections. Sections are typed generic or dynamic and hold $VARIABLE$ placeholders in their rich-text.
Document TypesManage document type classifications.
Section TypesManage section type classifications (each carries a mdst_code).
Edition pagePer document or section: Texts, Version properties (start/end dates, status), Available variables tab.

DataTables for documents and sections support filtering by document type, section type, and linked form. The Duplicate action (DocumentGeneratorDuplicateController) clones a document or section. A preview modal is available for both.

Listener: MelisDocumentGeneratorGenerateDocumentListener hooks melisformcreator_delete_form_end and nulls mdd_form_id on any document that referenced the deleted form (FK cleanup).

Variables & placeholder substitution

Section text uses the format $VARIABLE_NAME$. At generation the assembler matches placeholders with /\$(\w+(?:-\w+)*)\$/ and replaces each one from:

  • Form answer — reads mfa_form_config (field definitions) and mfa_form_answers (values) from the linked FormCreator answer. Handles text/textarea, file upload, datepicker (locale-formatted en_EN/fr_FR), radiocell, checkbox, and select (radio/checkbox support solo / horizontal / vertical display variants).
  • External fields — extra values merged via the melisdocument_get_external_values_start event (provided by melis-document-generator-variables); types file, partnerfile, specialfile.

Example

php
// Generate a filled PDF for a given form answer
$assembler = $serviceLocator->get('MelisDocumentGeneratorDocumentAssemblerService');
$result = $assembler->generateDocument(
    $contents,          // array of section content (text)
    $path,              // optional output path
    $documentVersionId,
    $formId,
    $formAnswerId,      // drives $VARIABLE$ substitution
    $language
);
// Returns a JsonModel with a base64-encoded PDF (or an error)

PDF output: A4 portrait, DejaVu Sans, Dompdf HTML5 parser enabled, remote resources disabled. <img> elements are inlined as base64 data-URIs via imageTrait::convertImagesToBase64.

Database tables

TableHolds
melis_docgen_documents / _transA document: status, document type, linked form (mdd_form_id).
melis_docgen_documents_versions / _transA document version: status, mddv_start_date, mddv_end_date.
melis_docgen_documents_versions_sectionsLink table — which sections a document version contains (mddvs_mddv_id, mddvs_mds_id, mddvs_position).
melis_docgen_sections / _transA section: type ENUM(generic, dynamic), status, doc type, section type, linked form (mds_form_id).
melis_docgen_sections_versions / _transA section version: status, mdsv_start_date, mdsv_end_date, version text.
melis_docgen_documents_types / _transDocument type classifications.
melis_docgen_sections_types / _transSection type classifications (with mdst_code).

Key files

ConcernPath
Assembler (PDF generation)vendor/melisplatform/melis-document-generator/src/Service/MelisDocumentGeneratorDocumentAssemblerService.php
External fields servicevendor/melisplatform/melis-document-generator/src/Service/MelisDocumentGeneratorExternalFieldsService.php
FK cleanup listenervendor/melisplatform/melis-document-generator/src/Listener/MelisDocumentGeneratorGenerateDocumentListener.php
Image-to-base64 traitvendor/melisplatform/melis-document-generator/src/Trait/imageTrait.php
Service / table configvendor/melisplatform/melis-document-generator/config/module.config.php
Back-office treevendor/melisplatform/melis-document-generator/config/app.toolstree.php
DataTable configvendor/melisplatform/melis-document-generator/config/app.tools.php
DB migrationsvendor/melisplatform/melis-document-generator/install/dbdeploy/
PDF viewvendor/melisplatform/melis-document-generator/view/melis-document-generator/documents-versions/document-version-pdf.phtml

See also: melis-core