This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

PDF File to HTML Conversion in Java Applications Using REST API  RSS feed

 
sher azam
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This technical tip allows developers to convert local PDF file to HTML without using Saaspose or any other storage using Saaspose.Pdf REST API in your Java applications. Saaspose API returns the output as a ZIP file when converting to HTML. We will create and use getZippedFiles method in this example to save output ZIP file as HTML. Some important steps for performing this task is to build url to convert Pdf file, sign URI, execute signed URI request and get response stream.

Sample Code for Converting PDF File to HTML Locally

SaasposeApp.setAppKey("9a6************************");
SaasposeApp.setAppSID("77**************************");
//build uri to convert Pdf file
String strURI = "http://api.saaspose.com/v1.0/pdf/convert?format=html";
//sign URI
String signedURI = Sign(strURI);
InputStream fileStream = new FileInputStream(inputPath);
//get response stream
InputStream htmlOutput = ProcessCommand(signedURI, "PUT", fileStream);

String destination = "c:\\OutputHTML\\";
boolean success = (new File(destination)).mkdir();

if (success) {
getZippedFiles(htmlOutput, destination);
}

public static void getZippedFiles(InputStream zipFile, String destination) {
try {

byte[] buf = new byte[1024];
ZipInputStream zipinputstream = null;
ZipEntry zipentry;
zipinputstream = new ZipInputStream(zipFile);

zipentry = zipinputstream.getNextEntry();
while (zipentry != null) {
// for each entry to be extracted
String entryName = destination + zipentry.getName();
entryName = entryName.replace('/', File.separatorChar);
entryName = entryName.replace('\\', File.separatorChar);
System.out.println("entryname " + entryName);
int n;
FileOutputStream fileoutputstream;
File newFile = new File(entryName);
if (zipentry.isDirectory()) {
if (!newFile.mkdirs()) {
break;
}
zipentry = zipinputstream.getNextEntry();
continue;
}

fileoutputstream = new FileOutputStream(entryName);

while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
fileoutputstream.write(buf, 0, n);
}

fileoutputstream.close();
zipinputstream.closeEntry();
zipentry = zipinputstream.getNextEntry();

}// while

zipinputstream.close();
} catch (Exception e) {
e.printStackTrace();
}
}

Overview: Saaspose.Pdf

Saaspose.Pdf is a REST API to create, edit & manipulate PDF files. It also convert PDF file to DOC, DOCX, HTML, XPS, TIFF etc. You can create a new PDF either from scratch or from HTML, XML, template, database, XPS or an image. A PDF file can also be rendered to JPEG, PNG, GIF, BMP, TIFF and many other image formats. It works with any language like .NET, Java, PHP, Ruby, Python and many others. It is platform independent REST API & working with web, desktop, mobile or cloud applications alike.

More about Saaspose.BarCode

- Homepage of Saaspose.Pdf
- More Technical Tips by Saaspose.Pdf
- Ask technical questions/queries from Saaspose Support Team

Contact Information
Aspose Pty Ltd, Suite 163,
79 Longueville Road
Lane Cove, NSW, 2066
Australia
Saaspose - Your File Format Experts 2.0
sales@aspose.com
Phone: 1.214.329.1520
Fax: 866.810.9465
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!