3

The link says that it is not "easily" possible to configure fonts from java code. How do I achieve this? I'm having problems rendering certain HTMLs 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 PDF generated is damaged as a result.

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'm using an XSLT provided by Antennahouse to convert HTML tags to FO tags.

stackMan10
  • 732
  • 6
  • 25
  • Could you post your code? – RevanProdigalKnight May 08 '15 at 12:10
  • I have updated it... – stackMan10 May 08 '15 at 12:28
  • "is damaged" is not a real description of a problem, what's wrong with the generated PDF that makes you conclude it's damaged? – Mike 'Pomax' Kamermans May 08 '15 at 17:35
  • Hmm... yes.. that's the most apt description I could come up with because the file doesn't open giving a message saying it is damaged... – stackMan10 May 08 '15 at 18:34
  • 1
    Maybe [this answer of mine](http://stackoverflow.com/a/28251945/4453460) could help; note that from the posted error messages the problem is not from the French accented characters, but from some Symbol and Wingdings glyphs (maybe used sa bullet points?) – lfurini May 11 '15 at 20:04
  • If the resulting PDF is **damaged** (= it cannot be opened) there must be something else going on besides font configuration; the font subsitution warnings you posted just refer to the missing registration of the bold variants for Symbol and ZapfDingbats, resulting in the "normal" variant being used instead. – lfurini May 17 '15 at 11:54
  • @Ifurini , I'm using the directory tag for bulk mapping. For windows I can use the system/fonts directory. Would you have any idea how I can make this universal so it works on linux? – stackMan10 Jul 01 '15 at 13:47
  • 1
    You can use the [``](http://xmlgraphics.apache.org/fop/trunk/fonts.html#autodetect) element in the configuration file, which tells FOP to look in the operating system's default font folders. – lfurini Jul 01 '15 at 15:37

1 Answers1

0

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 it may help you.

Avyaan
  • 1,285
  • 6
  • 20
  • 47