Replies: 3   Views: 116
Mergedocx() function - merging generate blank document

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. 


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);

// 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';


function startConversion(){
    echo "Inside startConversion\n";

    $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";

    // 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";

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

    // Save the modified document to a new file
    $output_file_path = $OUTPUT_DIRECTORY . $OUTPUT_FILE;

    echo "Conversion completed\n";


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


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 ( 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;

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] the files you are merging we'll test them using mergeDocx.


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?