Tech Blog

Rich formatting for PDF's directly from Actionscript

Posted At : May 10, 2008 1:25 PM 4 Comments

I've blogged previously about the AlivePDF libraries which allow you to create PDF's directly from Actionscript.

I've been using them for a project and all was going well until we hit a limitation in the libraries. We can do rich formatting and we can do automatic line wrapping and pagination, but we can't do the both together.

Screenshot of generated pdf

This would have been an enormous problem if this was a piece of commercial software as we would be completely at the mercy of the creators. However, as it is Open Source we have access to the code and we can extend it as we see fit.

And that's exactly what we did. I've added a new version of the writeText() method called very imaginatively writeText2().

It takes the same parameters but instead of taking a standard string it will render a string that has html markup. I.e. you can put a <b> tag in and your text will be bold. Eg:

// This will write the following text with appropriate styling and word wrapping
pdf.writeText2 ( 15, 'A long <u>piece</u> of <b>text</b> in <center>1111 center 1111 2222 center 2222 3333 center 3333 <b>BOLD TEXT</b> 4444 center 4444</center>standard format that should wrap by the time I finish this. AAAAAAAAAAAAAAAABBBBBBBBBBBBCCCCCCCCCCCCCCAAA <i>Italic text</i>. <b>Some bold text</b>. And now for some <b><i>Bold and Italic Text</i></b>. And back to normal. Then a line break<br/> That\'s All.'   );

For the curious you can download a sample document generated by it.

It currently supports the following tags:

  • <b> Make it bold
  • <u> Underline it
  • <i> Make it italic
  • <center> Center it
  • <br /> Add a line break

The code still needs a some cleanup and more testing but it seems to be reliable now.

The two files that were changed are available for download. Or alternatively you can download the full version with changes included. Give it a try and let me know what you think.

Cheers, Mark Lynch

4 Comments

Jean C. Kresko 5/28/08 10:15 PM

Hi...
Congratulation for your script!
I need justify the writeText. All my text will justify. Can you help me?
Jean

Catalin 7/14/08 4:34 PM

What do I have to do with the changed files in my Flex app in order to be able to use the writeText2() function???

I tried adding the files to org>alivepdf>pdf , but Flex did not recognize the new function. The two are actionscript files while everything else in org>alivepdf is HTML extension.

I'd really appreciate your help with this.

Thanks.

Catalin.

Chip Caramel 9/10/08 10:57 AM

Dude. If I could kiss you, I would!

Thanks for helping out with the alivePDF community!

tm 1/14/09 12:42 AM

Thanks a lot!

Please note that if the second parameter start right with <center> like this:
pdf.writeText2 ( 16, " <center>center it</center>");
it causes "null object refference" error.