生成html:

package com.hessianhealth.common.utils.imgUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;


/**
* Java根据html模板创建 html文件
* Created by on 2020/10/29.
*/
public class CreateHtmlUtils {
/**
* @Title: MakeHtml
* @Description: 创建html
* @param filePath 设定模板文件
* @param imagePath 需要显示图片的路径
* @param disrPath 生成html的存放路径
* @param fileName 生成html名字
* @return void 返回类型
* @throws
*/
public static void MakeHtml(String filePath,String imagePath,String disrPath,String fileName ){
try {
String title = "<image src="+'"'+imagePath+'"'+"/>";
System.out.print(filePath);
String templateContent = "";
FileInputStream fileinputstream = new FileInputStream(filePath);// 读取模板文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
System.out.println(templateContent);
templateContent = templateContent.replaceAll("###title###", title);
System.out.println("---------------开始替换--------------");
System.out.print(templateContent);

String fileame = fileName + ".html";
fileame = disrPath+"/" + fileame;// 生成的html文件保存路径。
FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流
System.out.print("文件输出路径:");
System.out.print(fileame);
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
} catch (Exception e) {
System.out.print(e.toString());
}
}

/**
* 拼接动态 生成html
*/
public static void appendHtml(String htmlFile, String imagePath){

//用于存储html字符串
StringBuilder stringHtml = new StringBuilder();

//输入HTML文件内容
stringHtml.append(HtmlUtils.start_html);
stringHtml.append(HtmlUtils.head);
stringHtml.append(HtmlUtils.start_body);


stringHtml.append(HtmlUtils.getTitle("一.生信分析测试"));
stringHtml.append(HtmlUtils.getContent("这是一张图片",imagePath,"分析图的描述"));


stringHtml.append(HtmlUtils.end_body);
stringHtml.append(HtmlUtils.end_html);

String templateContent=stringHtml.toString();

System.out.println("---------------templateContent--------------"+templateContent);



try{
//将HTML文件内容写入文件中
FileOutputStream fileoutputstream = new FileOutputStream(htmlFile);// 建立文件输出流

byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();


}catch(Exception e){
e.printStackTrace();
}
}


public static void main(String[] args) {

String htmlFile = "C:\\Users\\Desktop\\PDF\\lgn.html";
String imagePath ="C:\\Users\\Desktop\\PDF\\TSNE_clustering.png";
String pdfFile = "C:\\Users\\Desktop\\PDF\\lgn.pdf";
//MakeHtml(filePath,imagePath,disrPath,fileName);

appendHtml(htmlFile,imagePath);

}

}
package com.hessianhealth.common.utils.imgUtils;

