从Java打印会打印出包含错误消息的空白纸张

时间:2015-12-23 17:10:34

标签: java pdf printing itext

作为大型webapp的一部分,我有一个打印PDF文件的打印功能:

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");
    }
}

有时,它有效,有时则不然。当它不起作用时,印刷的纸张说:

  

PDF错误10:PostScript错误:IOERROR

我正在尝试使用不起作用的PDF文件找到规则,并且他们常常想到的是这些PDF文件已由我(由应用程序)在webapp的其他功能中编辑。该版本使用iText

制作

我用来写PDF的代码如下(代码已被修改为变得更简单,因此可能有任何sintax错误):

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);
            }
        }

    }

0 个答案:

没有答案