This seems to come up every once in a while, especially outside US and UK. People start looking at the PDF generating libraries, and find that when they use a non-latin character it stops to work. It needs some special care but it is really easy to get it working, I’m going to show how to do it, step by step, when using purepdf (a port of iText

Getting ready

You can find purepdf on google code. Once you download the zip you’ll find purePDF.swc and purePDFont.swc, for this example you’ll only need the first one so put it in your projects lib folder and you’re ready.
The next thing you need is a font to use, I’ll be using Ubuntu-L.ttf which can be downloaded from font.ubuntu.com, just drop it in the project. You can use any font, as long as it supports the charset you’ll be using. At this point my project looks something like this:

Flex stuff

To make the example work, we need some basic UI which, in the example, begins and ends with:

<s:TextInput id="TI" text="ążółć - works!" />
<s:Button click="createPDF()" label="Generate PDF" y="22" />

As you can see, the text has some polish characters (as Flex text is UTF encoded there’s no problem with putting what you need there). The other thing worth noting is that we’re going to be creating the PDF after a click, that’s important as we want to save the pdf to the user system and that’s only possible when the save function is being called after an user interaction.

What’s left to do now is embedding the font:

[Embed(source="Ubuntu-L.ttf", mimeType="application/octet-stream")]
private var embeddedFont: Class;

The mineType is required to stop Flex from handling the fonts as a font 1. That’s all the setup we need no we can get to the purepdf code.

Purepdf code

The code that’s needed to start using the font and generatign the pdf takes around 10 lines, I’ll paste it first and than add comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
protected function createPDF():void
{
   // Prepare the font
   FontsResourceFactory.getInstance().registerFont("newFont.otf", new embeddedFont());             
   var bf: BaseFont = BaseFont.createFont("newFont.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED );
   // parameters are:
   // family, font-size, style, color, BaseFont
   var font: org.purepdf.Font
       = new org.purepdf.Font( org.purepdf.Font.UNDEFINED, 24, -1, RGBColor.BLACK, bf );

   var buffer:ByteArray = new ByteArray();                             
   var writer:PdfWriter = PdfWriter.create( buffer, PageSize.A4);
   var doc:PdfDocument = writer.pdfDocument;           
   doc.open();
   // When creating a paragraph            
   // add the font parameter
   doc.add( new Paragraph(TI.text,font) );
   doc.close();

   // save the file
   var f: FileReference = new FileReference();
   f.save( buffer, "test.pdf" );
}

In lines 4-9 we’re using the embedded font file to create an instance of an org.purepdf.Font, that’s all you need to do to start using the font. The rest of the code is standard purepdf code.

11 – creates a buffer to hold the pdf file

12 – creates a PDFWriter linked with that buffer and sets the size of the pdf page

13 – gets the PDFDocument from the writer

14 – opens the document

15 – adds a paragraph to the open document. Here’s the important change if you already have some code written, you need to set the second parameter which is the font you’re wanting to use. If it supports the chars you’re using there will be no problem.

16 – closes the document

and 20-21 save the document to the user filesystem.

That’s it, the PDF you get in result looks something like this:

Hopefully this get’s people started. You can find many more examples and tutorials on the google code page for the project.

Download source code.

Notes:

  1. Without it you’ll get a message like “font transcoding requires you to specify ‘fontName’ and one of ‘source’, ‘systemFont’, or ‘sourceList’”