Conversion samples
Basics
Suggested code sample
First, you must create the webservice object:
OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
Then you should log in using the method "login". You can access the "login" method from the webservice object "ws" as shown below:
ws.login(user, password);
Once you are logged in with the webservices, the session is kept in the webservice object. Then you can use the other API methods.
At this point you can use all the Conversion methods from "conversion" class as shown below:
ws.conversion.doc2pdf(is, "test.docx");
Methods
doc2pdf
Description:
Method | Return values | Description |
---|---|---|
doc2pdf(InputStream is, String fileName) |
InputStream |
Retrieve the uploaded document converted to PDF format. |
The parameter fileName is the document file name. The application uses this parameter to identify the document MIME TYPE by its extension. |
||
The OpenOffice service must be enabled on the OpenKM server to get it running. |
Example:
package com.openkm;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;
public class Test {
public static void main(String[] args) {
String host = "http://localhost:8080/openkm";
String user = "okmAdmin";
String password = "admin";
OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try {
ws.login(user, password);
InputStream is = new FileInputStream("/home/openkm/test.docx");
FileOutputStream fos = new FileOutputStream("/home/openkm/out.pdf");
InputStream convertedStream = ws.conversion.doc2pdf(is, "test.docx");
IOUtils.copy(convertedStream, fos);
is.close();
convertedStream.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
imageConvert
Description:
Method | Return values | Description |
---|---|---|
imageConvert(InputStream is, String fileName, String params, String dstMimeType) |
InputStream |
Retrieve the uploaded image with transformation. |
The variable fileName is the document file name. Application uses this variable to identify by document extension the document MIME TYPE. The parameter dstMimeType is the expected document MIME TYPE result. Using this method, you are actually executing the ImageMagick convert tool on the server side. You can set many parameters (transformations) in the params variable. Take a look at ImageMagick convert tool to get a complete list of them. The image convert tool must be enabled on the OpenKM server to get it running. When the params value is not empty, it must always end with the sequence "${fileIn} ${fileOut}". Ensure there is only a single whitespace as a separator between two parameters. When building your integrations, we suggest installing ImageMagick software locally, and checking your image transformations first from your command line. |
Example:
package com.openkm;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;
public class Test {
public static void main(String[] args) {
String host = "http://localhost:8080/openkm";
String user = "okmAdmin";
String password = "admin";
OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try {
ws.login(user, password);
InputStream is = new FileInputStream("/home/openkm/test.png");
FileOutputStream fos = new FileOutputStream("/home/openkm/out.jpg");
InputStream convertedStream = ws.conversion.imageConvert(is, "test.png", "-resize 50% ${fileIn} ${fileOut}", "image/jpeg");
IOUtils.copy(convertedStream, fos);
is.close();
convertedStream.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
html2pdf
Description:
Method | Return values | Description |
---|---|---|
html2pdf(String url) |
InputStream |
Retrieve the PDF of a URL. |
The HTML to PDF conversion tool must be enabled on the OpenKM server to get it running. |
Example:
package com.openkm;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;
public class Test {
public static void main(String[] args) {
String host = "http://localhost:8080/openkm";
String user = "okmAdmin";
String password = "admin";
OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try {
ws.login(user, password);
FileOutputStream fos = new FileOutputStream("/home/openkm/out.pdf");
InputStream is = ws.conversion.html2pdf("https://www.openkm.com/es/");
IOUtils.copy(is, fos);
is.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
doc2txt
Description:
Method | Return values | Description |
---|---|---|
doc2txt(InputStream is, String fileName) |
String |
Extracts the text from the uploaded document. |
A Text Extractor must be enabled for the document MIME TYPE on the OpenKM server to get it running. |
Example:
package com.openkm;
import java.io.FileInputStream;
import java.io.InputStream;
import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;
public class Test {
public static void main(String[] args) {
String host = "http://localhost:8080/openkm";
String user = "okmAdmin";
String password = "admin";
OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try {
ws.login(user, password);
InputStream is = new FileInputStream("/home/openkm/test.docx");
System.out.println(ws.conversion.doc2txt(is, "test.docx"));
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
img2txt
Description:
Method | Return values | Description |
---|---|---|
img2txt(InputStream is, String fileName) |
String |
Extracts the text from the uploaded image. |
The OCR engine must be enabled on the OpenKM server to get it running. |
Example:
package com.openkm;
import java.io.FileInputStream;
import java.io.InputStream;
import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;
public class Test {
public static void main(String[] args) {
String host = "http://localhost:8080/openkm";
String user = "okmAdmin";
String password = "admin";
OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try {
ws.login(user, password);
InputStream is = new FileInputStream("/home/openkm/test.png");
System.out.println(ws.conversion.img2txt(is, "test.png"));
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
barcode2txt
Description:
Method | Return values | Description |
---|---|---|
barcode2txt(InputStream is, String fileName) |
String |
Extracts the barcode text from the uploaded image. |
The Bar Code tool must be enabled on the OpenKM server to get it running. |
Example:
package com.openkm;
import java.io.FileInputStream;
import java.io.InputStream;
import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;
public class Test {
public static void main(String[] args) {
String host = "http://localhost:8080/openkm";
String user = "okmAdmin";
String password = "admin";
OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try {
ws.login(user, password);
InputStream is = new FileInputStream("/home/openkm/test.png");
System.out.println(ws.conversion.barcode2txt(is, "test.png"));
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}