itext7 html转pdf 英文或数字不自动换行的问题

  1. 添加依赖 pom.xml
<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext7-core</artifactId>
            <version>7.1.3</version>
            <type>pom</type>
</dependency>
<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>2.0.0</version>
</dependency>
package cn.sh.ideal.utils;

import java.io.ByteArrayOutputStream;
import java.util.List;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.IOException;
import com.itextpdf.io.font.otf.GlyphLine;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.IBlockElement;
import com.itextpdf.layout.element.IElement;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.splitting.DefaultSplitCharacters;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Steven
 * @version 1.0
 * @date 2023/8/22 9:42
 * @description
 */
@Slf4j
public class ItextPdfUtil {

    public static ByteArrayOutputStream htmlToPdf(String htmlStr){
        Document document = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            PdfDocument pd = new PdfDocument(new PdfWriter(baos));
            document = new Document(pd, PageSize.A3);
            document.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters() {
                @Override
                public boolean isSplitCharacter(GlyphLine text, int glyphPos) {
                    //解决word-break: break-all;不兼容的问题,解决纯英文或数字不自动换行的问题
                    return true;
                }
            });
            DefaultFontProvider defaultFontProvider = new DefaultFontProvider(false, false, false);
            //字体路径
            defaultFontProvider.addFont("./font/NotoSansCJKsc-Regular.otf");
            ConverterProperties c = new ConverterProperties();
            c.setCharset("utf-8");
            c.setFontProvider(defaultFontProvider);
            List<IElement> list = HtmlConverter.convertToElements(htmlStr, c);
            for (IElement ie : list) {
                document.add((IBlockElement) ie);
            }
        } catch (IOException e) {
            log.error("HtmlToPDF 转换pdf失败....",e);
            throw  new RuntimeException("HtmlToPDF 转换pdf失败....",e);
        } catch (Exception e) {
            log.error("HtmlToPDF 转换pdf失败....", e);
            throw  new RuntimeException("HtmlToPDF 转换pdf失败....",e);
        } finally {
            if (document != null) {
                try {
                    document.close();
                } catch (Exception e) {
                    log.error("document关闭异常...",e);
                }
            }
        }
        return baos;
    }
}

字体目录:C:\Windows\Fonts