As a part of large webapp, I have a print function to print PDF files:
public void printResource(String path) {
// load file
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(path);
} catch (FileNotFoundException e) {
LOGGER.error("Problem trying to load the file!",e);
}
// file settings
DocFlavor docFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc document = new SimpleDoc(inputStream, docFormat, null);
// get default print service
PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
if (defaultPrintService != null) {
DocPrintJob printJob = defaultPrintService.createPrintJob();
try {
printJob.print(document, attributeSet);
} catch (PrintException e) {
LOGGER.error("PrintException!",e);
}
} else {
LOGGER.error("No printer has been found");
}
try {
inputStream.close();
} catch (IOException e) {
LOGGER.error("Ignoring error closing inputStream");
}
}
Sometimes, it works and sometimes it doesn't. When it doesn't work, the printed paper says:
PDF Error 10: PostScript Error: IOERROR
I'm trying to find a rule with the non-working PDF files, and one common think with them is that these PDF files has been edited by me (by the app) in other functionality of the webapp. The edition is made with the iText library
The code that I use to write to the PDF is the following one (The code has been modified to became simpler, so could have any sintax error):
public void signPDF(String path) {
// PDF
PdfReader reader = null;
PdfStamper stamper = null;
// stream
OutputStream os = null;
RandomAccessFile raf = null;
try {
// check for the first time
reader = new PdfReader(path);
raf = new RandomAccessFile(outPutPath, ACCESS_MODE_READ_AND_WRITE);
os = Channels.newOutputStream(raf.getChannel());
stamper = new PdfStamper(reader, os);
stamper.insertPage(reader.getNumberOfPages() + 1, reader.getPageSize(1));
// prepare canvas to sign
PdfContentByte canvas = stamper.getOverContent(reader.getNumberOfPages());
// sign document
String signature = "Signed";
int verticalPosition = calculateVerticalPosition();
Rectangle size = reader.getPageSize(reader.getNumberOfPages());
ColumnText.showTextAligned(canvas, Element.ALIGN_BOTTOM, new Phrase(signature), size.getWidth() - (size.getWidth() - 100), size.getHeight() - verticalPosition, 0);
} catch (DocumentException dEx) {
LOGGER.error(ERROR_UNEXPECTED, dEx);
throw new PDFException(Code.UNEXPECTED, ERROR_UNEXPECTED);
} catch (IOException ioEx) {
LOGGER.error(ERROR_IO, ioEx);
throw new PDFException(Code.READ_WRITE, MessageFormat.format(ERROR_IO, path));
} finally {
// close stamper
if (stamper != null) {
try {
stamper.close();
} catch (DocumentException dEx) {
LOGGER.error(ERROR_UNEXPECTED, dEx);
} catch (IOException ioEx) {
LOGGER.error(ERROR_IO, ioEx);
}
}
// close reader
if (reader != null) {
reader.close();
}
// close os
if (os != null) {
try {
os.close();
} catch (IOException ioEx) {
LOGGER.error(ERROR_IO, ioEx);
} finally {
IOUtils.closeQuietly(os);
}
}
// close raf
if (raf != null) {
try {
raf.close();
} catch (IOException ioEx) {
LOGGER.error(ERROR_IO, ioEx);
} finally {
IOUtils.closeQuietly(raf);
}
}
}