英文:
Show Bangla font properly on pdf file generated from Java project
问题
我正在基于Java、Spring、Hibernate、Thymeleaf、MySQL开发一个项目。在项目中,我需要在多个部分中生成PDF报告。因此,我开始使用Flying Saucer,并且一切都运行得很完美,直到我尝试在PDF报告中包含孟加拉语字体。起初,什么都没有显示。然后我包含了像"Kalpurush"、"SolaimanLipi"这样的孟加拉语字体。现在它显示出了孟加拉语字体,但形式不正确。比如,"মোট উপার্জন" 变成了 "ম োট উপ ার্জন"。我有一些页面使用了孟加拉语字体,它们都按预期工作。问题出现在使用孟加拉语字体的PDF中。我该如何解决这个问题?
还有哪些替代Flying Saucer的方法可以用来生成带有孟加拉语字体的PDF报告?
我的代码:
Controller
@GetMapping("/print_bangla_pdf")
private void printBanglaPdf(HttpServletResponse response) throws Throwable {
response.setContentType("application/octet-stream");
String headerKey = "Content-Disposition";
String headerValue = "attachment; filename=bangla_pdf_report.pdf";
response.setHeader(headerKey, headerValue);
Context context = new Context();
context.setVariable("banglaWord", "মোট উপার্জন");
String processHTML = templateEngine.process("pdf_reports/bangla_pdf_report", context);
ServletOutputStream outputStream = response.getOutputStream();
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver resolver = renderer.getFontResolver();
resolver.addFont("C:\\kalpurush.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.setDocumentFromString(processHTML);
renderer.layout();
renderer.createPDF(outputStream, false);
renderer.finishPDF();
outputStream.close();
}
Bangla_pdf_report.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<style>
.bengaliFont {
font-family: "Kalpurush";
}
</style>
</head>
<body>
<p class="bengaliFont" th:text="'মোট উপার্জন'"></p>
<p class="bengaliFont" th:text="${banglaWord}"></p>
</body>
</html>
英文:
I am working on a porject based on Java, Spring, Hibernate, Thymeleaf, MySQL. For the project I had to generate PDF reports in multiple sections. So I started using Flying Saucer and everything worked perfectly until I tried to include Bangla font in the PDF reports. At first it showed nothing. Then I included Bangla fonts like "Kalpurush", "SolaimanLipi". Now it shows Bangla fonts but in incorrect form. Like "মোট উপার্জন" has become "ম োট উপ ার্জন". I have some-pages with Bangla fonts and they are working as expected. the Problem occurs when using Bangla fonts in PDF. How can I solve this problem?
And what are the alternatives to Flying Saucer which can be used to generate PDF reports with Bangla font.
My Code:
Controller
@GetMapping("/print_bangla_pdf")
private void printBanglaPdf(HttpServletResponse response) throws Throwable {
response.setContentType("application/octet-stream");
String headerKey = "Content-Disposition";
String headerValue = "attachment; filename=bangla_pdf_report.pdf";
response.setHeader(headerKey, headerValue);
Context context = new Context();
context.setVariable("banglaWord", "মোট উপার্জন");
String processHTML = templateEngine.process("pdf_reports/bangla_pdf_report", context);
ServletOutputStream outputStream = response.getOutputStream();
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver resolver = renderer.getFontResolver();
resolver.addFont("C:\\kalpurush.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.setDocumentFromString(processHTML);
renderer.layout();
renderer.createPDF(outputStream, false);
renderer.finishPDF();
outputStream.close();
}
Bangla_pdf_report.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<style>
.bengaliFont {
font-family: "Kalpurush";
}
</style>
</head>
<body>
<p class="bengaliFont" th:text="'মোট উপার্জন'"></p>
<p class="bengaliFont" th:text="${banglaWord}"></p>
</body>
</html>
答案1
得分: 2
你尝试过隔离问题吗?我怀疑有两个潜在原因:
- 这是否与Thymeleaf引擎有关的问题?
- 您是否缺少TTF字体的@import?
为了排除这些原因,我尝试创建一个独立的HTML文件,而不使用Thymeleaf模板,并且除了Kalpurush.TTF之外,还使用了Google孟加拉字体(Galada)。可以使用Google Fonts API导入Galada,而Kalpurush.TTF必须从本地文件下载并导入。
在Chrome和Edge中,它渲染得很好。以下是代码片段(它使用本地文件,所以我不确定在Stack Overflow中如何渲染)。我还附上了浏览器中显示的屏幕截图。您可以将HTML文件保存在本地,然后自行尝试。只需将Kalpurush.TTF的@import替换为指向您本地目录路径的方式。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<style>
@import url('https://fonts.googleapis.com/css2?family=Galada&display=swap');
@import url('file://d:/Giri/temp/so/kalpurush.ttf');
.bengaliFont {
font-family: 'Galada', cursive;
}
.bengaliFont2 {
font-family: 'Kalpurush';
}
</style>
</head>
<body>
<p class="bengaliFont"><span>Galada font: </span>মোট উপার্জন</p>
<p class="bengaliFont2"><span>Kalpurush font: </span>মোট উপার্জন</p>
</body>
</html>
这是我本地浏览器渲染此HTML文件的屏幕截图。
这表明字体文件没有问题。
您可能希望将独立的HTML文件传递给FlyingSaucer以生成PDF,并查看是否解决了问题,然后从那里继续。
英文:
Have you tried isolating the problem? I suspect two potential causes:
- Is it a problem with the Thymeleaf engine?
- Are you missing an @import for the TTF font?
In order to eliminate the causes, I tried creating a standalone HTML file without using Thymeleaf templating and using a Google Bangla font (Galada) also in addition to Kalpurush.TTF. Galada can be imported using Google Fonts API, whereas Kalpursh.ttf had to be downloaded and imported from local file.
It renders properly in Chrome and Edge. Here's the snippet (it uses a local file, so I am not sure how it would render within SO). I am also attaching a screenshot image of how it appears in my browser. You can save the HTML locally and try it yourself. Just replace the @import for Kalpurush.TTF to refer to your local directory path.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<style>
@import url('https://fonts.googleapis.com/css2?family=Galada&display=swap');
@import url('file://d:/Giri/temp/so/kalpurush.ttf');
.bengaliFont {
font-family: 'Galada', cursive;
}
.bengaliFont2 {
font-family: 'Kalpurush';
}
</style>
</head>
<body>
<p class="bengaliFont"><span>Galada font:&nbsp;</span>মোট উপার্জন</p>
<p class="bengaliFont2"><span>Kalpurush font:&nbsp;</span>মোট উপার্জন</p>
</body>
</html>
<!-- end snippet -->
Here's the screenshot of my browser rendering this HTML file locally.
This points to no problems with the font files.
You may want to take the standalone html file and run it through FlyingSaucer to generate the PDF and see if it solves the problem and then go from there.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论