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
Sample Code for Converting PDF File to HTML Locally
Code:
SaasposeApp.setAppKey("9a6************************");
SaasposeApp.setAppSID("77**************************");
//build uri to convert Pdf file
String strURI = "api*saaspose*com/v1.0/pdf/convert?format=html";
//sign URI
String signedURI = Sign(strURI);
InputStreamfileStream = new FileInputStream(inputPath);
//get response stream
InputStreamhtmlOutput = ProcessCommand(signedURI, "PUT", fileStream);
String destination = "c:\\OutputHTML\\";
boolean success = (new File(destination)).mkdir();
if (success) {
getZippedFiles(htmlOutput, destination);
}
public static void getZippedFiles(InputStreamzipFile, String destination) {
try {
byte[] buf = new byte[1024];
ZipInputStreamzipinputstream = null;
ZipEntryzipentry;
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;
FileOutputStreamfileoutputstream;
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();
}
}