I am converting a pdf to image and image back to pdf by the below code.
PDDocument pdDocument = new PDDocument();
PDDocument oDocument = PDDocument.load(new File(outputFile));//outputfile is the location and name of the pdf file
PDFRenderer pdfRenderer = new PDFRenderer(oDocument);
int numberOfPages = oDocument.getNumberOfPages();
PDPage page = null;
for (int i = 0; i < numberOfPages; i++) {
page = new PDPage(PDRectangle.LETTER);
BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 300, ImageType.RGB);
PDImageXObject pdImage = JPEGFactory.createFromImage(pdDocument, bim, 0.1f);
PDPageContentStream contentStream = new PDPageContentStream(pdDocument, page);
float newHeight = PDRectangle.LETTER.getHeight();
float newWidth = PDRectangle.LETTER.getWidth();
contentStream.drawImage(pdImage, 0, 0, newWidth, newHeight);
contentStream.close();
pdDocument.addPage(page);
}
pdDocument.save(outputFile);
pdDocument.close();
This code is working fine on our development server, but giving the following error on our production server:
Exception in thread "main" java.awt.AWTError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
As this code is working fine on our development server, there is no way to test it. Can someone please help me to resolve this issue or provide any alternate way to convert PDF to a single image?