Robert Bak – Notes

Some programming tips – updated sometimes

Generating PDF-s on the client side (including non-latin chars)

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="azólc - 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'”

, , , ,

7 thoughts on “Generating PDF-s on the client side (including non-latin chars)

  • polish programmer says:

    Thanks! It works great!

  • McCzaju says:

    Very nice, Your article is very informative and a useful. Greetings.

  • Peter_D says:

    Hallo, please tell me how to make this example work in flash IDE?
    Thanks for help

  • admin says:

    Hey Peter, sorry for the late reply, this isn’t really Flex related so should work in pure Flash as is.

  • Jason says:

    I’m trying to work out if its possible to use the system fonts rather than embed the font files in with the application.. any ideas.?

    I’ve tried
    [Embed(systemFont=’Arial Unicode MS’,
    fontName=’myFont’,
    mimeType=’application/x-font’,
    embedAsCFF=’false’
    )]
    private var myFont:Class;

    but of course the embedded object is not a byteArray so I cannot register it with purePDF!

  • admin says:

    I can’t test this now, but the mimeType shouldn’t be “application/x-font” but “application/octet-stream” – not sure if that will work with systemFonts, but if you could try it out that would be great! (I haven’t used this in a long time so it’s a bit of a guess)

  • Jason says:

    … tried it .. but its a no go.

    When you switch the mimeType to application/octet-stream the transcoder is no longer looking for the systemFont attribute.

    So close… Flex lets me look up the fonts on the users system ( and I can use in flex components ) and PurePDF lets me register and use embedded font files for PDF creation…

    … but I can’t find anyway of letting the user use one of their system fonts for PDF creation – which would be really useful !

Leave a Reply

Your email address will not be published. Required fields are marked *