Forum


Replies: 3   Views: 109
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. 

<?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";
}
?>