Creating your own Document Converter

You can create your own Document Converter.

Conditions:

  • The new Document Converter class must implement the "Converter" interface.
  • The new Document Converter class must be declared under the package "com.openkm.plugin.conversion".
  • The new Document Converter class must be annotated with "@PluginImplementation".
  • The new Document Converter class must extend of "BasePlugin".

Document Converter interface:

package com.openkm.plugin.conversion;

import com.openkm.core.ConversionException;
import net.xeoh.plugins.base.Plugin;

import java.io.File;
import java.io.IOException;
import java.util.List;

public interface Converter extends Plugin {

    int TO_PDF = 0;
    int TO_IMAGE = 1;

    void makeConversion(File input, String mimeType, File output) throws IOException, ConversionException;

    List<String> getSourceMimeTypes();

    int getConversionType();
}

The new class must be loaded into the package com.openkm.plugin.conversion because application plugins system will try to load from there.

Do not miss the tag @PluginImplementation otherwise the application plugin system will not be able to retrieve the new class.

More information at: Register a new plugin.

Methods description

MethodTypeDescription

makeConversion(File input, String mimeType, File output)

void

The method converts the file at input to some specific format into file output

getSourceMimeTypes()

List<String>

Return a list of valid document MIME TYPE allowed to be converted.

getConversionType()

int

Return the value of the MIME TYPE of that will be converted the input document.

Allowed values are:

  • Converter.TO_PDF
  • Converter.TO_IMAGE

When you choose Converter.TO_IMAGE is expected to retun an image in PNG format.

Example of the Document converter implementation

package com.openkm.plugin.conversion;

import com.openkm.core.ConversionException;
import com.openkm.core.MimeTypeConfig;
import com.openkm.plugin.BasePlugin;
import com.openkm.util.DocConverter;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


@PluginImplementation
public class SqlToPdfConversion extends BasePlugin implements Converter {

    String[] sourceMimeTypes = new String[]{MimeTypeConfig.MIME_SQL};

    @Autowired
    private DocConverter docConverter;

    @Override
    public void makeConversion(File input, String mimeType, File output) throws IOException, ConversionException {
        docConverter.src2pdf(input, output, "sql");
    }

    @Override
    public List<String> getSourceMimeTypes() {
        return Arrays.asList(sourceMimeTypes);
    }

    @Override
    public int getConversionType() {
        return Converter.TO_PDF;
    }
}