0

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;
}
  • You are not describing your problem properly. Velocity does not and cannot directly generate a PDF from a template. What you are probably doing is using Velocity to generate some intermediate file and then generating the PDF file from the intermediate file. But what kind of intermediate file is it and how are you then generating the PDF? We need to know the details. – Stephen C Jan 10 '23 at 16:38
  • Hi @StephenC Thanks for the help. I updated the question with the code. Basically what I want to do is encode the html for turkish characters. I'm using flying saucer and openpdf for pdf conversion – Altan Mehmet Türkmen Jan 11 '23 at 05:50

0 Answers0