Question

I am using flying saucer with iText 2.1.7 for converting html to pdf. It works fine, but the problem occurs when there are some chinese, korean, etc characters in the html.

I get unexpected characters in my PDF instead of the normal chinese characters

I found this issue opened, so I assume there is currently no way of making flying saucer into rendering the PDF correctly?

PS: I also found this issue, but I can't understand the solution they have provided.

This is the code that I am using

String doc = file.toURI().toURL().toString();
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont (
    "C:\\ARIALUNI.TTF",
     BaseFont.IDENTITY_H,
     BaseFont.EMBEDDED
);
renderer.setDocument(doc);
String outputFile = "report.pdf";
OutputStream os = new FileOutputStream(outputFile);

renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();

Where file is the html which I am trying to convert.

Is there some other way or library to do the same?

This is the css that i am using

@font-face {
  font-family: "Arial";
  src: url("media/arialuni.ttf");
 -fs-pdf-font-embed: embed;
 -fs-pdf-font-encoding: Identity-H; 
}

The HTML file that I need to convert

These are the re-compiled flying saucer jar compatible with itext 2.1..x

Était-ce utile?

La solution

Your font is probably not embedded in the PDF file. ( How do I know if the fonts in a PDF file are embedded or not? )

Every font has a name, ARIALUNI.TTF defines Arial Unicode MS, you should use that.

So change this:

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

* {
        font-family: Arial1;
}

To this:

@font-face {
    font-family: Arial Unicode MS;
    src: url("arialuni.ttf");
    -fs-pdf-font-embed: embed;
    -fs-pdf-font-encoding: Identity-H;
}

* {
        font-family: Arial Unicode MS;
}

This way the font will be embedded.

And you don't need to call renderer.getFontResolver().addFont, the css is enough.

Autres conseils

Try this:

font.addFont(Html2Pdfs.class.getResource("SIMSUN.TTC").toString().substring(6),BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top