Wednesday, December 07, 2011

Arial Font from JasperReports for free and legally

If you are planning to ship documents to your clients and you need to use one of the commercial most used fonts like Arial you are in your way to pay a good chunk of money. There is not even a clear price on what they might cost because basically it depends on your application (read the license goes with how much money you are making with your product).

Thanks to the information I found in this post it took me little time to legally use a close to the proprietary Arial font in our application. If you plan to distribute or license your application you will need to buy Arial font or find a different project with a more commercial friendly license (like BSD) as the license for Liberation fonts is GPL. must then

  1. Download and extract the Red Hat Open Source GPL licensed Liberation Fonts
  2. From iReport Preferences on a Mac or Options for the rest of the mortals navigate through "iReport | Fonts | Install Font", then import "LiberationSans-Regular.ttf" from the exploded directory created as part of the above step. Click next and pick the bold, Italic and BoldItalic versions for LiberationSans. LiberationSans is the closest font to Arial. The rest of the Liberation Fonts are closer to Times New Roman and Courier New fonts
  3. Pick a good family name that will later use in your style fontName property, for example "LiberationSans". Check the "Embed this font in the PDF document"
  4. Include your font in a hello world jrxml like I show below and run it from iReport. Play with the properties and you should see bold and italic rendered as well (I am in this example striking through and underlining as well).
    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="" xmlns:xsi="" xsi:schemaLocation="" name="hello" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
     <property name="ireport.zoom" value="1.0"/>
     <property name="ireport.x" value="0"/>
     <property name="ireport.y" value="0"/>
     <style name="Default" isDefault="true" fontName="LiberationSans" fontSize="20" isBold="true" isItalic="true" isUnderline="true" isStrikeThrough="true"/>
      <band splitType="Stretch"/>
      <band height="79" splitType="Stretch">
        <reportElement x="12" y="10" width="531" height="57"/>
        <text><![CDATA[Hello JasperReports!!!]]></text>
      <band height="35" splitType="Stretch"/>
      <band height="61" splitType="Stretch"/>
      <band height="125" splitType="Stretch"/>
      <band height="45" splitType="Stretch"/>
      <band height="54" splitType="Stretch"/>
      <band height="42" splitType="Stretch"/>
  5. Now you should see from iReport your font correctly embedding in the resulting PDF
  6. From iReport navigate to the Fonts settings as I explained before and then pick "LiberationSans" and export it as liberation-sans.jar
  7. Include the jar in your application classpath
  8. Now your app is serving close to Arial font styled content

No comments: