1

我在构建使用 pdfbox 的应用程序时遇到问题。当我从 IDE 运行该应用程序时(我使用 netbeans 8.1),该应用程序能够阅读带有 jbig2 图像的书籍(我在 pom.xml 中有 jbig2 的 maven 依赖项)。问题是当我构建应用程序时创建了一个胖罐。当我使用相同的输入 pdf 运行 fat jar 时,会出现以下错误:

“Cannot read JBIG2 image: jbig2-imageio is not installed”

评论该错误的线程似乎没有解决我的问题(他们说必须将 maven 依赖项添加到 pom,但该依赖项已经在我的 pom 上)。

我还检查了 jbig2 库类是否在 fat jar 中,所以我不知道发生了什么。

我已经在一个看起来像这样的小应用程序中隔离了这个问题:

public static void main( String[] args )
{
    String fileName = null;
    if( args.length == 0 )
    {
        fileName = "test.pdf";
    }
    else
    {
        fileName = args[0];
    }

    PdfDocumentWrapper doc = null;
    try
    {
        PdfboxFactory factory = new PdfboxFactory();
        doc = factory.createPdfDocumentWrapper();
        doc.loadPdf( fileName );
        for( int ii = 0; ii < doc.getNumberOfPages(); ii++ )
        {
            int pageNum = ii+1;
            System.out.println("\n\nProcessing page: " + pageNum +"\n---------------------------------");
            List<ImageWrapper> imageList = doc.getImagesOfPage(ii);

            int jj=0;
            for( ImageWrapper image: imageList )
            {
                jj++;
                System.out.println(String.format("  Page[%d]. Image[%d] -> bounds: %s",
                        pageNum, jj, image.getBounds().toString() ) );
            }
        }
    }
    catch( Exception ex )
    {
        ex.printStackTrace();
    }
    finally
    {
        if( doc != null )
        {
            try
            {
                doc.close();
            }
            catch( Exception ex )
            {
                ex.printStackTrace();
            }
        }
    }
}

我将整个孤立的示例项目放在这里(目的是帮助解决问题): http ://www.frojasg1.com/20200504.PdfImageExtractor.zip

当我从 IDE 运行该应用程序时,它会产生以下输出:

Processing page: 1
---------------------------------
  Page[1]. Image[1] -> bounds: java.awt.Rectangle[x=17,y=33,width=442,height=116]
  Page[1]. Image[2] -> bounds: java.awt.Rectangle[x=53,y=513,width=376,height=124]
  Page[1]. Image[3] -> bounds: java.awt.Rectangle[x=101,y=250,width=285,height=5]
------------------------------------------------------------------------

当我从命令行运行应用程序时,它会给出以下输出:

$ java -jar ./PdfImageExtractor-v1.0-SNAPSHOT-all.jar


Processing page: 1
---------------------------------
may 04, 2020 3:40:18 PM org.apache.pdfbox.contentstream.PDFStreamEngine operatorException
GRAVE: Cannot read JBIG2 image: jbig2-imageio is not installed
may 04, 2020 3:40:18 PM org.apache.pdfbox.contentstream.PDFStreamEngine operatorException
GRAVE: Cannot read JBIG2 image: jbig2-imageio is not installed
may 04, 2020 3:40:18 PM org.apache.pdfbox.contentstream.PDFStreamEngine operatorException
GRAVE: Cannot read JBIG2 image: jbig2-imageio is not installed

有人知道为什么胖罐子无法读取 jbig2 图像吗?

4

2 回答 2

1

我在 pdfbox 用户邮件列表中发布了相同的问题,答案如下:

Your fat-jar consists of several ImageIO libs. You are simply merging all files 
to one big jar and overwriting the config files of those ImageIO libs. Have a 
look at the directory "/META-INF/services". The files of the JBig" plugin are 
overwritten by files of another plugin. Either you merge those files or don't 
create one big jar of all deps.

和解决方案:

非常感谢,原来如此!

我已经能够创建这些 META-INF 文件:

$ find src/serviceManifests/
src/serviceManifests/
src/serviceManifests/META-INF
src/serviceManifests/META-INF/services
src/serviceManifests/META-INF/services/javax.imageio.spi.ImageReaderSpi
src/serviceManifests/META-INF/services/javax.imageio.spi.ImageWriterSpi

将它们与 ImageIO jar 中的合并

通过将这些行添加到 pom.xml:

<properties>
    <service.declaration.dir>src/serviceManifests</service.declaration.dir>
    <service.files.path>META-INF/services</service.files.path>
</properties>

<build>
    <resources>
    ...
        <resource>
            <directory>${service.declaration.dir}</directory>
            <includes>
                <include>${service.files.path}/*</include>
            </includes>
        </resource>
    ...
    </resources>
    ...
</build>

问题解决了。

于 2020-05-04T20:43:02.683 回答
0

添加 ImageIO.scanForPlugins(); ImageIO.scanForPlugins() 之前;也可以解决这个问题

于 2020-12-22T01:35:27.747 回答