Forum


Replies: 3   Views: 370
Converting a merge docx into pdf or speed improvement
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by TristanCA  · 06-02-2024 - 08:39

hello guys, i'm using the latest version of php and phpdocx and i'm using the msword method and a windows server.
I was converting a template into multiple docx and then converting them into multiple pdf, which was a bit slow and not convenient so i decided to merge the docx first and then convert the merged docx in pdf. The merged docx is perfect and is built in no time but when i try to convert it in pdf, i get a timeout. So i'm wondering why, or maybe what's the fastest way that i can convert my template into a pdf with multiple pages.

Here's my code : 
 

<?php
require_once './Assets/phpdocx/14.5/classes/CreateDocx.php';
require_once './Assets/phpdocx/14.5/classes/MultiMerge.php';

$V_docx = new CreateDocxFromTemplate('./templateTableau.docx');
$V_merge = new MultiMerge();
$V_listeDoc = array(); // Initialisation de la liste des documents à fusionner
$V_variables = $V_docx->getTemplateVariables();
$V_article = $_POST['article'];
$V_designation = $_POST['designation'];
$V_signature = $_POST['signature'];
$V_nbimpression = $_POST['nbimpression'];
$V_arrayVariables = array(
        $V_variables['document'][0] => $V_article,
        $V_variables['document'][1] => $V_designation,
        $V_variables['document'][2] => $V_signature,
        $V_variables['document'][3] => $V_signature
);

$V_docx->replaceVariableByText($V_arrayVariables);

for ($V_i = 1; $V_i <= $V_nbimpression; $V_i++)
{
        $V_filePath = './Tmp/genere/document' . $V_i . '.docx';
        if (file_exists($V_filePath))
        {
                unlink($V_filePath); // Supprime le fichier existant
        }

        $V_docx->createDocx($V_filePath);
        $V_listeDoc[] = $V_filePath; // Ajoute le chemin du fichier à la liste des documents à fusionner
}

$V_outputDocxPath = 'C:\\inetpubPHP\\wwwroot\\Tmp\\genere\\document_merged.docx';

// Vérifier si le document fusionné existe déjà 
if (file_exists($V_outputDocxPath))
{
        unlink($V_outputDocxPath); // Supprime le fichier existant avant de fusionner
}

// Fusionner les documents en un seul fichier docx avant la conversion en PDF
$V_merge->mergeDocx($V_listeDoc[0], array_slice($V_listeDoc, 1), $V_outputDocxPath, array('mergeType' => 0));

// Convertir le document fusionné en PDF
$V_docx->transformDocument($V_outputDocxPath, str_replace('.docx', '.pdf', $V_outputDocxPath), 'msword');