Forum


Replies: 3   Views: 309
Mergedocx() function - merging generate blank document
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 fairshareitservices  · 10-04-2024 - 07:28

We are using the below code to merge 3 documents. But BLANK output file is created in the output folder. We are using the premium version. 

<?php
proc_nice(-20);

require 'fs_configuration.php';

$phpdocxFolderName = Configuration::getPhpdocxLibFolderName();

//PHPDocx import
require_once "$phpdocxFolderName/classes/CreateDocx.php";
require_once "$phpdocxFolderName/classes/MultiMerge.php";

// Error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// File paths
$INPUT_DIRECTORY = 'fs_raw_file_input/';
$OUTPUT_DIRECTORY = 'fs_raw_file_output/';
$INPUT_FILE1 = 'Doc1.docx';
$INPUT_FILE2 = 'Doc2.docx';
$INPUT_FILE3 = 'Doc3.docx';
$OUTPUT_FILE = 'output.docx';

startConversion();

function startConversion(){
    echo "Inside startConversion\n";
    global $INPUT_DIRECTORY, $INPUT_FILE1, $OUTPUT_DIRECTORY, $OUTPUT_FILE, $INPUT_FILE2, $INPUT_FILE3;

    $docx = new CreateDocx();
    $merge = new MultiMerge();

    // Check if input files exist
    if (!file_exists($INPUT_DIRECTORY . $INPUT_FILE1) || !file_exists($INPUT_DIRECTORY . $INPUT_FILE2) || !file_exists($INPUT_DIRECTORY . $INPUT_FILE3)) {
        echo "Input files not found\n";
        return;
    }

    // Merge files
    $mergeResult = $merge->mergeDocx($INPUT_DIRECTORY . $INPUT_FILE1, array($INPUT_DIRECTORY . $INPUT_FILE2,$INPUT_DIRECTORY . $INPUT_FILE3), $OUTPUT_DIRECTORY . $OUTPUT_FILE, array('mergeType' => 1));
    
    if ($mergeResult) {
        echo "Merge successful\n";
    } else {
        echo "Merge failed\n";
        return;
    }

    // Check if output file exists
    if (!file_exists($OUTPUT_DIRECTORY . $OUTPUT_FILE)) {
        echo "Output file not created\n";
        return;
    }

    // Save the modified document to a new file
    $output_file_path = $OUTPUT_DIRECTORY . $OUTPUT_FILE;
    $docx->createDocx($output_file_path);

    echo "Conversion completed\n";
}
?>
 

 

Posted by admin  · 10-04-2024 - 07:55

Hello,

We recommend you check and run the samples available in the package and the API documentation pages. And also read the available documentation.

For example, from the mergeDocx API documentation (https://www.phpdocx.com/api-documentation/docxutilities/merge-Word-documents-with-PHP). The signature of the method:

public mergeDocx (string $firstDocument, array $documentArray, string $finalDocument, array $options) 

A sample using it:

$merge = new MultiMerge();

$merge->mergeDocx('document.docx', array('second.docx', 'other.docx'), 'output.docx', array());

The mergeDocx method generates the output.docx file with DOCX merged. Please checn and run the samples included in the package.

In your code, you are merging DOCX documents:

$mergeResult = $merge->mergeDocx($INPUT_DIRECTORY . $INPUT_FILE1, array($INPUT_DIRECTORY . $INPUT_FILE2,$INPUT_DIRECTORY . $INPUT_FILE3), $OUTPUT_DIRECTORY . $OUTPUT_FILE, array('mergeType' => 1));

and then you are saving a DOCX with the same file name, so your code overwrites the file generated with mergeDocx:

$output_file_path = $OUTPUT_DIRECTORY . $OUTPUT_FILE;
$docx->createDocx($output_file_path);

As your code hasn't added any content to $docx, an empty document is created.

We also recommend you debug if your code can read and generate all needed files. If you send to contact[at]phpdocx.com the files you are merging we'll test them using mergeDocx.

Regards.

Posted by fairshareitservices  · 11-04-2024 - 10:16

Thank you. Now documents get merged. We want to save the merge document as a pdf. How can we do this?