Save to client location

Hi
I am using Aspose.word to generate a PDF. Follwing code saves it to Server. How ever our requirement is that it saves to client location, either to their downloads or to their browser.
I searched in this forum and found two solitions.
1. By giving Content-Disposition on doc.save but looks like doc.save no longer supports it.
2. By giving response object, which I donot have in this helper class.

Appreciate any reference to this issue.

Thanks



public static void generateAsposePDFFromHtml(String html,String fileName) throws Exception {
ByteArrayInputStream input = new ByteArrayInputStream(html.getBytes("UTF-8"));
com.aspose.words.LoadOptions loadOptions = new com.aspose.words.HtmlLoadOptions();
loadOptions.setLoadFormat(com.aspose.words.LoadFormat.HTML);
loadOptions.setEncoding(Charset.forName("UTF-8"));
com.aspose.words.Document doc = new com.aspose.words.Document(input, loadOptions);
File localFile = new File("AsposePDF.pdf");
//saving it to callidus.appserver.log.dir
/*String inputFileName = defaultFileDownloadPath(fileName);
File localFile = new File(inputFileName);*/

BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
try{
doc.save(outputStream, com.aspose.words.SaveFormat.PDF);
}catch(Exception ex){
logger.error("Exception in generating PDF using Aspose"+ex);
}finally{
outputStream.flush();
outputStream.close();
input.close();
}
}
Hi there,

Thanks for your inquiry. In your case, you need to implement an HTTPServlet before you send the document to the client browser. Please check the following code snippet. Hope this helps you.

/**
* Called from the web-app index page (because the POST method is chosen for the input form).
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// Get the output format selected by the user.
String formatType = "PDF";

try
{
// Open the stream. Read only access is enough for Aspose.Words to load a document.
InputStream stream = new FileInputStream(MyDir + "in.docx");

// Load the entire document into memory.
com.aspose.words.Document doc = new com.aspose.words.Document(stream);

// You can close the stream now, it is no longer needed because the document is in memory.
stream.close();

// Once we have a document, we can save it to a file, stream or send to the client's browser.
// We just send the document to the browser in this case.
sendToBrowser(doc, "AsposeWords", formatType, true, response);
response.flushBuffer();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}

/**
* Sends the document to the client's browser.
*/
private void sendToBrowser(Document doc, String demoName, String formatType, boolean openNewWindow, HttpServletResponse response)
throws Exception
{
String extension = formatType;

String fileName = demoName + "." + extension;

// Add the Response header
if(openNewWindow)
response.setHeader("content-disposition","attachment; filename=" + fileName);
else
response.addHeader("content-disposition","inline; filename=" + fileName);


response.setContentType("application/pdf");
doc.save(response.getOutputStream(), SaveFormat.PDF);
}

Timir

Thanks for the response. I saw this thread and mentioned in my second option that we use rest APIs and donot have response object.

2. By giving response object, which I donot have in this helper class.

How are rest APIs or helper/utilities classes handling this .

Thanks

Hi there,

Thanks for your inquiry.
amunnamg:
1. By giving Content-Disposition on doc.save but looks like doc.save no longer supports it.
Please check the detail of Document.Save methods.

You can save the document to java.io.OutputStreamoutputStream and send it to client browser using rest API. Please check following web links about sending document to client browser. Hope this helps you.

Correct way to send a file from REST web service to client

Document doc = new Document(MyDir + "input.docx");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
doc.save(outputStream, SaveFormat.PDF);