MailMerge Example using PHP code

hello sir

i am getting the ClassNotFoundException Error while generating the aspose.words Document.

i am not able to Create an Instances of the com.aspose.words.Document and DocumentBulider Classes.

where as the other classes such as com.aspose.words.LineStyle, or other Classes Instances can be easly Created . why i am not able to Create these two Classes. so would you tell me what could be the problem.

with regards

sunil satushe

Hi

Thanks for your request. As I told you earlier I can’t reproduce this on my side. So I can’t know why this can occur on your side. Maybe you have missed anything in configuration.

Best regards.

hello sir ,

i have created the aspose.words using the php-java bridge. it is created at some location say c:\temp\out.doc. now i want to show in the html page the created document .how should i do it . also i have in.doc file for the mailmerge which is static one, how should i have the dynamic to that as well as the created out.doc file ,i want to place it dynamically into different directory . the whole thing i am talking in context with the PHP .hope you will help me out. as you have done it before many times

thanks

sunil satushe

Hi

Thanks for your inquiry. I think that you can convert your document to HTML and show it on your page. For example see the following code:

//Create stream
$outputStream = new java("java.io.ByteArrayOutputStream"); 
//Save document in HTML format to stream
$doc->save($outputStream , $saveFormat->HTML);
//Show document on page
echo($outputStream);

Hope this helps.

Best regards.

hello sir,

i am not able to trace the code. could please give the full version of this code.

thank you

sunil satushe

Hi

Thanks for your request. Here is my code:

<?php 
require\_once("http://localhost:8080/JavaBridge/java/Java.inc"); 
java\_require("lib\Aspose.Words.jdk16.jar");
$doc = new Java("com.aspose.words.Document", "C:\\Temp\\in.doc");
$builder = new Java("com.aspose.words.DocumentBuilder");
$saveFormat = new java("com.aspose.words.SaveFormat");
$builder->setDocument($doc);
$builder->write("Hello world!");
//Create stream
$outputStream = new java("java.io.ByteArrayOutputStream"); 
//Save document in HTML format to stream
$doc->save($outputStream , $saveFormat->HTML);
//Show document on page
echo($outputStream);
?>

Best regards.

hello sir,

thank you for the code snippet .it works .but how to save that generated document on disk .

as i have gone through your API’s. i found three methods of save where taking different argument. one of with the filename .other with the outputStream and saveFormat, where as the last one is the filename and fileFormat which one to use in saving the file to the disk off course after view of the genrated document.

thanks

with regards

sunil satushe

Hi

Thanks for your request. You can try using the following code:

<?php 
require\_once("http://localhost:8080/JavaBridge/java/Java.inc"); 
java\_require("lib\Aspose.Words.jdk16.jar");
$doc = new Java("com.aspose.words.Document", "C:\\Temp\\in.doc");
$builder = new Java("com.aspose.words.DocumentBuilder");
$saveFormat = new java("com.aspose.words.SaveFormat");
$builder->setDocument($doc);
$builder->write("Hello world!");
$doc->save("C:\\Temp\\out.doc");
// We'll be outputting a DOC
header('Content-type: application/msword');
// It will be called out.doc
header('Content-Disposition: inline; filename="out.doc"');
// The DOC source is in C:\Temp\out.doc
readfile("C:\\Temp\\out.doc");
?>

But note that Aspose.Words for java does not provide document preview feature.

Best regards.

hello sir,

i am doing client-server technology with php script. i want to save the out.doc generated file on server at specific location . i am using the as Linux as sever . i am fwd my code for your preview please go through it .

file name result.php

