java HTML到PDF转换的PDF有“#”的一些字符[重复]

lpwwtiir  于 2024-01-05  发布在  Java
关注(0)|答案(1)|浏览(310)

此问题在此处已有答案

Embed a custom font into PDF created out of HTML(3个答案)
6天前关闭
我正在使用openhtmltopdf库将HTML转换为PDF,但转换后的PDF使用“#”而不是“#",”#“,"#,

Java代码:

  1. ByteArrayOutputStream renderedPdfBytes = new ByteArrayOutputStream();
  2. PdfRendererBuilder builder = new PdfRendererBuilder();
  3. builder.withHtmlContent(htmlContent, "/");
  4. builder.toStream(renderedPdfBytes);
  5. builder.run();
  6. renderedPdfBytes.close();
  7. byte[] renderedPdf = renderedPdfBytes.toByteArray();
  8. try (FileOutputStream fos = new FileOutputStream(pdfFile)) {
  9. fos.write(renderedPdf);
  10. }

字符串

Html内容:

  1. <html>
  2. <head>
  3. <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  4. </head>
  5. <body>
  6. <span class="close-icon close"></span>
  7. <div class="modal-content">
  8. <div class="modal-header"><h2>İade Prosedürü 27.09.2023</h2></div>
  9. <div class="modal-body" style="margin-bottom: 50px;"><p>İade edilmek istenen.</p></div>
  10. </div>
  11. </body>
  12. </html>

HTML视图:


的数据

转换后的PDF视图:


kgqe7b3p

kgqe7b3p1#

如果您遇到在使用openhtmltopdf库生成的PDF中无法正确显示字符的问题,则可能与字体嵌入或字体支持有关。您可以修改代码以包含具有适当字符支持的自定义字体。下面是它的示例。

  1. PdfRendererBuilder builder = new PdfRendererBuilder();
  2. // Use Noto Sans font (or any other font with proper character support)
  3. builder.useFont(() -> {
  4. try (InputStream inputStream = HtmlToPdfConverter.class.getResourceAsStream("/path/to/NotoSans-Regular.ttf")) {
  5. return FontUtil.createFont(inputStream);
  6. } catch (IOException e) {
  7. e.printStackTrace();
  8. return null;
  9. }
  10. });

字符串

相关问题