使用流的 Groupdocs 转换

Groupdocs conversion using streams

我需要能够将 .pdf 文档转换为 preview/thumbnail 图像以供以后检索。它现在在我的解决方案中工作,目前涉及将新文件转储到监视文件夹中,将转换后的文件写入磁盘,然后将它们推送到 MongoDB 集合中。但我想在不将任何文件写入磁盘的情况下这样做。 (图像文件本身将进入 MongoDB 集合,生成图像文件的内容来自不同的 MongoDB 集合。)我正在使用 groupdocs 转换来执行此操作。

我有一个 class,它检索内容并将其作为 InputStream 传递到另一个 class,其感兴趣的方法如下:

public void generateRenderedImages(InputStream inputStream, String fileName, ConversionHandler conversionHandler) {

    File previewImage = null;
    File thumbnailImage = null;
    File parent = null;

    try {
        ImageSaveOptions imageSaveOptions = new ImageSaveOptions();
        imageSaveOptions.setSaveFileType(ImageSaveOptions.JPEG);
        imageSaveOptions.setPage(1);
        imageSaveOptions.setHeight(THUMBNAIL_HEIGHT);
        imageSaveOptions.setWidth(THUMBNAIL_WIDTH);
        imageSaveOptions.setSavePath(null);

        // generate preview image
        imageSaveOptions.setSaveName("blah.jpg");

        OutputStream outputStream = conversionHandler.convertToImage(inputStream, fileName, imageSaveOptions);

        saveToDatabase(outputStream, CONTENT_TYPE_PREVIEW, fileName);

        imageSaveOptions.setHeight(THUMBNAIL_HEIGHT);
        imageSaveOptions.setWidth(THUMBNAIL_WIDTH);

        outputStream.close();

        outputStream = conversionHandler.convertToImage(inputStream, fileName, imageSaveOptions);
        saveToDatabase(outputStream, CONTENT_TYPE_THUMBNAIL, fileName);
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
        log.error("Image rendering failed); error = " + e.getMessage());
        resLogger.writeLog("Image rendering failed); error = " + e.getMessage());

    } finally {
        // maybe need something here, not sure yet
    }
}

问题是我在 conversionHandler.convertToImage(...) 行抛出一个 NullPointerException -- 堆栈跟踪的相关部分将跟随 -- 即使 conversionHandler 也不是它的任何参数为空。

java.lang.NullPointerException at com.groupdocs.conversion.converter.a.(ConversionHelper.java:31) at com.groupdocs.conversion.handler.ConversionHandler.convertToImage(ConversionHandler.java:127)

我对此进行了一些研究,但我能找到的相对缺乏的文档造成了一些限制。例如这个页面:

https://github.com/groupdocs/groupdocs-conversion-java-maven-sample/blob/master/src/main/java/com/groupdocs/conversion/maven/converters/ToImageSampleConversion.java

...在此片段中积极地表明这是可能的,至少在理论上如何:

/** * Convert 1 document page to images
* On this type of conversion convertToImage returns OutputStream with converted file
* Note: converted file is not saved in local directory
* To use this type of conversion 'page' and 'saveToStream' parameters must be set in ImageSaveOptions
* convertToImage method also can receive InputStream as parameter
* See JavaDoc for more info.
* @param conversionHandler ConversionHandler instance * @param page page number */ public void convertToStream(ConversionHandler conversionHandler, int page){ ImageSaveOptions imageSaveOptions = new ImageSaveOptions(); imageSaveOptions.setSaveFileType(ImageSaveOptions.PNG); imageSaveOptions.setPage(page);

    OutputStream outputStream = conversionHandler.convertToImage(PDF_FILE, imageSaveOptions);
    //do something with the stream

遗憾的是没有setSaveToStream()方法,但是我从这个页面找到:

http://groupdocs.com/Community/files/9/java-libraries/groupdocs_conversion_for_java/entry5343.aspx

在以前的版本中已删除:

Public Api changes ... com.groupdocs.conversion.option.SaveOptions The method setSaveToStream was removed to simplify options parameters. Now for saving result document to stream output just do not set SaveOptions.setSavePath or set it to null.

所以我不必显式设置 saveToStream 属性,我只需要确保 savePath 为 null。我已经完成了,但问题仍然存在。我已经尝试了 setting/not 设置属性的所有其他组合,结果都相同。

我使用的是 groupdocs 转换的 1.2.0 版。有人以前看过这个,知道为什么我可能会在没有任何内容为 null 的情况下得到 NPE 吗?

我离开了一段时间,但今天又回来了。

这是我的解决方案:

public class RenderImages {
private static final Log log = LogFactory.getLog(RenderImages.class);

public static void generateRenderedImages(Datastore datastore,
    InputStream inputStream,
    String fileName,
    ConversionHandler conversionHandler,
    SystemProperties systemProperties,
    Logger logger) {

    int thumbnailHeight = systemProperties.getAsInteger("RENDER_THUMBNAIL_HEIGHT");
    int thumbnailWidth = systemProperties.getAsInteger("RENDER_THUMBNAIL_WIDTH");
    String contentTypePreview = systemProperties.get("RENDER_CONTENT_TYPE_PREVIEW");
    String contentTypeThumbnail = systemProperties.get("RENDER_CONTENT_TYPE_THUMBNAIL");

    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        IOUtils.copy(inputStream, outputStream);
        byte[] imageBytes = outputStream.toByteArray();
        inputStream.close();

        ImageSaveOptions imageSaveOptions = new ImageSaveOptions();
        imageSaveOptions.setSaveFileType(ImageSaveOptions.JPEG);
        imageSaveOptions.setPage(1);

        ByteArrayOutputStream previewStream = conversionHandler.convertToImage(new ByteArrayInputStream(imageBytes),
            fileName + ".pdf",
            imageSaveOptions);

        saveToDatabase(datastore, previewStream, contentTypePreview, fileName);
        previewStream.close();

        imageSaveOptions.setHeight(thumbnailHeight);
        imageSaveOptions.setWidth(thumbnailWidth);

        ByteArrayOutputStream thumbnailStream = conversionHandler.convertToImage(
            new ByteArrayInputStream(imageBytes),
            fileName + ".pdf",
            imageSaveOptions);

        saveToDatabase(datastore, thumbnailStream, contentTypeThumbnail, fileName);
        thumbnailStream.close();
    } catch (Exception e) {
        e.printStackTrace();
        log.error("Image rendering failed); error = " + e.getMessage());
        logger.writeLog("Image rendering failed); error = " + e.getMessage());

    } finally {
        // maybe need something here, not sure yet
    }
}

private static void saveToDatabase(Datastore datastore,
    ByteArrayOutputStream image, String contentType, String fileName) throws IOException {

    RenderedImage renderedImage = datastore.find(RenderedImage.class)
        .filter("attachmentId", fileName)
        .filter("contentType", contentType)
        .get();

    if (renderedImage == null) {
        renderedImage = new RenderedImage();
        renderedImage.setAttachmentId(fileName);
        renderedImage.setContentType(contentType);
        renderedImage.setContent(image.toByteArray());
        datastore.save(renderedImage);
    }
}

}