[我在构建使用pdfbox的应用程序时遇到了问题。当我从IDE(我使用netbeans 8.1)运行它时,该应用程序能够读取带有jbig2图像的书籍(我在pom.xml中具有jbig2的maven依赖项)。问题是当我构建创建胖子的应用程序时。当我使用相同的输入pdf运行胖罐时,出现以下错误:
“Cannot read JBIG2 image: jbig2-imageio is not installed”
注释该错误的线程似乎并不能解决我的问题(他们说,必须向pom添加一个maven依赖项,但是该依赖项已经在我的pom上。
我还检查了jbig2库类是否在胖子罐中,所以我不知道发生了什么。
我已经在一个看起来像这样的小应用程序中隔离了问题:
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图像吗?
我在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>
问题已解决。