How do I configure fonts from Java code in Apache FOP?

The link says it can't easily customize fonts from Java code. How can I achieve this? I am having problems displaying some HTML pages from international languages ​​like French and Japanese.

WARNING: Font "Symbol,normal,700" not found. Substituting with "Symbol,normal,400".
May 08, 2015 4:45:39 PM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Font "ZapfDingbats,normal,700" not found. Substituting with "ZapfDingbats,normal,400". 

      

The result is a PDF file.

Update

My Html contains French words like "Modifié Créée le Propriétaire"

File file = new File("C:\\Users\\me\\Desktop\\Test.html");


fopFactory = FopFactory.newInstance();
foUserAgent = fopFactory.newFOUserAgent();


String fileName = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("\\")+1,file.getAbsolutePath().lastIndexOf("."));
        String workspacePath = file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("\\"));
        File xsltfile = new File("xhtml2fo.xsl");
        StreamSource source = null;
        source = new StreamSource(file);
        StreamSource transformSource = new StreamSource(xsltfile);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();


        Transformer xslfoTransformer = null;
        TransformerFactory fac = TransformerFactory.newInstance();
        xslfoTransformer = fac.newTransformer(transformSource);
        xslfoTransformer.setErrorListener(this);

        Fop fop;
        fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outStream);
        // Resulting SAX events (the generated FO)
        Result res = new SAXResult(fop.getDefaultHandler());
        xslfoTransformer.transform(source, res);

        output = new File(workspacePath + File.separator + fileName + ".pdf");
        OutputStream out = new java.io.FileOutputStream(output);
        out = new java.io.BufferedOutputStream(out);
        FileOutputStream str = new FileOutputStream(output);
        str.write(outStream.toByteArray());
        str.close();

      

I am using XSLT provided by Antennahouse to transform HTML tags to FO tags.

+3


source to share


1 answer


sample code.

/** The Constant FOP_CONFIG. */
    private static final String FOP_CONFIG = "pdf.fop.cfg.xml";

fopFactory = FopFactory.newInstance();
                    // for image base URL
                    String serverPath = request.getSession().getServletContext().getRealPath("/");
                    //disable strict validatetion
                    fopFactory.setStrictValidation(false);
                    fopFactory.setBaseURL(serverPath);
                    // for fonts base URL
                    fopFactory.getFontManager().setFontBaseURL(serverPath);
                    // read custom font setting
                    StreamSource configSrc = new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream(PDFComponentFactory.FOP_CONFIG));
                    DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
                    Configuration cfg = cfgBuilder.build(configSrc.getInputStream());
                    fopFactory.setUserConfig(cfg);

      



Hope this helps you.

0


source







All Articles