title: Font not load/not found Apache fop 2.3
body: <p>I have updated <a href="https://xmlgraphics.apache.org/fop/" rel="nofollow noreferrer">apache fop</a> version from <a href="https://xmlgraphics.apache.org/fop/1.1/" rel="nofollow noreferrer">1.1</a> to <a href="https://xmlgraphics.apache.org/fop/2.3/" rel="nofollow noreferrer">2.3</a>, now I am getting custom font related issue, I am trying to load font from font directory which is deployed as part of war file, it war working fine with version 1.1.    </p>
  
  <p><strong>Getting Error</strong> </p>
  
  <pre><code>SEVERE: org.xml.sax.SAXParseException; systemId: jndi:/localhost/fileprocessor/stylesheets/mainpage_invoice.xsl; lineNumber: 104; columnNumber: 53; java.lang.RuntimeException: Failed to read font file CALIBRI.TTF
  Apr 08, 2019 2:57:25 PM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [fileprocessor] in context with path [/fileprocessor] threw exception [javax.xml.transform.TransformerException: java.lang.RuntimeException: Failed to read font file CALIBRI.TTF] with root cause
  java.lang.NullPointerException
  </code></pre>
  
  <p><strong>Configuration file:</strong></p>
  
  <p></p>
  
  <pre><code>&lt;!-- Strict user configuration --&gt;
  &lt;strict-configuration&gt;true&lt;/strict-configuration&gt;
  
  &lt;!-- Strict FO validation --&gt;
  &lt;strict-validation&gt;true&lt;/strict-validation&gt;
  
  &lt;!-- Base URL for resolving relative URLs --&gt;
  &lt;base&gt;servlet-context:/&lt;/base&gt;
  
  &lt;!-- Font Base URL for resolving relative font URLs --&gt;
  &lt;font-base&gt;servlet-context:/fonts/&lt;/font-base&gt;
  
  &lt;!-- Source resolution in dpi (dots/pixels per inch) for determining the size of pixels in SVG and bitmap images, default: 72dpi --&gt;
  &lt;source-resolution&gt;300&lt;/source-resolution&gt;
  &lt;!-- Target resolution in dpi (dots/pixels per inch) for specifying the target resolution for generated bitmaps, default: 72dpi --&gt;
  &lt;target-resolution&gt;300&lt;/target-resolution&gt;
  
  &lt;renderers&gt;
      &lt;renderer mime="application/pdf"&gt;
          &lt;fonts&gt;             
              &lt;font embed-url="glypha_lt_35_thin.ttf"&gt;            &lt;font-triplet name="glypha" style="normal" weight="100"/&gt;&lt;/font&gt;
              &lt;font embed-url="glypha_lt_35_thin_oblique.ttf"&gt;    &lt;font-triplet name="glypha" style="italic" weight="100"/&gt;&lt;/font&gt;
              &lt;font embed-url="glypha_lt_45_light.ttf"&gt;           &lt;font-triplet name="glypha" style="normal" weight="300"/&gt;&lt;/font&gt;
              &lt;font embed-url="glypha_lt_45_light_oblique.ttf"&gt;   &lt;font-triplet name="glypha" style="italic" weight="300"/&gt;&lt;/font&gt;
              &lt;font embed-url="glypha_lt_55_roman.ttf"&gt;           &lt;font-triplet name="glypha" style="normal" weight="400"/&gt;&lt;/font&gt;
              &lt;font embed-url="glypha_lt_55_oblique.ttf"&gt;         &lt;font-triplet name="glypha" style="italic" weight="400"/&gt;&lt;/font&gt;
              &lt;font embed-url="glypha_lt_65_bold.ttf"&gt;            &lt;font-triplet name="glypha" style="normal" weight="700"/&gt;&lt;/font&gt;
              &lt;font embed-url="glypha_lt_65_bold_oblique.ttf"&gt;    &lt;font-triplet name="glypha" style="italic" weight="700"/&gt;&lt;/font&gt;
              &lt;font embed-url="glypha_lt_75_black.ttf"&gt;           &lt;font-triplet name="glypha" style="normal" weight="900"/&gt;&lt;/font&gt;
              &lt;font embed-url="glypha_lt_75_black_oblique.ttf"&gt;   &lt;font-triplet name="glypha" style="italic" weight="900"/&gt;&lt;/font&gt;
  
              &lt;font embed-url="courier.ttf"&gt;                      &lt;font-triplet name="courier" style="normal" weight="400"/&gt;&lt;/font&gt;
              &lt;font embed-url="courier_bold.ttf"&gt;                 &lt;font-triplet name="courier" style="normal" weight="700"/&gt;&lt;/font&gt;
              &lt;font embed-url="courier_bold_italic.ttf"&gt;          &lt;font-triplet name="courier" style="italic" weight="700"/&gt;&lt;/font&gt;
              &lt;font embed-url="courier_italic.ttf"&gt;               &lt;font-triplet name="courier" style="italic" weight="400"/&gt;&lt;/font&gt;
  
  
              &lt;font embed-url="CALIBRI.TTF"&gt;                      &lt;font-triplet name="calibri" style="normal" weight="400"/&gt;&lt;/font&gt;
              &lt;font embed-url="CALIBRIB.TTF"&gt;                     &lt;font-triplet name="calibri" style="normal" weight="700"/&gt;&lt;/font&gt;
              &lt;font embed-url="CALIBRII.TTF"&gt;                     &lt;font-triplet name="calibri" style="italic" weight="700"/&gt;&lt;/font&gt;
              &lt;font embed-url="CALIBRIZ.TTF"&gt;                     &lt;font-triplet name="calibri" style="italic" weight="400"/&gt;&lt;/font&gt;
  
  
          &lt;/fonts&gt;
      &lt;/renderer&gt;
  &lt;/renderers&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>fileprocessor class:</strong></p>
  
  <pre><code>public class fileprocessor extends HttpServlet {
      private static final long serialVersionUID = 1L;
      protected URIResolver uriResolver;
      private FopFactory fopFactory;
      private TransformerFactory factory;
      private FopFactoryBuilder fopFactoryBuilder;
      private ResourceResolver resolver;
  
      public static InputSource sourceToInputSource(StreamSource ss) throws ServletException {
          InputSource isource = new InputSource(ss.getSystemId());
          isource.setByteStream(ss.getInputStream());
          isource.setCharacterStream(ss.getReader());
          isource.setPublicId(ss.getPublicId());
          return isource;
      }
      class MyErrorListener implements ErrorListener{
          @Override
          public void error(TransformerException arg0)
                  throws TransformerException {
              throw arg0;
          }
          @Override
          public void fatalError(TransformerException arg0)
                  throws TransformerException {
              throw arg0;
          }
          @Override
          public void warning(TransformerException arg0)
                  throws TransformerException {
              throw arg0;
          }
      };
  
      @Override
      public void init() throws ServletException {
          this.uriResolver = new ServletContextURIResolver(getServletContext());
  
          factory = TransformerFactory.newInstance();
          factory.setURIResolver(this.uriResolver);
          factory.setErrorListener(new MyErrorListener());
  
          this.resolver = new ResourceResolver() {
  
              @Override
              public Resource getResource(URI uri) throws IOException {               
                  return new Resource(getServletContext().getResourceAsStream(uri.toASCIIString()));
              }
  
              @Override
              public OutputStream getOutputStream(URI uri) throws IOException {
                  URL url = getServletContext().getResource(uri.toASCIIString());
                  return url.openConnection().getOutputStream();
              }
          };
  
  
          try {
              Source s = this.uriResolver.resolve("servlet-context:/configuration.xml", null);
  
              DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
              Configuration cfg = cfgBuilder.build(sourceToInputSource((StreamSource)s));
  
  
  
              fopFactoryBuilder = new FopFactoryBuilder(new File(".").toURI(), resolver).setConfiguration(cfg);
  
              this.fopFactory = fopFactoryBuilder.build();
  
  
  
          } catch (SAXException e) {
              throw new ServletException("error",e);
          } catch (IOException e) {
              throw new ServletException("error",e);
          } catch (ConfigurationException e) {
              throw new ServletException("error",e);
          } catch (TransformerException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
  
      }
  
      public fileprocessor() {
      }
  
      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          try {
              String uri=request.getRequestURI().substring(request.getContextPath().length());
  
  
               if (uri.equals("/invoice/pdf")) {
                  // base stylesheet mainpage_invoice.xsl where i am using font
                  Source xslt = uriResolver.resolve("servlet-context:/stylesheets/mainpage_invoice.xsl", null);
                  Transformer transformer = factory.newTransformer(xslt);
                  transformer.setURIResolver(this.uriResolver);               
                  transformer.setParameter("draft", false);
                  transformer.setErrorListener(new MyErrorListener());
  
                  String xmlInput = CharStreams.toString(request.getReader());
  
                  Source src = new StreamSource(new StringReader(xmlInput));
                  response.setContentType("application/pdf");
                  Fop fop = this.fopFactory.newFop(MimeConstants.MIME_PDF, response.getOutputStream());
                  Result res = new SAXResult(fop.getDefaultHandler());
                  transformer.transform(src, res);            
              } else {
                  response.sendError(404);
              }
          } catch (Exception e) {             
              throw new ServletException(e);
          }
      }
  
  }
  </code></pre>