HKP HKP - 2 months ago 11x
Java Question

itext pdf creating in java: Chinese not working when a service call is made from localhost but works fine when called from main method

I am trying to create pdf that contains Chinese characters.
It is working as expected when i call generatePdf()` from the main method. The screen shot is shown. However when i deploy in weblogic server and call from the browser "http://localhost:7001/PdfGeneration/itext/genpdf" it doesn't apply the font. I have attached screen shot.

Following settings:

  • weblogic version: 12.2.1

  • Itextpdf,xmlworker: 5.4.5

  • Jersey version: 2.2

  • Intellij IDE 2016.1.3

  • jdk 1.8

style.css contains just this

body {
font-family: "arial unicode ms";


@Api(value = "itext service")
public class iTextService {

//creating an object in main and calling the method works fine
//this part is commented when calling form server (localhost)
public iTextService() throws Exception {

public static void main(String args[]) throws Exception {
iTextService obj = new iTextService();

public void generatePdf() throws Exception {

ByteArrayOutputStream out = new ByteArrayOutputStream();
Document doc = new Document(PageSize.A4, 40, 40, 20, 10);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("testPDF.pdf"));;
parseHTML(writer, doc);

//this method gives the artifact path
// screen shot of the artifact is shown
public String getFilePath() {
URL url = getClass().getClassLoader().getResource("/resource/");
String path = url.getPath();
try {
path = URLDecoder.decode(path, "utf-8");
} catch (UnsupportedEncodingException e) {
path = new File(path).getPath();
return path;

public void parseHTML(PdfWriter writer, Document document) throws Exception {

//comment this when calling from main method
String pathToRes = getFilePath();

//case 1 : calling from main method
//byte[] encoded = Files.readAllBytes("style.css"));
//case 2: calling from browser (localhost)
byte[] encoded = Files.readAllBytes(Paths.get(pathToRes + "\\style.css"));
String style = new String(encoded);

CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(style.getBytes()));

XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
//case 1
//case 2
fontProvider.register(pathToRes + "\\ARIALUNI.ttf");

//FontFactory.register(pathToFont + "\\ARIALUNI.ttf");
//FontFactory.setFontImp(fontProvider); //tried with these two along with exisitng code once

CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);

// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser parser = new XMLParser(worker);
parser.parse(new ByteArrayInputStream("<body><p>篆書 test</p></body>".getBytes()), Charset.forName("UTF-8"));




I found the issue, I used the parse function provided by itext which requires Inputstream as you can see below

public void parse(InputStream in, Charset charSet) throws IOException { this.charset = charSet; InputStreamReader reader = new InputStreamReader(in, charSet); this.parse((Reader)reader); }

And Initially I was doing this

XMLParser parser = new XMLParser(worker); parser.parse(new ByteArrayInputStream(buildTemplate().getBytes("UTF-8")), Charset.forName("UTF-8"));

Now I created an Inputstream and then I passed it to the parse and it worked.

InputStream is = new ByteArrayInputStream(buildTemplate().getBytes("UTF-8")); parser.parse(is, Charset.forName("UTF-8"));

For some reason the first method works in local but not when hosted.