Search in sources :

Example 1 with OfficeDocumentConverter

use of org.artofsolving.jodconverter.OfficeDocumentConverter in project openmeetings by apache.

the class DocumentConverter method doJodConvert.

/**
 * Generates PDF using JOD Library (external library)
 *
 * @param in - file to convert
 * @param out - file to write result
 * @return - result of the conversion as {@link ProcessResult}
 */
public ProcessResult doJodConvert(File in, File out) {
    try {
        createOfficeManager(cfgDao.getString(CONFIG_PATH_OFFICE, null), man -> {
            OfficeDocumentConverter converter = new OfficeDocumentConverter(man);
            converter.convert(in, out);
        });
    } catch (Exception ex) {
        log.error("doJodConvert", ex);
        return new ProcessResult("doJodConvert", ex.getMessage(), ex);
    }
    return new ProcessResult("doJodConvert", "Document converted successfully", null).setExitCode(0);
}
Also used : ProcessResult(org.apache.openmeetings.util.process.ProcessResult) OfficeDocumentConverter(org.artofsolving.jodconverter.OfficeDocumentConverter)

Example 2 with OfficeDocumentConverter

use of org.artofsolving.jodconverter.OfficeDocumentConverter in project xwiki-platform by xwiki.

the class DefaultOfficeServer method initialize.

/**
 * Initialize JodConverter.
 *
 * @throws OfficeServerException when failed to initialize
 */
public void initialize() throws OfficeServerException {
    if (this.config.getServerType() == OfficeServerConfiguration.SERVER_TYPE_INTERNAL) {
        DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
        configuration.setPortNumber(this.config.getServerPort());
        String homePath = this.config.getHomePath();
        if (homePath != null) {
            configuration.setOfficeHome(homePath);
        }
        String profilePath = this.config.getProfilePath();
        if (profilePath != null) {
            configuration.setTemplateProfileDir(new File(profilePath));
        }
        configuration.setMaxTasksPerProcess(this.config.getMaxTasksPerProcess());
        configuration.setTaskExecutionTimeout(this.config.getTaskExecutionTimeout());
        try {
            this.jodManager = configuration.buildOfficeManager();
        } catch (Exception e) {
            // We wrap this in an OfficeServerException in order to display some nicer message to the user.
            throw new OfficeServerException("Failed to start Office server. Reason: " + e.getMessage(), e);
        }
    } else if (this.config.getServerType() == OfficeServerConfiguration.SERVER_TYPE_EXTERNAL_LOCAL) {
        ExternalOfficeManagerConfiguration externalProcessOfficeManager = new ExternalOfficeManagerConfiguration();
        externalProcessOfficeManager.setPortNumber(this.config.getServerPort());
        externalProcessOfficeManager.setConnectOnStart(true);
        this.jodManager = externalProcessOfficeManager.buildOfficeManager();
    } else {
        setState(ServerState.CONF_ERROR);
        throw new OfficeServerException("Invalid office server configuration.");
    }
    this.jodConverter = null;
    // Try to use the JSON document format registry to configure the office document conversion.
    InputStream input = getClass().getResourceAsStream(DOCUMENT_FORMATS_PATH);
    if (input != null) {
        try {
            this.jodConverter = new OfficeDocumentConverter(this.jodManager, new JsonDocumentFormatRegistry(input));
        } catch (Exception e) {
            this.logger.warn("Failed to parse {} . The default document format registry will be used instead.", DOCUMENT_FORMATS_PATH, e);
        }
    } else {
        this.logger.debug("{} is missing. The default document format registry will be used instead.", DOCUMENT_FORMATS_PATH);
    }
    if (this.jodConverter == null) {
        // Use the default document format registry.
        this.jodConverter = new OfficeDocumentConverter(this.jodManager);
    }
    File workDir = this.environment.getTemporaryDirectory();
    this.converter = new DefaultOfficeConverter(this.jodConverter, workDir);
}
Also used : JsonDocumentFormatRegistry(org.artofsolving.jodconverter.document.JsonDocumentFormatRegistry) DefaultOfficeConverter(org.xwiki.officeimporter.internal.converter.DefaultOfficeConverter) OfficeServerException(org.xwiki.officeimporter.server.OfficeServerException) InputStream(java.io.InputStream) ExternalOfficeManagerConfiguration(org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration) File(java.io.File) DefaultOfficeManagerConfiguration(org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration) OfficeServerException(org.xwiki.officeimporter.server.OfficeServerException) OfficeDocumentConverter(org.artofsolving.jodconverter.OfficeDocumentConverter)

