Apache Velocity template turkish characters problem

99 Views Asked by At

I'm trying to create an vm template to convert to pdf. But characters like "İ" , "ş" , "ı" etc. is missing from the pdf. Like here : enter image description here This text must've been İmza Sahibi: $sincomModelData. imza_sahibi bu belgeyle aşağıda belirtilen aracın But the letters are missing in the pdf.

I encode the vm template like this : Template t = ve.getTemplate("templates/helloworld.vm", "UTF-8"); but it only works for some of the characters like "ö" , "ü". How could I encode all turkish letters properly to work with velocity template? Thanks for the help.

Trying to encode turkish characters in velocity template. Here is the code of the conversion of html -> xhtml -> pdf

VelocityEngine ve = new VelocityEngine();
    System.out.println(sincomModel);
    /* next, get the Template */
    ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    ve.setProperty("classpath.resource.loader.class",
            ClasspathResourceLoader.class.getName());
    ve.init();
    System.out.println(vehicleDto);
    Template t = ve.getTemplate("templates/helloworld.vm", "UTF-8");
    VelocityContext context = new VelocityContext();
    context.put("sincomModelData", sincomModel);
    context.put("tanitim_numFirst", tanitim_numFirst);
    context.put("tanitim_numSec", tanitim_numSec);
    context.put("vehicleDate",vehicleDto.getDate());
    context.put("vehicleRenk",vehicleDto.getRenk());
    context.put("yakitIc",yakitIc);
    context.put("yakitDis",yakitDis);
    StringWriter writer = new StringWriter();
    t.merge(context, writer);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    baos = xhtmlToPdf(writer.toString());

    HttpHeaders header = new HttpHeaders();
    header.setContentType(MediaType.APPLICATION_PDF);
    header.set(HttpHeaders.CONTENT_DISPOSITION,
            "attachment; filename=" + fileName.replace(" ", "_"));
    header.setContentLength(baos.toByteArray().length);

    return new HttpEntity<byte[]>(baos.toByteArray(), header);

}
private static String htmlToXhtml(String html) {
    Document document = Jsoup.parse(html);
    document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
    return document.html();
}
private ByteArrayOutputStream xhtmlToPdf(String xhtml) throws IOException {
    String xhtml_son = htmlToXhtml(xhtml);
    ITextRenderer iTextRenderer = new ITextRenderer();
    iTextRenderer.setDocumentFromString(xhtml_son);
    iTextRenderer.layout();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    iTextRenderer.createPDF(baos);
    return baos;
}
0

There are 0 best solutions below