Convert HTML to PDF and send it to browser using REST protocol

Hi,

I am trying to write a REST service that will convert received HTML document to PDF in java and return back the converted PDF to the calling client. I am getting stuck in sending back the converted PDF to the browser. I have searched all help and examples but couldn’t find anything similar to what i want to do. following code snippet would explain what i want to do

@GET
@Path("/get")
@Produces(“application/pdf”)
public Response convertpdf() {

	String savedir="/users//";
	   String FILE_PATH = "/users/testuser/trac.html";
	
			
	   String basePath = "https://trac.abc.cnn.com";
			HtmlLoadOptions htmloptions = new HtmlLoadOptions(basePath);
			// Load HTML file
			Document doc = new Document(FILE_PATH, htmloptions);
			// Save HTML file
			
			ByteArrayOutputStream output = new ByteArrayOutputStream();
			doc.save(output, com.aspose.pdf.SaveFormat.Pdf);
	  
	  try{
	ResponseBuilder response = Response.ok((Object) output.toByteArray());
	response.header("Content-Disposition",
			"attachment; filename=trac.pdf");
	response.type("application/pdf");
	return response.build();
}
finally
{
	if (doc != null)
		doc.dispose();
}

}

@shiv.kadalli,
Your query is not really about the Aspose.Pdf for Java API. You are seeking a way to send a PDF file to the client in response of a REST API call and this forum is specific to the issues of Aspose.Pdf API. Please Google for an appropriate forum and post your query there. However, if you come across any problem which is about the Aspose.Pdf API, then please let us know, we will assist you as per your need.

Best Regards,
Imran Rafique

Thanks for your response. I beg to differ on your assessment. My question is related to Document class Save method that converts the HTML and saves it to the disk. Instead of saving to disk, i wanted to know if there are any methods that converts the pdf and hold it in memory stream that can be pushed to browser as octet-stream. I did not find any examples doing that. If you have some examples, I would appreciate if you could point me to that link.

Thanks
Shiv

@shiv.kadalli,

In the shared code example, you are already saving a PDF in the stream object, which stores PDF in the main memory, not to the disk. Please also refer to the last sub topic: Save PDF to ByteArrayOutputStream

Besides this, you can use our Aspose.Pdf for Cloud service. Please refer to the help topics: Convert PDF to other Formats without using Storage and Aspose for Cloud UI Help Topics.