AR11980
AR11980

Reputation: 31

PDFBox Printing: Null Pointer Exception while printing using PDFBox

while printing a pdf file using following code in linux with CentOS:

    PrinterJob job = PrinterJob.getPrinterJob();
    PDDocument pdf = PDDocument.load(new ByteArrayInputStream(out.toByteArray()));
    job.setPageable(new PDPageable(pdf, job));
    job.print();

A null pointer exception is thrown :

2014-06-03 10:37:06 WARN  org.apache.pdfbox.util.PDFStreamEngine  PDFStreamEngine:542 - java.lang.NullPointerException
java.lang.NullPointerException
  at org.apache.pdfbox.pdmodel.font.PDCIDFont.getFontHeight(PDCIDFont.java:200)
  at org.apache.pdfbox.pdmodel.font.PDType0Font.getFontHeight(PDType0Font.java:119)
  at org.apache.pdfbox.util.PDFStreamEngine.processEncodedText(PDFStreamEngine.java:401)
  at org.apache.pdfbox.util.operator.ShowTextGlyph.process(ShowTextGlyph.java:62)
  at org.apache.pdfbox.util.PDFStreamEngine.processOperator(PDFStreamEngine.java:529)
  at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:258)
  at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:225)
  at org.apache.pdfbox.util.PDFStreamEngine.processStream(PDFStreamEngine.java:205)
  at org.apache.pdfbox.pdfviewer.PageDrawer.drawPage(PageDrawer.java:154)
  at org.apache.pdfbox.pdmodel.PDPageable.print(PDPageable.java:195)
  at sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:1936)
  at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1431)
  at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1247)

When we execute the same in Windows , its working fine. Any idea ? Please help

Upvotes: 0

Views: 2232

Answers (2)

user3815093
user3815093

Reputation: 9

For this always use pdfbox 1.8.6 and fop0.93

PDDocument doc = null;
        try
        {
            doc = new PDDocument();
            PDPage page = new PDPage();
            doc.addPage(page);
            PDPageContentStream contentStream = new PDPageContentStream(doc, page);

            PDFont pdfFont = PDType1Font.HELVETICA;
            float fontSize = 25;
            float leading = 1.5f * fontSize;

            PDRectangle mediabox = page.findMediaBox();
            float margin = 72;
            float width = mediabox.getWidth() - 2*margin;
            float startX = mediabox.getLowerLeftX() + margin;
            float startY = mediabox.getUpperRightY() - margin;

            String text = "Hello sir finally PDF is created : thanks"; 
            List<String> lines = new ArrayList<String>();
            int lastSpace = -1;
            while (text.length() > 0)
            {
                int spaceIndex = text.indexOf(' ', lastSpace + 1);
                if (spaceIndex < 0)
                {
                    lines.add(text);
                    text = "";
                }
                else
                {
                    String subString = text.substring(0, spaceIndex);
                    float size = fontSize * pdfFont.getStringWidth(subString) / 1000;
                    if (size > width)
                    {
                        if (lastSpace < 0) // So we have a word longer than the line... draw it anyways
                            lastSpace = spaceIndex;
                        subString = text.substring(0, lastSpace);
                        lines.add(subString);
                        text = text.substring(lastSpace).trim();
                        lastSpace = -1;
                    }
                    else
                    {
                        lastSpace = spaceIndex;
                    }
                }
            }

            contentStream.beginText();
            contentStream.setFont(pdfFont, fontSize);
            contentStream.moveTextPositionByAmount(startX, startY);            
            for (String line: lines)
            {
                contentStream.drawString(line);
                contentStream.moveTextPositionByAmount(0, -leading);
            }
            contentStream.endText(); 
            contentStream.close();

            doc.save("E:\\document.pdf");
        }catch (Exception exp){
            logger.error("[GetInformation] email id is " +exp);

        }
        finally
        {
            if (doc != null)
            {
                try{
                doc.close();
                }catch (Exception expe){
                    logger.error("[GetInformation] email id is " +expe);

                }

Upvotes: 1

Sangram Jadhav
Sangram Jadhav

Reputation: 2478

This may be because font is not available on linux/CentOs.
You can check the fonts in pdf using acrobat reader. From file menu-> properties -> Font tab
Install these fonts and try again.

Upvotes: 2

Related Questions