Menu

Error converting PDF file unit test.

Help
Tom
2017-01-25
2017-01-25
  • Tom

    Tom - 2017-01-25

    I'm getting the following error message when running my code from a Spring WebMvcTest. The code works correctly when called from my application, just not in the test. Code is:

        private byte[] createThumbnailImage(byte[] imageData, String imageFormat) throws ImageConversionException {
    
            ByteArrayOutputStream os = new ByteArrayOutputStream();
    
            try {
                InputStream is = new ByteArrayInputStream(imageData);
    
                IMOperation op = new IMOperation();
                op.addImage("-");
                op.thumbnail(THUMBNAIL_SIZE);
    
                if (imageFormat.equalsIgnoreCase(PDF_FILE_EXTENSION)) {
                    op.background("white");
                    op.alpha("remove");
                    //create PNG image output for PDF files
                    op.addImage(PNG_FILE_EXTENSION+":-");
                }
                else {
                    op.addImage(imageFormat+":-");
                }
    
                Pipe pipeIn  = new Pipe(is,null);
                Pipe pipeOut = new Pipe(null,os);
    
                ConvertCmd convert = new ConvertCmd();
                convert.setInputProvider(pipeIn);
                convert.setOutputConsumer(pipeOut);
                convert.run(op);
    
            }
            catch (IOException | IM4JavaException | InterruptedException e) {
    
                if (e.getMessage().contains("Cannot run program \"convert\"")) {
                    String message = "Imagemagick not found or not installed.";
                    LOG.error(message);
                    throw new ImageConversionException(message);
                }
    
                throw new ImageConversionException(e.getMessage());
            }
    
            return os.toByteArray();
        }
    

    I can run the same test for an image file (.jpg) and it converts correctly, both in the test and when the application is running. When converting for a PDF the command passed to IM is:

    - -thumbnail 255 -background white -alpha remove png:-

    The command is the same both when running the application and when calling the same code from a unit test, but in the unit test I get the following exception thrown and error message:

    CommandException: magick: no images found for operation-thumbnail' at CLI arg 2`

    I have IM and Ghostscript installed locally. Could there be a problem with IM calling Ghostscript, or a Pipe wrapper problem from a unit test? Appreciate any help. Thanks,

    Tom

     

    Last edit: Tom 2017-01-25
  • Bernhard Bablok

    Bernhard Bablok - 2017-01-29

    I'm a bit confused by "CommandException: magick". This suggests a new version of IM where all commands like "convert" are symlinks to "magick". Could you please check this? You could try to replace the symlinks with hardlinks. The magick-command behaves differently depending on the name it is called with, but im4java already resolves to the real name while searching for the command so this information gets lost.

    Bernhard

     

Log in to post a comment.