Sategroup
Sategroup

Reputation: 955

Apache FOP - Not Accepting Custom Font

While trying to use Apache FOP to use custom fonts (Google Lato Font), I am getting below error. I have converted ttf font to xml as per documentation & kept both ttf & xml files in same directory resource

WARNING: Font "Lato,normal,700" not found. Substituting with "any,normal,700"
WARNING: Font "Lato,normal,400" not found. Substituting with "any,normal,400".

Configuration file:

<?xml version="1.0"?>
<fop version="1.0">
    <base>.</base>
    <source-resolution>72</source-resolution>
    <target-resolution>72</target-resolution>
    <default-page-settings height="11.00in" width="8.50in"/>
    <renderers>
        <renderer mime="application/pdf">
            <filterList>
                <!-- provides compression using zlib flate (default is on) -->
                <value>flate</value>
            </filterList>
            <fonts>
                <font metrics-url="Lato-Regular.xml" kerning="yes" embed-url="Lato-Regular.ttf">
                    <font-triplet name="Lato" style="normal" weight="400"/>
                </font>
                <font metrics-url="Lato-Bold.xml" kerning="yes" embed-url="Lato-Bold.ttf">
                    <font-triplet name="Lato" style="normal" weight="700"/>
                </font>
            </fonts>
        </renderer>
    </renderers>
</fop>

Execution code:

public static void main(String[] args) throws SAXException, IOException, TransformerException, URISyntaxException {
        File fopConf = new File("\\fop.xconf");
        FopFactory fopFactory = FopFactory.newInstance(fopConf);

        OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("D:/Hello.pdf")));

        try {
            // Step 3: Construct fop with desired output format
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

            TransformerFactory factory = TransformerFactory.newInstance();
            Source xslt = new StreamSource(new File(ClassLoader.getSystemResource("resources/foo.xsl").toURI()));
            Transformer transformer = factory.newTransformer(xslt);

            // Resulting SAX events (the generated FO) must be piped through to FOP
            Result res = new SAXResult(fop.getDefaultHandler());

            Source src = new StreamSource(new File(ClassLoader.getSystemResource("resources/name.xml").toURI()));
            // Step 6: Start XSLT transformation and FOP processing

            transformer.transform(src, res);

        } finally {
            //Clean-up
            out.close();
        }
    }

Any suggestions? I have wasted entire day looking on Google, but unable to find a relevant solution. I have seen stackoverflow posts mentioning about using realtive/absolute path, sub-fonts, etc. but none of them seems to work

Upvotes: 2

Views: 1901

Answers (1)

Sategroup
Sategroup

Reputation: 955

Followed @Eduard approach & executed code using Standalone FOP (Command Line) & figured out that my execution code was not reading conf file.

  • Changed the execution code to read it as a file, instead & it worked. Above execution code is edited & works fine.

  • XML conversion is not required in FOP 2.2

Upvotes: 2

Related Questions