Itext生成pdf文件,html转pdf时中文一直显示不出来

复制代码
 之前使用freemark模板渲染ftl页面,转出的pdf中,css2有些样式好像不支持,比较常用的居中样式都没有效果,text-align:center
改造成使用html页面来转pdf,css2的样式可以生效,itext是不支持css3的弹性布局的

ITextRenderer pdfRenderer = new ITextRenderer();
            // 添加字体设置
            ITextFontResolver fontResolver = pdfRenderer.getFontResolver();
          /*  fontResolver.addFont("C:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            fontResolver.addFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
*/
            try {
                fontResolver.addFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

                /*String path = ResourceUtil.getResourceObj("classpath:/fonts/SimSun.ttf").getUrl().getPath();
                *//*fontResolver.addFont(path, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);*//*
                fontResolver.addFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
*/
            } catch (Exception e) {
                log.error(e.getMessage(),e);
            }

            pdfRenderer.setDocumentFromString(createXHtml(rowsHtml));
           /* File f= getFileFromResources();
            pdfRenderer.setDocument(f);*/
            pdfRenderer.layout();
            pdfRenderer.createPDF(outputStream);

尝试好多种方式,最后可能是跟字体有关系

字体设置为C:/Windows/Fonts/simhei.ttf 黑体,同时html页面上样式要添加

复制代码
body {
    font-family: SimHei;
}

pdf生成方式参考项目:E:\myfiles\project\github\demo-html2pdf

字体问题参考文章:https://blog.51cto.com/u_15127651/4527950

最后完美解决字体问题!!

相关推荐
摘星编程1 分钟前
深入 Actix-web 源码:解密 Rust Web 框架的高性能内核
开发语言·前端·rust·actixweb
小白的码BUG之路2 分钟前
Vue3 -- 响应式 ref和 reactive
前端·javascript·vue.js
Onion8 分钟前
Vue2日历组件-仿企微日程日历
前端·javascript·vue.js
用户84298142418109 分钟前
js中如何隐藏eval关键字?
前端·javascript·后端
chxii11 分钟前
前端与Node.js
前端·node.js
zmirror13 分钟前
React-Router v6 useNavigate 非组件不生效
前端
红树林0733 分钟前
BeautifulSoup 的页面中需要获取某个元素的 xpath 路径
前端·python·网络爬虫·beautifulsoup
三小河1 小时前
教你发布一个npm的组织包
前端
青椒a1 小时前
002.nestjs后台管理项目-数据库之prisma(上)
前端
米诺zuo1 小时前
react 中的useContext和Provider实践
前端·react.js