Skip to content

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 in the package “com.openkm.plugin.conversion”.
  • The new Document Converter class must be annotated with@PluginImplementation”.
  • The new Document Converter class must extend 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;
int TO_SVG = 2;
int TO_PDFA = 3;
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 the application plugin system will try to load it from there.

Method descriptions

Method Type Description
makeConversion(File input, String mimeType, File output) void The method converts the input file to a specific format and writes it to the output file.
getSourceMimeTypes() List Returns a list of valid document MIME types that are allowed to be converted.
getConversionType() int Returns the value of the MIME type to which the input document will be converted.
Allowed values are:
- Converter.TO_PDF
- Converter.TO_IMAGE
- Converter.TO_SVG
- Converter.TO_PDFA
When you choose Converter.TO_IMAGE, it is expected to return an image in PNG format.

Example of the Document Converter implementation

Section titled “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;
}
}