我正在为Bitbucket Server编写一个插件,其中必须处理将PDF渲染为图像的问题。我为此使用PDFBox。我有一个pdfToPng
方法将被调用来进行处理,并且我已经修改了PDF渲染器,类似于Apache示例所建议的。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.rendering.PageDrawer;
import org.apache.pdfbox.rendering.PageDrawerParameters;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class PDFProcessor {
private static Logger logger = LoggerFactory.getLogger(PDFProcessor.class);
private static int _colorMode;
/**
* Convert a page of a PDF document into a colored PNG
* @param color color to use, 0 - black, 1- red
* @param fileName output file
* @param pdfFile PDF to process
* @throws IOException
*/
public static void pdfToPng(int color, String fileName, File pdfFile) throws IOException{
_colorMode = color;
try (PDDocument pdfDoc = PDDocument.load(pdfFile)){
logger.info("Begin PDF to PNG render for " + fileName);
PDFRenderer renderer = new CustomPdfRenderer(pdfDoc);
BufferedImage image = renderer.renderImageWithDPI(0,600);
logger.info("Making PNG transparent...");
BufferedImage transparentImage = ImageProcessor.makeTransparent(image,new Color(image.getRGB(0,0)));
ImageIO.write(transparentImage, "PNG",new File(fileName+".png"));
logger.info("Image processed successfully, writing to " + fileName + ".png");
}
}
private static class CustomPdfRenderer extends PDFRenderer{
CustomPdfRenderer(PDDocument document){
super(document);
}
@Override
protected PageDrawer createPageDrawer(PageDrawerParameters params) throws IOException{
return new CustomPageDrawer(params);
}
}
private static class CustomPageDrawer extends PageDrawer{
CustomPageDrawer(PageDrawerParameters params) throws IOException{
super(params);
}
@Override
protected Paint getPaint(PDColor color) throws IOException{
if ((color.toRGB() == (Color.BLACK.getRGB() & 0x00FFFFFF)) && (_colorMode == 1)){
return Color.RED;
}
return super.getPaint(color);
}
}
}
该项目是使用Maven构建的,因为这是BitBucket用于插件开发的时候。但是,当实际调用该方法时,我得到一个ClassNotFound
异常说明:
[INFO] Caused by: java.lang.NoClassDefFoundError: org/apache/pdfbox/rendering/PDFRenderer
[INFO] at com.my-plugin.DiffManager.prepareDiff(DiffManager.java:99)
[INFO] at com.my-plugin.DiffManager.doGet(DiffManager.java:67)
[INFO] at com.my-plugin.DiffManager.doPost(DiffManager.java:84)
[INFO] ... 33 common frames omitted
[INFO] Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.rendering.PDFRenderer not found by com.my-plugin.integrationPlugin [219]
[INFO] at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
[INFO] ... 36 common frames omitted
如您所见,我已经导入了PDFRenderer
类。我的pom.xml
还包含正确的依赖项定义:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.14</version>
<scope>provided</scope>
</dependency>
为什么Java无法找到PDFRenderer
类?以这种方式声明的其他依赖项永远不会出现此问题。
答案 0 :(得分:2)
您正在将pdfbox
与<scope>provided</scope>
的依赖关系使用。您期望谁提供此依赖关系?像Tomcat这样的应用程序容器?
我建议您删除scope
行,这样依赖项将隐式为compile
范围,这将为您解决问题。
也请咨询Maven scope docs,其中指出:
compile
–这是默认范围,如果未指定,则使用该范围。编译依赖项在项目的所有类路径中均可用。
provided
–类似于编译,但是表明您希望JDK或容器在运行时提供依赖项。