cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to include Arial font in FOPTransformer

yogesh
Champ in-the-making
Champ in-the-making
I am working on an application which has some forms and then using FOP it creates pdf files. PDF files that are getting generated are in Time New Roman but I want them in Arial font. I added arial.ttf in projectName->fonts->arial.ttf as described here:
http://www.publicstaticfinal.de/2011/01/26/fop-embedding-fonts-from-classpath/

This ttf and code has to go into a amp file but right now I havent added it as jar file for arial.ttf because I am not sure how to make a jar file with just .ttf file,  Here is what I did to accomplish this but pdf are still in times new roman:

<fop version="0.94">

  <!– Base URL for resolving relative URLs –>
  <base>./</base>

  <!– Font Base URL for resolving relative font URLs –>
  <font-base>./</font-base>
 
  <renderer mime="application/postscript">
     <fonts>
            <font kerning="yes" embed-url="/fonts/arial.ttf" encoding-mode="auto">
                <font-triplet name="Arial" style="normal" weight="normal" />
        </font>
     </fonts>
   </renderer>
</fop>

Created a class file as follows:

//ClasspathUriResolver
import java.io.InputStream;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;

public class ClasspathUriResolver implements URIResolver
{

  public Source resolve(String href, String base) throws TransformerException
  {
    Source source = null;
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(href);
    if (inputStream != null)
    {
      source = new StreamSource(inputStream);
    }
    return source;
  }
}

And then in FOPTransformer i used it like this:

FopFactory fopFactory = FopFactory.newInstance();
         FOURIResolver uriResolver = (FOURIResolver)fopFactory.getURIResolver();
         uriResolver.setCustomURIResolver(new ClasspathUriResolver());
         FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
1 REPLY 1

billerby
Champ on-the-rise
Champ on-the-rise
I succeeded in doing this a while ago. Looking through your fopcfg.xml what differs from mine is that you seem to specify postscript as output format, my renderer element look like this (notice the mime-type application/pdf):


    <renderer mime="application/pdf">
      <filterList>
        <!– provides compression using zlib flate (default is on) –>
        <value>flate</value>
 
        <!– encodes binary data into printable ascii characters (default off)
             This provides about a 4:5 expansion of data size –>
        <!– <value>ascii-85</value> –>
 
        <!– encodes binary data with hex representation (default off)
             This filter is not recommended as it doubles the data size –>
        <!– <value>ascii-hex</value> –>
      </filterList>

      <fonts>
       <font kerning="yes"
               embed-url="fonts/helv_lt_77_bold_cond.ttf">
          <font-triplet name="HelveticaNeueLT57Cn" style="normal" weight="bold"/>
         </font>
         <font kerning="yes"
               embed-url="fonts/helv_lt_47_light_cond.ttf">
          <font-triplet name="HelveticaNeueLT47LightCn" style="normal" weight="200"/>
        </font>
   
      </fonts>

      <!– This option lets you specify additional options on an XML handler –>
      <!–xml-handler namespace="http://www.w3.org/2000/svg">
        <stroke-text>false</stroke-text>
      </xml-handler–>

    </renderer>