Conversion samples

Basics

Suggested code sample

First, you must create the webservice object:

OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

Then should login using the method "login". You can access the "login" method from webservice object "ws" as is shown below:

ws.login(user, password);

Once you are logged with the webservices the session is keep in the webservice Object. Then you can use the other API method

At this point you can use all the Conversion methods from "conversion" class as is shown below:

ws.conversion.doc2pdf(is, "test.docx");

Methods

doc2pdf

Description:

MethodReturn valuesDescription

doc2pdf(InputStream is, String fileName)

InputStream

Retrieve the uploaded document converted to PDF format.

The parameter fileName is the document file name. Application uses this parameter to identify by document extension the document MIME TYPE.

The openoffice service must be enabled in 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:

MethodReturn valuesDescription

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 really executing on server side the ImageMagick convert tool.

You can set a lot of parameters - transformations - in params variable. Take a look at ImageMagick convert tool to get a complete list of them.

The image convert tool must be enabled in OpenKM server to get it running.

When params value is not empty always must contains ends with the chain "${fileIn} ${fileOut}".

Ensure there is only a white space as separator between two parameters.

When building your integrations, we suggest installing ImageMagic software locally, and check 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:

MethodReturn valuesDescription

html2pdf(String url)

InputStream

Retrieve the PDF of an URL.

The HTML to PDF conversion tool must be enabled in 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:

MethodReturn valuesDescription

doc2txt(InputStream is, String fileName)

String

Extracts the text from the upload document.

Must be enabled a Text Extractor for the document MIME TYPE in 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:

MethodReturn valuesDescription

img2txt(InputStream is, String fileName)

String

Extracts the text from the uploaded image.

The OCR engine must be enabled in 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:

MethodReturn valuesDescription

barcode2txt(InputStream is, String fileName)

String

Extracts the barcode text from the uploaded image.

The Bar Code tool must be enabled in 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();
        }
    }
}