Execute Mail Merge Template (PHP REST)

Skip to end of metadata
Go to start of metadata
This example allows you to populate a word doucment from XML data using Saaspose.Words REST API in your PHP applications. You can also use other templates as described at http://saaspose.com/docs/display/words/executeTemplate+Resource. Please check http://saaspose.com/docs/display/words/executeMailMerge+vs+executeTemplate if you are not sure which type of mail merge you should use. Input Word template should be uploaded at Saaspose storage before running this example.
Please check http://saaspose.com/docs/display/words/SaaSpose.Words+Templates+1.0+Features for more details on how to create templates.


SaasposeApp::$AppSID  = "77***********************************";
SaasposeApp::$AppKey = "9a*******************************";
SaasposeApp::$OutPutLocation = "C:\\TempFiles\\";
$xml = simplexml_load_file("c:\\Sample.xml");

//build URI to execute XML template
$strURI = 'http://api.saaspose.com/v1.0/words/Sample.doc/executeTemplate'; 

//sign URI
$signedURI = Utils::Sign($strURI);

$responseStream = Utils::processCommand($signedURI, "POST", "", $xml);
$json = json_decode($responseStream);

//build URI to download output DOC 
$strURI = 'http://api.saaspose.com/v1.0/storage/file/' . $json->Document->FileName;

//sign URI
$signedURI = Utils::Sign($strURI);

$responseStream = Utils::processCommand($signedURI, "GET", "", "");
//save output DOC after execute template
$outputFile = SaasposeApp::$OutPutLocation . 'Output.doc';
Utils::saveFile($responseStream, $outputFile);

Simple XML Template

<?xml version="1.0" encoding="utf-8" ?> 
<Orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="OrdersSchema.xsd">
	<Order>
		<Address>
			<Number>23</Number> 
			<Street>Nelson Street</Street> 
			<Suburb>Howick</Suburb> 
			<City>Auckland</City> 
		</Address>
		<PhoneNumber>543 1234</PhoneNumber> 
		<Date>03/01/2010</Date> 
		<Total>14.00</Total> 
		 <Item>
			  <Name>BBQ Chicken Pizza</Name> 
			  <Price>6.00</Price> 
			  <Quantity>1</Quantity> 
			  <ItemTotal>6.00</ItemTotal> 
		  </Item>
		 <Item>
			  <Name>1.5 Litre Coke</Name> 
			  <Price>4.00</Price> 
			  <Quantity>2</Quantity> 
			  <ItemTotal>8.00</ItemTotal> 
		  </Item>
	</Order>
	<Order>
		<Address>
		  <Number>10</Number> 
		  <Street>Parkville Avenue</Street> 
		  <Suburb>Pakuranga</Suburb> 
		  <City>Auckland</City> 
		</Address>
		<PhoneNumber>548 7342</PhoneNumber> 
		<Date>05/03/2010</Date> 
		<Total>6.00</Total> 
		<Item>
		  <Name>Hawaiian Pizza</Name> 
		  <Price>4.00</Price> 
		  <Quantity>1</Quantity> 
		  <ItemTotal>4.00</ItemTotal> 
		</Item>
		<Item>
		  <Name>Fries</Name> 
		  <Price>1.00</Price> 
		  <Quantity>2</Quantity> 
		  <ItemTotal>2.00</ItemTotal> 
		 </Item>
	</Order>
</Orders>

XML Template with Images

<?xml version="1.0" encoding="utf-8"?>
<Items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Item>
    <Caption>This is the first caption !!!</Caption>
    <Img>folder1\image1.jpg</Img>
  </Item>
  <Item>
    <Caption>This is the second caption !!!</Caption>
    <Img>folder1\image2.jpg</Img>
  </Item>
</Items>

XML Template with Scaled Images

<?xml version="1.0" encoding="utf-8"?>
<Items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Item>
    <Caption>This is the first caption !!!</Caption>
    <Img width="60%" height="105%">folder1\image1.jpg</Img>
  </Item>
  <Item>
    <Caption>This is the second caption !!!</Caption>
    <Img>folder1\image2.jpg</Img>
  </Item>
</Items>
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.