Forum


Replies: 3   Views: 374
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');

 

Posted by admin  · 06-02-2024 - 10:35

Hello,

The time needed to transform a DOCX to PDF depends on the document complexity and the server performance. Unfortunately, there's no way to improve the time needed to transform a DOCX to PDF except using a faster server.
If you are getting a PHP timeout we recommend you test the same transformation using PHP CLI mode, so you can check the exact time the conversion needs.

An alternative in some cases would be transforming smaller DOCX documents (instead of a single big DOCX) to PDF, and then merge all PDF outputs into a single PDF using mergePDF (https://www.phpdocx.com/api-documentation/docxutilities/merge-pdf-documents-with-PHP).

The conversion plugin based on LibreOffice is usually faster than the MS Word one, but it requires DOCX that use contents and styles supported by LibreOffice.

Regards.

Posted by TristanCA  · 06-02-2024 - 15:57

hi, the thing that bother me is that when i convert a single docx into a pdf it takes very little time but when i convert a single docx, merge it and then convert to pdf, i timeout. The docx that i try to convert are exactly the same but the merged one timeout. I dont get it. maybe you have the awnser

Posted by admin  · 06-02-2024 - 16:26

Hello,

The DOCX output generated by the mergeDocx method is as any other DOCX. There's no reason why a merged DOCX output isn't transformed as any other DOCX unless some warning appears when opening it with MS Word or MS Word can't read it due to missing permissions.

Can you transform the DOCX doing only the transformation? Instead of doing the whole process, if you test only the transformation with the merged DOCX, is it transformed correctly or do you get a timeout too?

If you send to contact[at]phpdocx.com a DOCX sample you are transforming to PDF and returns a timeout error, we'll check it in a test server.

Regards.