使用 Itext 的 pdfHTML 将 html 转换为 pdf 时,字体粗细 css 不起作用

Font weight css is not working when using Itext's pdfHTML to convert html to pdf

这就是我将 HTML 转换为 pdf 的方式:-

  File pdfDest = new File("output");

  ConverterProperties converterProperties = new ConverterProperties();

  HtmlConverter.convertToPdf(html, new FileOutputStream(pdfDest), converterProperties);

以下是重现问题的示例HTML:-

<html>

  <head>
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css?family=IBM+Plex+Sans:wght@400;500;600;700" rel="stylesheet">

    <style>
     body {
       font-family: 'IBM Plex Sans', sans-serif;
       color: #444444;
      }


      .pt-16 {
        padding-top: 16px;
      }

      .pb-16 {
        padding-bottom: 16px;
      }


      .pr-4{
        padding-right: 4px;
      }


      .f-18 {
        font-size: 18px;
      }

 
      .font-weight-medium {
        font-weight: 400;
      }

      .font-weight-semibold {
        font-weight: bold;
      }

      .font-weight-bold {
        font-weight: bold;
      }


      .data {
        display: inline-block;
        padding-left: 14px;
        padding-top: 5px;
      }


    </style>
  </head>

  <body>

          <div class="pt-16">
            <div class="f-18 font-weight-semibold">4. Time Periods</div>
              <div class="pb-16">
                <div class="data">
                  <span class="pr-4">Some data -</span>
                  <span class="font-weight-medium">Lorem,</span>
                  <span class="font-weight-medium">Gypsum,</span>
                </div>
              </div>
          </div>
         
    
  </body>

</html>

在浏览器中看到的内容:-

pdf 中的内容:-

显然,CSS 使文本半粗体/粗体不起作用,如何解决这个问题? 字体似乎是正确的,看起来问题只在 CSS

如果您检查 url https://fonts.googleapis.com/css?family=IBM+Plex+Sans:wght@400;500;600;700 的内容,您会发现它不包含 500、600 和 700 字体的 @font-face 规则-重量。

显然,wght@ 参数是 google css v2 api (https://fonts.googleapis.com/css2) 特有的,在以前版本的 api (https://fonts.googleapis.com/css).

因此您必须将 url 替换为 :

  • https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;600;700
  • https://fonts.googleapis.com/css?family=IBM+Plex+Sans:400,500,600,700

这是我的测试程序(它输出具有预期格式的 pdf,here the result):

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
    File pdfDest = new File("/tmp/output.pdf");

    ConverterProperties converterProperties = new ConverterProperties();

    HtmlConverter.convertToPdf(getHtml(), new FileOutputStream(pdfDest), converterProperties);
    }

    public static String getHtml(){
    return "<html>\n" +
            "\n" +
            "  <head>\n" +
            "    <link rel=\"preconnect\" href=\"https://fonts.gstatic.com\">\n" +
            "    <link href=\"https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;600;700\" rel=\"stylesheet\">\n" +
            "\n" +
            "    <style>\n" +
            "     body {\n" +
            "       font-family: 'IBM Plex Sans', sans-serif;\n" +
            "       color: #444444;\n" +
            "      }\n" +
            "\n" +
            "\n" +
            "      .pt-16 {\n" +
            "        padding-top: 16px;\n" +
            "      }\n" +
            "\n" +
            "      .pb-16 {\n" +
            "        padding-bottom: 16px;\n" +
            "      }\n" +
            "\n" +
            "\n" +
            "      .pr-4{\n" +
            "        padding-right: 4px;\n" +
            "      }\n" +
            "\n" +
            "\n" +
            "      .f-18 {\n" +
            "        font-size: 18px;\n" +
            "      }\n" +
            "\n" +
            " \n" +
            "      .font-weight-medium {\n" +
            "        font-weight: 400;\n" +
            "      }\n" +
            "\n" +
            "      .font-weight-semibold {\n" +
            "        font-weight: bold;\n" +
            "      }\n" +
            "\n" +
            "      .font-weight-bold {\n" +
            "        font-weight: bold;\n" +
            "      }\n" +
            "\n" +
            "\n" +
            "      .data {\n" +
            "        display: inline-block;\n" +
            "        padding-left: 14px;\n" +
            "        padding-top: 5px;\n" +
            "      }\n" +
            "\n" +
            "\n" +
            "    </style>\n" +
            "  </head>\n" +
            "\n" +
            "  <body>\n" +
            "\n" +
            "          <div class=\"pt-16\">\n" +
            "            <div class=\"f-18 font-weight-semibold\">4. Time Periods</div>\n" +
            "              <div class=\"pb-16\">\n" +
            "                <div class=\"data\">\n" +
            "                  <span class=\"pr-4\">Some data -</span>\n" +
            "                  <span class=\"font-weight-medium\">Lorem,</span>\n" +
            "                  <span class=\"font-weight-medium\">Gypsum,</span>\n" +
            "                </div>\n" +
            "              </div>\n" +
            "          </div>\n" +
            "         \n" +
            "    \n" +
            "  </body>\n" +
            "\n" +
            "</html>\n";
    }
}

maven 依赖项:

<dependencies>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>html2pdf</artifactId>
        <version>3.0.5</version>
    </dependency>
</dependencies>