PhpWord-TemplateProcessor does not replace values

107 views Asked by At

I'm trying to replace some content into a docx file but even if the saveAs works properly I dont see any modification in the new document. Here is my code :

require 'vendor/autoload.php';

use PhpOffice\PhpWord\TemplateProcessor;
use Mpdf\Mpdf;

// Chemin du fichier Word d'origine
$inputDocx = 'Attestation_CLUE_modele V1 .docx';

// Chemin du fichier Word modifié
$outputDocx = 'Attestation_CLUE_modele V1MODA.docx';

// Instancier le TemplateProcessor
$templateProcessor = new TemplateProcessor($inputDocx);

// Remplacer le texte avec des guillemets français
$templateProcessor->setValues(['NOM', 'TEST DU NOM']);
var_dump($templateProcessor);
// Sauvegarder le document Word modifié
$templateProcessor->saveAs($outputDocx);

and here is what I'm trying to replace : enter image description here

Yet nothing change, at first I was trying with the full version of what I want to change :

$templateProcessor->setValues(['«NOM»', 'TEST DU NOM']);

But without success and even without the «» that could maybe have caused some encoding issue (maybe), nothing changed

1

There are 1 answers

0
TSCAmerica.com On

Use the setValue method to replace each placeholder individually

require 'vendor/autoload.php';

use PhpOffice\PhpWord\TemplateProcessor;

// Chemin du fichier Word d'origine
$inputDocx = 'Attestation_CLUE_modele V1 .docx';

// Chemin du fichier Word modifié
$outputDocx = 'Attestation_CLUE_modele V1MODA.docx';

// Instancier le TemplateProcessor
$templateProcessor = new TemplateProcessor($inputDocx);

// Remplacer le texte
$templateProcessor->setValue('NOM', 'TEST DU NOM');

// Sauvegarder le document Word modifié
$templateProcessor->saveAs($outputDocx);