Flying Saucer font for unicode characters

11,462

Solution 1

For some reason it started working with following css and .ttf file, which was generated by face-kit-generator:

@font-face {
    src: url('arialuni.ttf');
    -fs-pdf-font-embed: embed;
    -fs-pdf-font-encoding: Identity-H;
}

body {
    font-family: Arial Unicode MS, Lucida Sans Unicode, Arial, verdana, arial, helvetica, sans-serif;
    font-size: 8.8pt;
}

Weird thing is that if I put font into some folder, let say "fonts", it will find the font but characters won't be rendered.

Solution 2

I managed to "enable" unicode characters (cyrillic or czech) within java code and furthermore providing a true type font in my resources (CALIBRI.TTF).

import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.pdf.BaseFont; 

...
    ITextRenderer renderer = new ITextRenderer();
    URL fontResourceURL = getClass().getResource("fonts/CALIBRI.TTF");
    //System.out.println("font-path:"+fontResourceURL.getPath());

    /* HERE comes my solution: */
    renderer.getFontResolver().addFont(fontResourceURL.getPath(), 
                BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

    renderer.setDocument(doc, null);
    renderer.layout();
    baos = new ByteArrayOutputStream();
    renderer.createPDF(baos);
    baos.flush();
    result = baos.toByteArray();
...

Finally I added the font-family 'Calibri' to the css section of my document:

...
<style type="text/css">
    span { font-size: 11pt; font-family: Calibri; }
...
Share:
11,462
Alex K.
Author by

Alex K.

Updated on June 09, 2022

Comments

  • Alex K.
    Alex K. about 2 years

    I am generating PDF using Grails export plugin (basically, Flying Saucer). My GSP page is an UTF-8 page (or at least properties are showing that it is UTF-8, also in the beginning of the GSP page there is a <?xml version="1.0" encoding="UTF-8"?> directive). At first generated PDF properly contained umlaut characters "äöüõ", but Cyrillic characters were missing from PDF (not rendered at all). Then I've changed my css file as described in documentation by adding following:

    @font-face {
        src: url(ARIALUNI.TTF);
        -fs-pdf-font-embed: embed;
        -fs-pdf-font-encoding: UTF-8;
    }
    body {
          font-family: "Arial Unicode MS", Arial, sans-serif;
    }
    

    ArialUni.ttf is also deployed to the server. But now I am getting both umlaut characters and Cyrillic characters rendered as boxes. If I am changing -fs-pdf-encoding property value to Identity-H then umlaut characters are rendered properly, but Cyrillic characters are rendered as question marks.

    Any ideas of what font can be used to properly render both umlaut and Cyrillic characters? Or may be my CSS is somehow wrong? Any hints would be much appreciated.

    Upd 1: I have also tried following css (which was generated by http://fontface.codeandmore.com/):

    @font-face {
        font-family: 'ArialUnicodeMS';
        src: url('arialuni.ttf');
        src: url('arialuni.eot?#iefix') format('embedded-opentype'),
            url('arialuni.woff') format('woff'),
            url('arialuni.ttf') format('truetype'),
            url('arialuni.svg#arialuni') format('svg');
        font-weight: normal;
        font-style: normal;
        -fs-pdf-font-embed: embed;
        -fs-pdf-font-encoding: UTF-8;
    }
    
    body {
        font-family:'ArialUnicodeMS';
    }
    

    I've added <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> I was also trying to run grails with -Dfile.encoding=UTF-8, as was mentioned here: http://grails.1312388.n4.nabble.com/PDF-plugin-Having-problems-with-instalation-td2297840.html, but nothing helps. Cyrillic characters are not shown at all. Any other ideas what might be the problem?

    *BTW:*I am packaging my PDF as zip and sending it back to browser in the response like that:

    response.setHeader "Content-disposition", "attachment; filename=test.zip"
    response.setHeader "Content-Encoding", "UTF-8"
    response.contentType = 'application/zip'
    response.outputStream << zip
    response.outputStream.flush()
    response.outputStream.close()
    

    Do I need to somehow consider encoding while zipping????, which I do like that:

    public static byte[] zipBytes(Map<String, ByteArrayOutputStream> fileNameToByteContentMap) throws IOException {
            ByteArrayOutputStream zipBaos = new ByteArrayOutputStream();
            ZipOutputStream zos = new ZipOutputStream(zipBaos);
            fileNameToByteContentMap.eachWithIndex {String fileName, ByteArrayOutputStream baos, i  ->
                byte[] content = baos.buf
                ZipEntry entry = new ZipEntry(fileName)
                entry.setSize(content.length)
                zos.putNextEntry(entry)
                zos.write(content)
                zos.closeEntry()
            }
            zos.close()
            return zipBaos.toByteArray();
        }
    
    • Diodeus - James MacFarlane
      Diodeus - James MacFarlane over 11 years
      If your content-type defined as UTF-8 as well?
  • jaygooby
    jaygooby over 9 years
    The -fs-pdf-font-encoding: Identity-H; is the key; it tells Flying Saucer that this is a Unicode font and not a font that is restricted to certain code pages
  • fego
    fego about 8 years
    to put the font in a folder I used src: url('${baseUrl}/assets/ARIALUNI.TTF'); where baseUrl is passed in the model with grailsLinkGenerator.serverBaseURL
  • Maciek Łoziński
    Maciek Łoziński over 7 years
    I had problems using this solution. I fixed it by using URL fontResourceURL = getClass().getResource("/fonts/CALIBRI.TTF"); (note the initial slash in font resource path) and fontResolver.addFont(fontResourceURL.toString(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); (note toString method instead getPath)
  • Wojtek Okoński
    Wojtek Okoński over 3 years
    From the getResource docs: If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'. Otherwise, the absolute name is of the following form: modified_package_name/name Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e'). But getPath() works fine for me.
  • Wojtek Okoński
    Wojtek Okoński over 3 years
    And basically "fonts/CALIBRI.TTF" can be passed directly: renderer.getFontResolver().addFont("fonts/CALIBRI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);