转载 

xhtmlrenderer将html转成图片 中文字体空白,不依赖系统处理

分类:java    385人阅读    IT小君  2020-08-26 19:43

1、pom.xml新增xhtmlrenderer依赖
<dependency>
  <groupId>org.xhtmlrenderer</groupId>
  <artifactId>core-renderer</artifactId>
  <version>R8</version>
</dependency>

2、模板 template.xhtml 一定要是标准的HTML,否则会报错

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head lang="en">  
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
</head>  
<body style="font-family:SimSun;">
  ...
</body>
</html>

3、核心代码

int width = 600, height = 800;
File template = new File("xxx/template.xhtml");
Java2DRenderer renderer = new Java2DRenderer(template, width, height);
// 自动装载字体..
// 这样就不需要服务器安装字体了..(跳过第4步..
AWTFontResolver fontResolver = (AWTFontResolver) renderer.getSharedContext().getFontResolver();
fontResolver.setFontMapping(FontEnums.SIMSUN.getName(), FontEnums.SIMSUN.getFont());
BufferedImage image = renderer.getImage();
FSImageWriter imageWriter = new FSImageWriter();  
imageWriter.write(img, "xxx/xxx.png");

4、Linux服务器缺乏中文字库,导致中文乱码

// 4.1 查看系统字体..
fc-list
// fc-list:command not found...
// 说明没有安装字体库..

// 4.2 安装字体库..
yum -y install fontconfig

// 4.3 将字体.ttf文件拷贝到/usr/share/fonts/文件夹下

// 4.5 重启服务器

5、将字体.ttf文件拷贝到/resources/资源文件下,自动装载字体,部署环境时,就不用操心字体了

public enum FontEnums {
    SIMSUN("SimSun", "simsun.ttf"),
    ;
    private String name;
    private String path;
    private Font font;

    FontEnums(String name, String path) {
        this.name = name;
        this.path = path;
    }

    public String getName() {
        return this.name;
    }

    public Font getFont() {
        if (null == font) {
            InputStream is = null;
            BufferedInputStream bis = null;
            try {
                is = Resources.getResourceAsStream(path);
                bis = new BufferedInputStream(is);
                // 可能会报 "java.awt.FontFormatException: bad table, tag=23592960" 错误..
                font = Font.createFont(Font.TRUETYPE_FONT, bis);
            } catch (Exception e) {
            } finally {
                try {
                    if (bis != null) {
                        bis.close();
                    }
                } catch (Exception e) {
                }
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (Exception e) {
                }
            }
        }
    }
}

6、解决 “java.awt.FontFormatException: bad table, tag=23592960” 问题

发现本地运行时与使用maven打包之后的ttf文件大小不一致

以下摘自maven文档

Warning: Do not filter files with binary content like images! This will most likely result in corrupt output.

警告:不要过滤含有二进制内容的文件,如图像!这很可能导致输出损坏。

If you have both text files and binary files as resources it is recommended to have two separated folders. One folder src/main/resources (default) for the resources which are not filtered and another folder src/main/resources-filtered for the resources which are filtered.

如果您同时拥有文本文件和二进制文件作为资源,建议使用两个独立的文件夹。一个文件夹src/main/resources(默认)用于未过滤的资源,另一个文件夹src/main/resources-filtered用于已过滤的资源。

在pom.xml文件中,添加maven-resources-plugin插件,通过nonFilteredFileExtension过滤后缀为ttf的文件
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <encoding>UTF-8</encoding>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>


点击广告,支持我们为你提供更好的服务

html5 canvas进度条圆环图表统计动画特效

中小型创意设计服务公司网站模板

canvas炫酷鼠标移动文字粒子特效

网页设计开发公司网站模板

响应式咖啡饮品宣传网站模板

js+css3抽奖转盘旋转点餐代码

css+js实现的颜色渐变数字时钟动画特效

jQuery右端悬浮带返回顶部特效

HTML5数字产品服务公司网站模板

响应式太阳能能源公司网站模板

HTML5 Canvas竖直流动线条背景动画特效

响应式时尚单品在线商城网站模板

html5图标下拉搜索框自动匹配代码

小众时尚单品在线电子商务网站模板

css鼠标跟随文字模糊特效

现代时尚家具公司网站模板

有机水果蔬菜HTML5网站模板

html5 canvas彩色碎片组合球形旋转动画特效

html5 svg夜空中星星流星动画场景特效

HTML5现代家居装潢公司网站模板

点击广告,支持我们为你提供更好的服务
 工具推荐 更多»
点击广告,支持我们为你提供更好的服务