Example 3 with OfficeDocumentConverter

use of org.artofsolving.jodconverter.OfficeDocumentConverter in project fess-crawler by codelibs.

the class JodExtractor method getText.

/*
     * (non-Javadoc)
     *
     * @see org.codelibs.fess.crawler.extractor.Extractor#getText(java.io.InputStream,
     * java.util.Map)
     */
@Override
public ExtractData getText(final InputStream in, final Map<String, String> params) {
    if (in == null) {
        throw new CrawlerSystemException("in is null.");
    }
    final String resourceName = params == null ? null : params.get(TikaMetadataKeys.RESOURCE_NAME_KEY);
    String extension;
    String filePrefix;
    if (StringUtil.isNotBlank(resourceName)) {
        final String name = getFileName(resourceName);
        final String[] strings = name.split("\\.");
        final StringBuilder buf = new StringBuilder(100);
        if (strings.length > 1) {
            for (int i = 0; i < strings.length - 1; i++) {
                if (buf.length() != 0) {
                    buf.append('.');
                }
                buf.append(strings[i]);
            }
            filePrefix = buf.toString();
            extension = strings[strings.length - 1];
        } else {
            filePrefix = name;
            extension = "";
        }
    } else {
        filePrefix = "none";
        extension = "";
    }
    File inputFile = null;
    File outputFile = null;
    try {
        inputFile = File.createTempFile("jodextin_" + filePrefix + "_", StringUtil.isNotBlank(extension) ? "." + extension : extension, tempDir);
        final String outExt = getOutputExtension(extension);
        outputFile = File.createTempFile("cmdextout_" + filePrefix + "_", "." + outExt, tempDir);
        // store to a file
        CopyUtil.copy(in, inputFile);
        final OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        converter.convert(inputFile, outputFile);
        final ExtractData extractData = new ExtractData(getOutputContent(outputFile, outExt));
        if (StringUtil.isNotBlank(resourceName)) {
            extractData.putValues("resourceName", new String[] { resourceName });
        }
        return extractData;
    } catch (final IOException e) {
        throw new ExtractException("Could not extract a content.", e);
    } finally {
        if (inputFile != null && !inputFile.delete()) {
            logger.info("Failed to delete " + inputFile.getAbsolutePath());
        }
        if (outputFile != null && !outputFile.delete()) {
            logger.info("Failed to delete " + outputFile.getAbsolutePath());
        }
    }
}
Also used : ExtractException(org.codelibs.fess.crawler.exception.ExtractException) ExtractData(org.codelibs.fess.crawler.entity.ExtractData) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) IOException(java.io.IOException) File(java.io.File) OfficeDocumentConverter(org.artofsolving.jodconverter.OfficeDocumentConverter)

Aggregations

OfficeDocumentConverter (org.artofsolving.jodconverter.OfficeDocumentConverter)3 File (java.io.File)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ProcessResult (org.apache.openmeetings.util.process.ProcessResult)1 JsonDocumentFormatRegistry (org.artofsolving.jodconverter.document.JsonDocumentFormatRegistry)1 DefaultOfficeManagerConfiguration (org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration)1 ExternalOfficeManagerConfiguration (org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration)1 ExtractData (org.codelibs.fess.crawler.entity.ExtractData)1 CrawlerSystemException (org.codelibs.fess.crawler.exception.CrawlerSystemException)1 ExtractException (org.codelibs.fess.crawler.exception.ExtractException)1 DefaultOfficeConverter (org.xwiki.officeimporter.internal.converter.DefaultOfficeConverter)1 OfficeServerException (org.xwiki.officeimporter.server.OfficeServerException)1