<?php require\_once("java/Java.inc");
java\_autoload();
?>
<?php 
header("Content-type: text/plain;charset=UTF-8");
java\_require("<http://xyz.com/javabridge/WEB-INF/lib/Aspose.Words.jdk15.jar>; <http://xyz.com/javabridge/WEB-INF/lib/jaxen-1.1.jar>;
<http://xyz.com/javabridge/WEB-INF/lib/RelationSet.jar>"); 
$id = $\_GET[id];
$doc = new Java("com.aspose.words.Document","/home/thinkbiz/public\_html/my/javabridge/Document/in.doc");
$myResultSet = new Java("java.sql.ResultSet");
$saveFormat = new java("com.aspose.words.SaveFormat");
$myQueryResult = new Java("RelationSet"); 
$myResultSet = $myQueryResult->execQuery($id);
$doc->getMailMerge()->executeWithRegions("tmptblclients", $myResultSet);
$outputStream = new java("java.io.ByteArrayOutputStream"); 

$doc->save($outputStream , $saveFormat->HTML);
echo($outputStream);

?>

in th above code the save method is not saving the file on the server , i want to show the out.doc file as well as to save it on some specific location.how should i do it

thanks,

with regards

sunil satushe

Hi

Thanks for your request. Of course your code does not save document on the server. This code saves document to stream in HTML format and sends stream to the client browser (see last two lines in your code). I am not strong in PHP but I think that you can save your document in root folder of your site.

//Save document in root folder of site
define("HOST",$\_SERVER['DOCUMENT\_ROOT']);
$path = HOST . "/out.doc";
//echo($path);
$doc->save($path);
// We'll be outputting a DOC
header('Content-type: application/msword');
// It will be called out.doc
header('Content-Disposition: inline; filename="out.doc"');
// The DOC source is out.doc
readfile($path);

Best regards.

hello sir,

i am working with the mailmerge.where i want to change the input file to some other dynamic once . means i have created the UI where user can change the input file for the mailmerge. so could you please tell me which constructor Documen to be used .

thank you

sunil satushe

Hi

Thanks for your request. I think that you should use Document constructor that takes file name as parameter.

$doc = new Java("com.aspose.words.Document", "C:\\Temp\\in.doc");

Best regards.

hello sir ,

how should i do with the following code. i have variable name called as $finalimagepath ,which specifices the file name how should i do it

<?php require\_once("java/Java.inc");
java\_autoload();
?>

<?php 
header("Content-type: text/plain;charset=UTF-8");
java\_require("[/javabridge/WEB-INF/lib/Aspose.Words.jdk15.jar](http://my.thinkbizkit.se/javabridge/WEB-INF/lib/Aspose.Words.jdk15.jar); [/javabridge/WEB-INF/lib/jaxen-1.1.jar](http://my.thinkbizkit.se/javabridge/WEB-INF/lib/jaxen-1.1.jar);
/[javabridge/WEB-INF/lib/RelationSet.jar](http://javabridge/WEB-INF/lib/RelationSet.jar)"); 

$id = $\_GET[id];

$imageid = $\_GET[imageid];
$imagepath = "/home/thinkbiz/public\_html/my/";
$finalimagepath = $imagepath.$imageid.".doc";
echo "final image path ".$finalimagepath;
$doc = new Java("com.aspose.words.Document",$finalimagepath);

$myResultSet = new Java("java.sql.ResultSet");
$saveFormat = new java("com.aspose.words.SaveFormat");
$myQueryResult = new Java("RelationSet"); 
$myResultSet = $myQueryResult->execQuery($id);
$doc->getMailMerge()->executeWithRegions("tmptblclients", $myResultSet);

$outputStream = new java("java.io.ByteArrayOutputStream"); 
$genFilepath = "/home/thinkbiz/public\_html/my/javabridge/Document/";
$updatedpath = $genFilepath.$id.".doc";
$doc->save($updatedpath,$saveFormat->DOC);
?>

Hi

Thanks for your inquiry. But I can’t understand what problem you have here. You should just write path into a variable and put this variable into Document constructor, something like the following:

$filepath = "C:\\Temp\\in.doc";
$doc = new Java("com.aspose.words.Document", $filepath);

I am not strong in PHP so I can’t suggest you how you should use PHP. I can just tell you how you ca use Aspose.Words.

Best regards.

hello sir ,

i have use the below code to get the mailmerge functionality. so far it is working fine it has shown the output. but when i am accessing the php page from any where other than my PC using Mozilla browser it is not giving me the output.so is it browser specific .and i want the output to be zoom one as i am using it for buisnesscard .where the buisness card is too small in size . i want to enlarge it. in all term . means in Layout as well as the content on the Card. Is the aspose.words provide some facility for that ?

thank you

sunil satushe

<?php 

require\_once("http://localhost:8080/JavaBridge/java/Java.inc"); 

java\_require("lib\Aspose.Words.jdk16.jar");

$doc = new Java("com.aspose.words.Document", "C:\\Temp\\in.doc");

$builder = new Java("com.aspose.words.DocumentBuilder");

$saveFormat = new java("com.aspose.words.SaveFormat");

$builder->setDocument($doc);

$builder->write("Hello world!");

//Create stream

$outputStream = new java("java.io.ByteArrayOutputStream"); 

//Save document in HTML format to stream

$doc->save($outputStream , $saveFormat->HTML);

//Show document on page

echo($outputStream);

?>

Hi

Thanks for your inquiry. No, unfortunately Aspose.Words for java doesn’t provide preview feature.

Best regards.