Question

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();
    }
Was it helpful?

Solution 2

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.

OTHER TIPS

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; }
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top