将PDF转换为tiff图像不适用于PDFBox

时间:2018-01-05 00:56:34

标签: java pdfbox

我正在使用PDFBox 2.0.8并希望将pdf转换为tiff。这不起作用,并发生以下异常

java.lang.RuntimeException: java.lang.ClassNotFoundException: com.sun.media.imageio.plugins.tiff.BaselineTIFFTagSet

at com.github.jaiimageio.impl.plugins.tiff.TIFFImageMetadata.parseIFD(TIFFImageMetadata.java:1517)
at com.github.jaiimageio.impl.plugins.tiff.TIFFImageMetadata.mergeNativeTree(TIFFImageMetadata.java:1599)
at com.github.jaiimageio.impl.plugins.tiff.TIFFImageMetadata.mergeTree(TIFFImageMetadata.java:1623)
at org.apache.pdfbox.tools.imageio.TIFFUtil.updateMetadata(TIFFUtil.java:115)
at org.apache.pdfbox.tools.imageio.ImageIOUtil.writeImage(ImageIOUtil.java:229)
at org.apache.pdfbox.tools.imageio.ImageIOUtil.writeImage(ImageIOUtil.java:146)
at org.apache.pdfbox.tools.imageio.ImageIOUtil.writeImage(ImageIOUtil.java:73)
at de.renderer.core.convert.PDFImageConverter.convert(PDFImageConverter.java:38)
at de.renderer.core.test.convert.PDFImageConverterTest.convertToTiffithDestinationPathAndSuffix(PDFImageConverterTest.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

引起:java.lang.ClassNotFoundException:com.sun.media.imageio.plugins.tiff.BaselineTIFFTagSet

at com.github.jaiimageio.impl.plugins.tiff.TIFFImageMetadata.parseIFD(TIFFImageMetadata.java:1517)
...

如pdfbox文档所述,它需要一些额外的依赖来使用tiff输出,所以我在gradle文件中添加了必要的依赖项

testCompile group: 'com.github.jai-imageio', name: 'jai-imageio-core', version: '1.3.1'jpeg2000
testCompile group: 'com.github.jai-imageio', name: 'jai-imageio-jpeg2000', version: '1.3.0'

将pdf转换为png,jpg和bmp。

这是我尝试将pdf转换为tiff

的方法
public static List<File> convert(File input, String outputPath, ImageSuffix suffix, int dpi) {

    List<File> convertList = new ArrayList<File>();

    try(PDDocument document = PDDocument.load(input)){
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        for (int page = 0; page < document.getNumberOfPages(); ++page)
        {
            BufferedImage bim = null;
            try {
                bim = pdfRenderer.renderImageWithDPI(page, dpi, ImageType.RGB);
            } catch (IOException e) {
                System.out.println(e);
            }
            String outFile = outputPath + "/" + input.getName() + "-" + (page+1) + "." + suffix.name();
            // suffix in filename will be used as the file format
            try {
                if (ImageIOUtil.writeImage(bim, outFile, dpi)){
                    convertList.add(new File(outputPath));
                }
                else{
                    throw new RuntimeException("could not write image: " + outputPath);
                }

            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }
    catch (IOException e){
        System.out.println(e);
    }

    return convertList;
}

这里是来自Junit的方法调用

 List<File> convertResultList = PDFImageConverter.convert(TESTFORM_1_FILE,  buildPath, ImageSuffix.tif, 72);

1 个答案:

答案 0 :(得分:1)

此问题的原因是错误的transitiv依赖项,它使用旧版本的ImageIOUtil类。在第一个解决方案中,我可以通过强制gradle使用所需版本的pdfbox来解决这个问题。正如蒂尔曼解释的那样,有更好的解决方案,我稍后会检查。

这里gradle配置发生了变化,之后生成了tif文件。

configurations.all{
  resolutionStrategy{
    force  'org.apache.pdfbox:pdfbox-tools:2.0.8'
}

}