AnsweredAssumed Answered

Unable to include Arial font in FOPTransformer

Question asked by yogesh on Apr 20, 2012
Latest reply on Apr 25, 2012 by billerby
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();

Outcomes