我想用Ghost4j将一些PDF文件转换成PNG文件。这个库需要log4j的工作,所以我添加到我的库也log4j。log4j不能在tomcat8服务器上使用log4j.properties文件
由于我的应用程序是动态Web项目,因此我将这些库添加到文件夹WEB-INF/lib中。
当我试图运行我的本地tomcat8服务器上的应用程序,它记录了一些警告,比如:
log4j:WARN No appenders could be found for logger (org.ghost4j.Ghostscript).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
所以,网上冲浪,我发现我不见了log4j.properties文件中我的应用程序。因此,我创建了文件log4j.properties并将其添加到WEB-INF/classes中(正如其他一些stackoverflow文章中所建议的那样)。然后,使用此修复程序,我的位置tomcat8服务器上的应用程序运行平稳。
虽然,当我试图将它部署在远程tomcat8服务器上时,应用程序不起作用。问题在于它不会产生任何类型的异常,它只会中断其工作。有或者没有log4j.properties文件,在远程tomcat8服务器上没有区别:它只是停在与之前在本地服务器上记录我之前编写的警告相同的“代码行”上运行。
任何帮助是apreciated。
P.S .:转换代码非常简单。我发布它来完成我的问题:
private void createImages(String machine, String fileName){
LOGGER.warning("started creating images");
try{
PDFDocument document = new PDFDocument();
document.load(new File(DOCS_DIR + machine + "/" + fileName + ".pdf"));
String commonPath = IMGS_DIR + machine + "/" + fileName + "/";
Path path = Paths.get(new File(commonPath).getPath());
if(Files.notExists(path, LinkOption.NOFOLLOW_LINKS)){
new File(commonPath).mkdirs();
}
SimpleRenderer renderer = new SimpleRenderer();
renderer.setResolution(300);
renderer.setAntialiasing(SimpleRenderer.OPTION_ANTIALIASING_HIGH);
LOGGER.warning("IT STOPS HERE!!");
List<Image> images = renderer.render(document); // this is the line in which the execution of the program stops..
LOGGER.warning("pdf pages are: " + images.size());
for (int i = 0; i < images.size(); i++) {
Image img = images.get(i);
Image scaledImg = img.getScaledInstance(1200, -1, Image.SCALE_SMOOTH);
BufferedImage newImage = new BufferedImage(scaledImg.getWidth(null), scaledImg.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImage.createGraphics();
g.drawImage(scaledImg, 0, 0, null);
g.dispose();
String extension = ".png";
String imgName = commonPath + (i + 1) + extension;
LOGGER.warning("creating img n: " + (i+1) + " - creating img in folder: " + imgName);
ImageIO.write((RenderedImage) newImage, "png", new File(imgName));
}
LOGGER.warning("finished creating images!");
} catch(FileNotFoundException e){
LOGGER.warning("ERROR DOCUMENT SERVICE -- FileNotFoundException");
LOGGER.warning(e.printStackTrace());
} catch (IOException e) {
LOGGER.warning("ERROR DOCUMENT SERVICE -- IOException");
LOGGER.warning(e.printStackTrace());
} catch (RendererException e) {
LOGGER.warning("ERROR DOCUMENT SERVICE -- RendererException");
LOGGER.warning(e.printStackTrace());
} catch (DocumentException e) {
LOGGER.warning("ERROR DOCUMENT SERVICE -- DocumentException");
LOGGER.warning(e.printStackTrace());
} catch (Exception e){
LOGGER.warning("ERROR DOCUMENT SERVICE -- Exception");
LOGGER.warning(e.printStackTrace());
}
}