使用CentOS在linux下使用以下代码打印pdf文件:
PrinterJob job = PrinterJob.getPrinterJob();
PDDocument pdf = PDDocument.load(new ByteArrayInputStream(out.toByteArray()));
job.setPageable(new PDPageable(pdf, job));
job.print();
抛出空指针异常:
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)
当我们在Windows中执行相同操作时,它的工作正常。任何的想法 ?请帮忙
答案 0 :(得分:2)
这可能是因为linux / CentOs上没有字体。
您可以使用acrobat reader检查pdf中的字体。从文件菜单 - >属性 - >字体选项卡
安装这些字体,然后重试。
答案 1 :(得分:1)
为此,请始终使用pdfbox 1.8.6和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);
}