Question

I have something strange going on with text writing using 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.

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.

Was it helpful?

Solution

The NullPointerException occurs at 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)

Line 423 is

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

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

The former (codes being null) is pretty implausible for a StandardType1Font.

The latter (codes.getKey(textCode) being null) would mean that the text to draw contains characters not present in the encoding at hand.

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).

If that assumption is wrong and you indeed in production only show "Text", more analysis of the differences production environment <-> development environment is required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top