iText7 支持中文,使用 阿里巴巴普惠体 免费可商用。

java

package com.starxg.itext7chinesefont;import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.font.FontProvider;
import com.itextpdf.styledxmlparser.css.media.MediaDeviceDescription;
import com.itextpdf.styledxmlparser.css.media.MediaType;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Objects;public class IText7ChineseFont {public static void main(String[] args) throws Exception {final File fontDir = new File(SystemUtils.getJavaIoTmpDir(), "IText7ChineseFont");if (!fontDir.exists() && !fontDir.mkdirs()) {throw new IllegalStateException();}final InputStream is = Objects.requireNonNull(IText7ChineseFont.class.getResourceAsStream("/Template.html"));final InputStream fontLight = Objects.requireNonNull(IText7ChineseFont.class.getResourceAsStream("/fonts/AlibabaPuHuiTi-2-45-Light.ttf"));final InputStream fontBold = Objects.requireNonNull(IText7ChineseFont.class.getResourceAsStream("/fonts/AlibabaPuHuiTi-2-85-Bold.ttf"));IOUtils.copy(fontLight, Files.newOutputStream(new File(fontDir, "AlibabaPuHuiTi-2-45-Light.ttf").toPath()));IOUtils.copy(fontBold, Files.newOutputStream(new File(fontDir, "AlibabaPuHuiTi-2-85-Bold.ttf").toPath()));final File file = File.createTempFile("itext-chinese-font-", ".pdf");final ConverterProperties properties = new ConverterProperties();final FontProvider fontProvider = new FontProvider();fontProvider.addDirectory(fontDir.getAbsolutePath());properties.setFontProvider(fontProvider);properties.setMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT));try (final OutputStream os = Files.newOutputStream(file.toPath());final PdfWriter pdfWriter = new PdfWriter(os);final PdfDocument pdfDocument = new PdfDocument(pdfWriter)) {try (final Document doc = HtmlConverter.convertToDocument(IOUtils.toString(is, StandardCharsets.UTF_8), pdfDocument, properties)) {doc.add(new AreaBreak());}}System.out.println(file.getAbsolutePath());FileUtils.deleteQuietly(fontDir);}
}

pom.xml

<properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><itext7-core.version>7.2.1</itext7-core.version><html2pdf.version>4.0.1</html2pdf.version><commons-io.version>2.11.0</commons-io.version></properties><dependencies><dependency><groupId>com.itextpdf</groupId><artifactId>itext7-core</artifactId><version>${itext7-core.version}</version><type>pom</type></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>${commons-io.version}</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.8.1</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>html2pdf</artifactId><version>${html2pdf.version}</version></dependency></dependencies>

效果

完整代码

https://github.com/starxg/itext7-chinese-font

iText7 HTML to PDF 支持中文 支持加粗相关推荐

  1. LaTeX中宋体中文无法加粗的情况

    1 致谢 感谢LaTeX科技排版工作室的帮助 链接如下: http://www.latexstudio.net/ 2 问题描述 最近在用LaTeX写硕士论文 遇到一个奇怪的问题 那就是宋体的中文无法加 ...

  2. Latex排版小技巧0001:中文字体加粗

    论文排版时中文字体加粗 在设置CJK字体过程中添加[AutoFakeBold = {3.17}]设置,参数 "3.17" 用于指定加粗程度.加粗时使用 latex 默认加粗指令 \ ...

  3. XeLaTeX下LaTeX中文字体加粗解决方案

    (伪斜体法) 在页首添加如下指令:(以常用的黑体和宋体为例) \let\heiti\relax \newCJKfontfamily\heiti{SimHei}[AutoFakeBold] \setCJ ...

  4. android TextView设置中文字体加粗实现方法

    英文设置加粗可以在xml里面设置: 复制代码 代码如下: <SPAN style="FONT-SIZE: 18px">android:textStyle="b ...

  5. 如何实现 “中间这几个字要加粗,但是不要太粗,比较纤细的那种粗” ?

    分享一个最近做业务遇到的简单又蛮有意思的的文本显示处理过程.具体就是有这么一段文字,类似"转盘抽奖获得跑车x1,点击领取-",用TextView显示. 设计师:"能让文字 ...

  6. Markdown (CSDN) MD编辑器(二)- 文本样式(更改字体、字体大小、字体颜色、加粗、斜体、高亮、删除线)

    目录 1.Markdown现有的文本样式. 2.HTML的font标签-改字体.字体颜色.字体大小. 3.HTML的mark标签-标记文本 4.HTML的strong标签-加粗文本 5.HTML的em ...

  7. andorid text加粗_android textView设置粗体

    释放双眼,带上耳机,听听看~! 今天实在没有什么可以讲的,在网上搜素了很多资料,也没有找到合适的内容.于是只有讲这个很简单的内容,毕竟自己还是想保持写博客的习惯. 第一种实现的代码: TextView ...

  8. Android 设置TextView字体加粗

    今天,简单讲讲Android里如何设置TextView字体加粗. 不废话了,用过多次,还是没记住.直接上代码. 1.布局文件中这样设置即可: XML/HTML代码 android:textStyle= ...

  9. 厦门大学LaTeX模板:宋体加粗变黑体问题

    为了防止LaTeX模板中,中文宋体加粗被覆盖为黑体,可以 在documentclass中这是字体类别为fanfol \ifXMU@istwoside \LoadClass[zihao=-4,a4pap ...

最新文章

  1. 从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码
  2. Hashtable元素的删除
  3. 短址(short URL)原理及其实现
  4. numpy更改形状、类型
  5. python子类分配
  6. 三相逆变器双pi控制器参数如何调节_光伏逆变器短路特性简析
  7. 优酷视频如何将地区设置为中国大陆
  8. atitit.窗体静听esc退出本窗体java swing c# .net php
  9. EndNote编辑毕业论文格式
  10. matlab中的求导函数diff
  11. Reflex WMS入门系列三十:不使用RF功能做上架
  12. windows2003下如何关闭默认共享
  13. 项目(百万并发网络通信架构)10.3---send()函数的极限测试
  14. 八个典型的大数据应用案例
  15. STM32F407学习笔记——MG90S舵机模块(基本控制)
  16. 7-2 多分支表达-数据奇偶判断
  17. checkbox选中和不选中 jqu_JQuery控制radio选中和不选中方法总结
  18. ureport 报表合并单元格后双斜表头制作
  19. Cydia 软件备份工具 xBackup,升级刷机好助手
  20. No-sql课程学习

热门文章

  1. python3.6 爬虫 上海所有教育机构地址
  2. php停车场收费系统,停车场收费系统C语言版
  3. RE 逆向工程初学者指南:方法和工具
  4. 2020中高级前端面试题合集
  5. Mac第三方恶意软件删除不了怎么办,这个方法你尝试了吗?
  6. 怎么压缩图片大小,图片压缩方法
  7. docker发布项目的一般性步骤
  8. 计算机专业硕士论文评语,硕士论文答辩导师评语
  9. win7系统损坏怎么修复【系统天地】
  10. 腾达ac23虚拟服务器怎么设置,腾达(Tenda)AC23 如何设置上网? | 192路由网