Apache Velocity 模板土耳其字符问题

Apache Velocity template turkish characters problem

提问人:Altan Mehmet Türkmen 提问时间:1/10/2023 最后编辑:Altan Mehmet Türkmen 更新时间:1/11/2023 访问量:90

问:

我正在尝试创建一个 vm 模板以转换为 pdf。但是 pdf 中缺少“İ”、“ş”、“ı”等字符。就像这里一样:enter image description here这段文字一定是 İmza Sahibi: $sincomModelData。 imza_sahibi Bu belgeyle aşağıda belirtilen 阿拉辛 但是 pdf 中缺少这些字母。

我像这样对 vm 模板进行编码:但它仅适用于某些字符,例如“ö”、“ü”。如何正确编码所有土耳其字母以使用速度模板? 感谢您的帮助。Template t = ve.getTemplate("templates/helloworld.vm", "UTF-8");

尝试在速度模板中对土耳其字符进行编码。 这是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;
}
Java 模板 编码 速度 土耳其语

评论

0赞 Stephen C 1/11/2023
您没有正确描述您的问题。Velocity 不会也不能直接从模板生成 PDF。您可能正在做的是使用 Velocity 生成一些中间文件,然后从中间文件生成 PDF 文件。但是它是什么样的中间文件,然后如何生成 PDF?我们需要知道细节。
0赞 Altan Mehmet Türkmen 1/11/2023
嗨@StephenC感谢您的帮助。我用代码更新了问题。基本上我想做的是为土耳其字符编码html。我正在使用飞碟和 openpdf 进行 pdf 转换

答: 暂无答案