Hi, I have the below code for merging Word documents. By default, it takes page no for the first page as 1. But we have a requirement that the first page should start at 2 instead of 1. So how to do this?
<?php
require_once 'fs_configuration.php';
require_once 'vendor/autoload.php';
$phpdocxFolderName = Configuration::getPhpdocxLibFolderName();
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);
$jobId = $argv[1];
$factsheetType = $argv[2];
// File paths
$INPUT_DIRECTORY = 'fs_raw_file_input/';
$OUTPUT_DIRECTORY = 'fs_raw_file_output/';
$OUTPUT_FILE = "Output.docx";
$inputFileName = $INPUT_DIRECTORY . 'master.xlsx';
startConversion();
function startConversion() {
mLog("Inside startConversion\n");
global $INPUT_DIRECTORY, $OUTPUT_DIRECTORY, $OUTPUT_FILE, $inputFileName, $factsheetType;
$docx = new CreateDocx();
$merge = new MultiMerge();
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($inputFileName);
$sheet = $spreadsheet->getActiveSheet();
$schemeCodes = array();
echo "$factsheetType";
$column = ($factsheetType == 'IDX') ? 'H' : 'G';
for ($sequenceNumber = 1; $sequenceNumber <= 200; $sequenceNumber++) {
foreach ($sheet->getRowIterator() as $row) {
$currentSequenceNumber = $sheet->getCell($column . $row->getRowIndex())->getValue();
if ($currentSequenceNumber == $sequenceNumber) {
$schemeCode = $sheet->getCell('A' . $row->getRowIndex())->getValue();
$schemeCodes[$sequenceNumber] = $schemeCode;
break;
}
}
}
$remainingFiles = array();
foreach ($schemeCodes as $sequenceNumber => $schemeCode) {
foreach (glob($INPUT_DIRECTORY . '*.docx') as $docxFile) {
$fileName = basename($docxFile);
$pos = strpos($fileName, $schemeCode);
if ($pos !== false && !ctype_alnum(substr($fileName, $pos + strlen($schemeCode), 1))) {
mLog("Adding DOCX file '$docxFile' for sequence number $sequenceNumber and scheme code $schemeCode");
$remainingFiles[] = $docxFile;
break;
}
}
}
if($factsheetType == "IDX"){
// $coverPagePDF = $INPUT_DIRECTORY . 'cover_INDEX.docx';
$indexPagePDF = $INPUT_DIRECTORY . 'glossary_INDEX.docx';
$previewPagePDF = $INPUT_DIRECTORY . 'marketupdate_INDEX.docx';
$contentPagePDF = $INPUT_DIRECTORY . 'contents_INDEX.docx';
}
if($factsheetType == "MF"){
// $coverPagePDF = $INPUT_DIRECTORY . 'cover_MF.docx';
$indexPagePDF = $INPUT_DIRECTORY . 'glossary_MF.docx';
$previewPagePDF = $INPUT_DIRECTORY . 'marketupdate_MF.docx';
$contentPagePDF = $INPUT_DIRECTORY . 'contents_MF.docx';
}
//mLog("Cover Page Path: $coverPagePDF\n");
mLog("Index Page Path: $indexPagePDF\n");
mLog("Preview Page Path: $previewPagePDF\n");
$filesAdded = array();
/*if (file_exists($coverPagePDF)) {
mLog("Cover Page found. Adding to merge process\n");
$mergeResult = $merge->mergeDocx($coverPagePDF, array(), $OUTPUT_DIRECTORY . $OUTPUT_FILE, array('mergeType' => 0));
if (!$mergeResult) {
mLog("Merge failed for cover page\n");
return;
}
$filesAdded[] = $coverPagePDF;
} else {
mLog("Cover Page not found\n");
}*/
if (file_exists($indexPagePDF)) {
mLog("Glossary Page found. Adding to merge process\n");
$mergeResult = $merge->mergeDocx($indexPagePDF, array(), $OUTPUT_DIRECTORY . $OUTPUT_FILE, array('mergeType' => 0));
if (!$mergeResult) {
mLog("Merge failed for Glossary page\n");
return;
}
$filesAdded[] = $indexPagePDF;
} else {
mLog("Glossary Page not found\n");
}
if (file_exists($previewPagePDF)) {
mLog("Market Update Page found. Adding to merge process\n");
$mergeResult = $merge->mergeDocx($OUTPUT_DIRECTORY . $OUTPUT_FILE, array($previewPagePDF), $OUTPUT_DIRECTORY . $OUTPUT_FILE, array('mergeType' => 0));
if (!$mergeResult) {
mLog("Merge failed for Market Update page\n");
return;
}
$filesAdded[] = $previewPagePDF;
} else {
mLog("Market Update Page not found\n");
}
if (file_exists($contentPagePDF)) {
mLog("Content Page found. Adding to merge process\n");
$mergeResult = $merge->mergeDocx($OUTPUT_DIRECTORY . $OUTPUT_FILE, array($contentPagePDF), $OUTPUT_DIRECTORY . $OUTPUT_FILE, array('mergeType' => 0));
if (!$mergeResult) {
mLog("Merge failed for Content page\n");
return;
}
$filesAdded[] = $contentPagePDF;
} else {
mLog("Content Page not found\n");
}
$remainingFiles = array_diff($remainingFiles, $filesAdded);
if (!empty($remainingFiles)) {
mLog("Merging All Fundfact DOCX files\n");
$mergeResult = $merge->mergeDocx($OUTPUT_DIRECTORY . $OUTPUT_FILE, $remainingFiles, $OUTPUT_DIRECTORY . $OUTPUT_FILE, array('mergeType' => 0));
if (!$mergeResult) {
mLog("Merge failed for All Fundfact DOCX files\n");
return;
}
} else {
mLog("No All Fundfact DOCX files to merge\n");
}
// Add nme.docx at the end if it exists
mLog("Conversion completed\n");
}
function mLog($message) {
$message = $message . "\n\r";
echo $message;
}
?>