/**
* Created by on 2020/10/30.
*/
public class HtmlUtils {


/**
* 注意事项:

1.输入的HTML页面必须是标准的XHTML页面。页面的顶上必须是这样的格式:

<!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">
*/
public static String start_html="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
"<html xmlns=\"http://www.w3.org/1999/xhtml\">";
public static String head="\t<head>\n" +
"\t\t<meta charset=\"utf-8\"></meta>" +
"\t\t<title></title>\n" +
"\t\t<style>\n" +
"\t\t\t* {\n" +
"\t\t\t\tmargin: 0;\n" +
"\t\t\t\tpadding: 0;\n" +
"\t\t\t}\n" +
"\t\t\thtml,body{\n" +
"\t\t\t\tfont-size: 14px;\n" +
"\t\t\t\tfont-family:Microsoft Yahei, \"微软雅黑\", Arial;\n" +
"\t\t\t\tcolor: #333;\n" +
"\t\t\t\tbackground: #FFFFFF;\n" +
"\t\t\t}\n" +
"\t\t\t.container{\n" +
"\t\t\t\twidth: 1200px;\n" +
"\t\t\t\tmargin: 0 auto;\n" +
"\t\t\t\theight: 100%;\n" +
"\t\t\t}\n" +
"\t\t\t.title{\n" +
"\t\t\t\twidth: 100%;\n" +
"\t\t\t\theight: 40px;\n" +
"\t\t\t\tline-height: 40px;\n" +
"\t\t\t\tfont-size: 20px;\n" +
"\t\t\t\tfont-weight: 900;\n" +
"\t\t\t\tborder-bottom: 2px solid #ccc;\n" +
"\t\t\t\tmargin-bottom: 24px;\n" +
"\t\t\t}\n" +
"\t\t\t.title span{\n" +
"\t\t\t\tdisplay: inline-block;\n" +
"\t\t\t\tpadding-right: 20px;\n" +
"\t\t\t\tborder-bottom: 2px solid #000;\n" +
"\t\t\t}\n" +
"\t\t\t.descripe span{\n" +
"\t\t\t\ttext-indent: 2em;\n" +
"\t\t\t\tdisplay: block;\n" +
"\t\t\t\tmargin-bottom: 20px;\n" +
"\t\t\t\tline-height: 24px;\n" +
"\t\t\t}\n" +
"\t\t\t.chartsTitle{\n" +
"\t\t\t\tbackground: #8692A8;\n" +
"\t\t\t\ttext-align: center;\n" +
"\t\t\t\twidth: 100%;\n" +
"\t\t\t\theight: 40px;\n" +
"\t\t\t\tline-height: 40px;\n" +
"\t\t\t\tcolor: #000;\n" +
"\t\t\t\tfont-weight: 900;\n" +
"\t\t\t\tfont-size: 16px;\n" +
"\t\t\t\tmargin-bottom: 20px;\n" +
"\t\t\t}\n" +
"\t\t\t.chartDes{\n" +
"\t\t\t\ttext-align: center;\n" +
"\t\t\t\tcolor: #2F9E2C;\n" +
"\t\t\t\tfont-weight: 900;\n" +
"\t\t\t\tmargin-top: 20px;\n" +
"\t\t\t}\n" +
"\t\t\t.chartImgBox {\n" +
"\t\t\t\twidth: 100%;\n" +
"\t\t\t\tdisplay: flex;\n" +
"\t\t\t}\n" +
"\t\t\t.chartImg:first-child{\n" +
"\t\t\t\tmargin-right: 20px;\n" +
"\t\t\t}\n" +
"\t\t\t.chartImg img{\n" +
"\t\t\t\twidth: 100%;\n" +
"\t\t\t}\n" +
"\t\t</style>\n" +
"\t</head>";

public static String start_body="\t<body style = \"font-family: SimSun;\">\n" +
"\t\t<div class=\"container\">";



public static String end_body="\t\t</div>\n" +
"\t</body>";

public static String end_html="</html>";

/**
* 获取标题
* @return
*/
public static String getTitle(String title){
StringBuilder stringHtml = new StringBuilder();
stringHtml.append("<div class=\"title\">");
stringHtml.append("<span>"+title+"</span>");
stringHtml.append("</div>");

return stringHtml.toString();
}

/**
* 获取内容
* @param content
* @return
*/
public static String getContent(String content,String imgPath,String imgDescribe){
StringBuilder stringHtml = new StringBuilder();
stringHtml.append("<div class=\"content\">");
stringHtml.append("<p class=\"descripe\">"+content+"</p>");

//------------图片----------
stringHtml.append("<div class=\"charts\">");
stringHtml.append("<p class=\"chartsTitle\">ALL</p>");
stringHtml.append("<div class=\"chartImgBox\">" +
"<div class=\"chartImg\">");

stringHtml.append("<img src='"+imgPath+"'/>");


stringHtml.append("</div>" +
"</div>");

stringHtml.append("<p class=\"chartDes\">"+imgDescribe+"</p>");//图片描述
stringHtml.append("</div>");
//------------图片end----------


stringHtml.append("</div>");

return stringHtml.toString();
}



}

html转pdf:

package com.hessianhealth.common.utils.pdfUtils;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.layout.font.FontProvider;

import java.io.File;
import java.io.IOException;

/**
* Created by on 2020/10/31.
*/
public class PdfTest {


private static void html2Pdf(){
ConverterProperties converterProperties = new ConverterProperties();
//指定目录,支持将html相对路径的图片转换到pdf,注意pom文件需要配置将img文件加入到编译后的项目中
converterProperties.setBaseUri("C:\\Users\\Desktop\\PDF\\");
FontProvider fontProvider = new FontProvider();
fontProvider.addDirectory("C:\\Users\\Desktop\\PDF\\ttf");
converterProperties.setFontProvider(fontProvider);

//读取的html文件地址
File htmlFile = new File("C:\\Users\\Desktop\\PDF\\lgn.html");
//生成的pdf地址
File pdfFile = new File("C:\\Users\\Desktop\\PDF\\lgn.pdf");
try {
//HtmlFile TO PdfFile
HtmlConverter.convertToPdf(htmlFile,pdfFile,converterProperties);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
html2Pdf();
}
}

pom文件:

<!--将html转换成pdf-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>2.0.2</version>
</dependency>

设置中文字体,ttf文件夹下SimSum-01和Dengb分别支持细字体和粗字体,缺一不可

链接: https://pan.baidu.com/s/1mcUAhE9ane0qyKW1Q6ByTg
提取码: sx89

Java生成带文字和图片的html并转化为pdf!_java