Perform Microsoft Word Mail Merge in PHP using Aspose.Words REST API

I’m getting started on Aspose Cloud for Word in PHP. At the outset the experience so far has been highly frustrating. There is very few examples and no complete examples where I can see say an upload, merge, convert and download. My time has been spent looking at the composer code and trying to understand the results without any documentation.

Issue 1:
Calling getListFiles returns no results, I get a {} with an empty path (or I try directory) with or without the storage name. Nothing.

Issue 2:
Calling download file returns no results.

Issue 3:
Calling postDocumentExecuteMailMerge seems to work. I get an object back but I’m not able to do anything with [document][links][15][href]? I can an authentication denied in the browser when attempting the URL and I can’t see any methods on your SDK to download it?

Issue 4:
I’m unsure if I need Aspose PDF installed to also access PDFs after mail merge.

I would appreciate a prompt response. I’ve tried to be fairly patient but I’m not getting anywhere.

Tim

@digeratisolutions

We are sorry for your inconvenience. We are in process of updating the examples of different platforms in the documentation. However, meanwhile you may please check complete unit test of APIs for the purpose.

I am creating a sample code for your other issues and will share it soon.

Thanks Tilal,

A quick update. Issue 2 is no longer a problem, an ajax response was sanitizing the object data in my testing.

@digeratisolutions

Thanks for your feedback.

The getListFiles is working fine at my end. Please find sample code below.

The postDocumentExecuteMailMerge saves output file to storage and returns possible options to convert resultant document to other supported formats. You can convert supported documents to other formats using GetDocumetnWithFormat API. You can also get the sample PHP unit test for it from github repo.

Yes, Please note if you want to manipulate the PDF file then you can use Aspose.Pdf Cloud API.

<?php

require_once('C:\xampp\htdocs\aspose-words-cloud-php-master\vendor\autoload.php');

//TODO: Get your AppSID and AppKey at https://dashboard.aspose.cloud (free registration is required).

$AppKey="xxxxxxxxxxxxxxxxxxxxx";
$AppSid="xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx";

$configuration = new Aspose\Words\Configuration();
$configuration->setAppSid($AppSid);
$configuration->setAppKey($AppKey);

$wordsApi = new Aspose\Words\WordsApi(null,$configuration); 

$storageApi = new Aspose\Storage\Api\StorageApi();
$storageApi->getConfig()->setAppKey($AppKey)->setAppSid($AppSid);

try {
		
	## List the files and folders
	$path = "output";
    $storage = null;
    $request = new Aspose\Storage\Model\Requests\GetListFilesRequest($path, $storage);
    $result = $storageApi->getListFiles($request);
    print_r($result);		
		
		
	$templateName = "SampleMailMergeTemplate.docx";
    $outputName = "ExecuteMailMerge_output.docx";
	##read mailmerge data
    $data = file_get_contents("SampleMailMergeTemplateData.txt");
	##upload template file to storage
    $putRequest = new Aspose\Storage\Model\Requests\PutCreateRequest($templateName, $templateName);
    $storageApi->PutCreate($putRequest);
	##Execute MailMerge
    $request = new Aspose\Words\Model\Requests\PostDocumentExecuteMailMergeRequest($templateName, $data, null,null, null, null, null,null, null, false, $outputName);
    $result = $wordsApi->postDocumentExecuteMailMerge($request);
        		
	## Download a file 		
	$path = "ExecuteMailMerge_output.docx";
	$versionId = null;
    $storage = null;
    $request = new Aspose\Storage\Model\Requests\GetDownloadRequest($path, $versionId, $storage);
    $result = $storageApi->getDownload($request);	
    echo "Status: ",  $result->getSize();		
	

    
    
} catch (Exception $e) {
    echo  "Something went wrong: ",  $e->getMessage(), "\n";
    PHP_EOL;
}

?>

@digeratisolutions

Kindly note we have released API Version V4.0 since Aspose.Words Cloud 19.4. It is an improved version with better memory management and API structure. In this version, we have introduced its Storage methods and combined “ExecuteMailMerge” and “ExecuteTemplate” methods along with other improvements. Now API has only “mailmerge” method which can be used with both template types: “mailmerge fields” or “mustache”. Please check the updated MailMerge PHP code. It is recommended to upgrade to the latest version. Please feel free to contact us for any assistance in this regard.

<?php

require_once('D:\xampp\htdocs\aspose-words-cloud-php-master\vendor\autoload.php');

//TODO: Get your Client Id and Secret at https://dashboard.aspose.cloud (free registration is required).

$ClientSecret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$ClientId="xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx";

$wordsApi = new Aspose\Words\WordsApi($ClientId,$ClientSecret);

try {
		
$templateName = "SampleMailMergeTemplate.docx";
$outputName = "ExecuteMailMerge_output.docx";
$remoteFolder="Temp";
#read mailmerge data
$data = file_get_contents("SampleMailMergeTemplateData.txt");

##upload template file to storage
$uploadRequest = new Aspose\Words\Model\Requests\UploadFileRequest($templateName,$remoteFolder."/".$templateName,null);
$wordsApi->uploadFile($uploadRequest);

##Execute MailMerge
$request = new Aspose\Words\Model\Requests\ExecuteMailMergeRequest($templateName, $data, $remoteFolder,null, null, null, null,null, null, false, $remoteFolder."/".$outputName);
$result = $wordsApi->executeMailMerge($request);
		
##Download file 
$request = new Aspose\Words\Model\Requests\DownloadFileRequest($remoteFolder."/".$outputName,NULL,NULL);
$result = $wordsApi->downloadFile($request);
copy($result->getPathName(),"MailMerge_output.docx");
print_r($result);
    
} catch (Exception $e) {
    echo  "Something went wrong: ",  $e->getMessage(), "\n";
    PHP_EOL;
}

?>