Cannot find Arial.ttf font with apache fop 1.0

I use:

  • Apache fop 1.0
  • Java

I need to handle special characters that are unicode higher than u0100. The target font I should use is Arial. Since I cannot expect Arial to be present on the target platform (like Linux), I embedded Arial.ttf in my jar. My actual configuration file is as follows:

<fop ...> <!-- Base URL for resolving relative URLs --> <base>./</base> <!-- Font Base URL for resolving relative font URLs --> <font-base>./</font-base> <renderers> <renderer mime="application/pdf"> <fonts> <font kerning="yes" embed-url="arial.ttf" encoding-mode="auto"> <font-triplet name="Arial" style="normal" weight="normal"/> <font-triplet name="ArialMT" style="normal" weight="normal"/> </font> <!--<directory>C:/Windows/Fonts</directory>--> </fonts> <auto-detect/> </renderer> ... </fop> 

By doing this this way, I get an error loading my configuration file in fop:

 org.apache.fop.apps.FOPException: Failed to resolve font with embed-url 'arial.ttf' 

The only way I managed to get it to work so far is to hardcode the path to the folder in the configuration file:

  <font kerning="yes" embed-url="C:/myapp/arial.ttf" encoding-mode="auto"> <font-triplet name="Arial" style="normal" weight="normal"/> <font-triplet name="ArialMT" style="normal" weight="normal"/> </font> 

But obviously this cannot be a solution!

Using the "directory" tag, which works on Windows, just because you have Arial. But, as mentioned above, it should also work on Linux, Mac, etc.

The auto-detect tag did not work (not even on Windows) - and is not a solution, since I cannot expect the target platform with Arial installed.

Any suggestions to solve this problem?

+4
source share
4 answers

I finally found the answer. See: http://www.publicstaticfinal.de/2011/01/26/fop-embedding-fonts-from-classpath/

The solution is to add your own URIResolver in fopFactory.

+4
source

This solution does not work for me. This is because ClassLoader.getSystemResourceAsStream(href); returns null .

I found a different solution , but it has a different problem - if you have other resources that you add to the FOP with an absolute system path (for example, images selected by the user) than your custom URIResolver used for or for them to be fixed incorrectly .

If you want to fix these examples, just connect them and you will get a working solution as follows:

 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 { @Override public Source resolve(String href, String base) throws TransformerException { Source source = null; InputStream inputStream = getClass().getResourceAsStream(href); if (inputStream != null) { source = new StreamSource(inputStream); } return source; } } 

Using

 [...] FopFactory fopFactory = FopFactory.newInstance(); FOURIResolver uriResolver = (FOURIResolver) fopFactory.getURIResolver(); uriResolver.setCustomURIResolver(new ClasspathUriResolver()); [...] 
+1
source

You have a <auto-detect /> outside <fonts> tag. To solve the font problem, put the <auto-detect /> inside the <fonts> tag. We do not use additional <font> configurations, but just use system fonts detected with <auto-detect />

0
source

Not sure if this is your problem, but I found out that since some versions of the relative paths do not work in the Fop configuration file. This worked for me if I either provided an absolute path (starting with "/") or a path prefix of "file:", for example, "file :."

0
source

Source: https://habr.com/ru/post/1401791/


All Articles