简体   繁体   English

PdfClown nullpointer关于字体访问

[英]PdfClown nullpointer on Font access

I have something strange going on with text writing using PdfClown 0.1.2. 使用PdfClown 0.1.2编写文本时,发生了一些奇怪的事情。

PrimitiveComposer composer = new PrimitiveComposer(page);
    BlockComposer blockComposer = new BlockComposer(composer);
    addHeader(document, composer, blockComposer);
    addOfferData(document, offer, blockComposer, composer);
    composer.flush();

private void addHeader(Document document, PrimitiveComposer composer, BlockComposer blockComposer) {
    Rectangle2D frame = new Rectangle2D.Double(36, 0, 842, 36);
    blockComposer.begin(frame, XAlignmentEnum.Left, YAlignmentEnum.Middle);
    composer.setFont(getSimpleFont(document), 12);
    blockComposer.showText(getHeader());
    blockComposer.end();
}

private void addOfferData(Document document, Offer offer, BlockComposer blockComposer, PrimitiveComposer composer) {
    blockComposer.begin(new Rectangle2D.Double(456, 156, 340, 250), XAlignmentEnum.Left, YAlignmentEnum.Top);
    composer.setFont(getSimpleFont(document), 12);

        blockComposer.showText("Text");
        blockComposer.showBreak();

    blockComposer.end();
}

    private StandardType1Font getSimpleFont(Document document) {
    return new StandardType1Font(document, StandardType1Font.FamilyEnum.Times, false, false);
}

In JUnit test everything is ok, but in production mode it is failing in the second method, on showText. 在JUnit测试中,一切正常,但在生产模式下,第二种方法showText失败。

java.lang.NullPointerException
    at org.pdfclown.documents.contents.fonts.Font.encode(Font.java:423)
    at org.pdfclown.documents.contents.composition.PrimitiveComposer.showText(PrimitiveComposer.java:1058)
    at org.pdfclown.documents.contents.composition.PrimitiveComposer.showText(PrimitiveComposer.java:960)
    at org.pdfclown.documents.contents.composition.BlockComposer.showText(BlockComposer.java:553)
    at org.pdfclown.documents.contents.composition.BlockComposer.showText(BlockComposer.java:463)
    at com.example.service.PdfGenerationService.addOfferData(PdfGenerationService.java:121)

Does the font needs to be handled somehow differently, or what could cause this problem? 字体是否需要以不同的方式处理,还是可能导致此问题? In the first method text is being rendered what is the strangest here. 在第一种方法中,呈现文本的最奇怪的地方是这里。

The NullPointerException occurs at Font.encode(Font.java:423) . NullPointerException发生在Font.encode(Font.java:423) This method is: 该方法是:

public final byte[] encode(
    String text
    )
{
    ByteArrayOutputStream encodedStream = new ByteArrayOutputStream();
    try
    {
        for(int index = 0, length = text.length(); index < length; index++)
        {
            int textCode = text.charAt(index);
            byte[] charCode = codes.getKey(textCode).data;
            encodedStream.write(charCode);
            usedCodes.add(textCode);
        }
        encodedStream.close();
    }
    catch(IOException e)
    {throw new RuntimeException(e);}

    return encodedStream.toByteArray();
}

( Font.java revision 85, lines 413..433) Font.java修订版85,第413..433行)

Line 423 is 423行是

            byte[] charCode = codes.getKey(textCode).data;

Thus, either codes is null or codes.getKey(textCode) is null. 因此, codesnullcodes.getKey(textCode)null.

The former ( codes being null ) is pretty implausible for a StandardType1Font . 对于StandardType1Font ,前者( codesnull )是非常不现实的。

The latter ( codes.getKey(textCode) being null ) would mean that the text to draw contains characters not present in the encoding at hand. 后者( codes.getKey(textCode)null )意味着要绘制的文本包含手头编码中不存在的字符。

Assuming that in production you do not call 假设在生产中您不打电话

blockComposer.showText("Text");

but instead use some different production text, you should check that text for special characters (probably not present in standard PDF encodings). 但应使用一些不同的生产文本,而应检查该文本是否包含特殊字符(可能在标准PDF编码中不存在)。

If that assumption is wrong and you indeed in production only show "Text" , more analysis of the differences production environment <-> development environment is required. 如果该假设是错误的,并且您实际上在生产中仅显示"Text" ,则需要对差异进行更多分析,生产环境<->开发环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM