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 webservice object "ws" as shown below:
ws.login(user, password);
Once you are logged in to the webservice, 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 |
Retrieves 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 file extension. |
||
The OpenOffice service must be enabled in the OpenKM server for it to run. |
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 |
Retrieves the uploaded image with transformation. |
The variable fileName is the document file name. The application uses this variable to identify the document MIME type by its file extension. The parameter dstMimeType is the expected document MIME type of the result. By 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 in the OpenKM server for it to run. When the params value is not empty, it must always end with the chain "${fileIn} ${fileOut}". Ensure there is only a single whitespace as a separator between 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 |
Retrieves the PDF of a URL. |
The HTML to PDF conversion tool must be enabled in the OpenKM server for it to run. |
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 in the OpenKM server for it to run. |
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 in the OpenKM server for it to run. |
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 barcode tool must be enabled in the OpenKM server for it to run. |
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();
}
}
}