**第一种方法 :用XMLWorkerHelper方法把字符串类型的HTML转成PDF **
String dtd="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"> ";
String htmlString = "<html>"+dtd+"<style></style><body><div>" +
HtmlCount+"</div></body></html>";//htmlString :为要打印的html界面的字符串
HtmlToPDF.htmlToPDF(htmlString, fildPath,htmlFileName);//fildPath为pdf要保存的地址
第二步:
public static void htmlToPDF(String htmlString,String pdfPath,String htmlFileName) {
try {
Document document = new Document(PageSize.A4);
PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream(pdfPath));
document.open();
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(){
public Font getFont(String fontname, String encoding, float size, int style) {
return super.getFont(fontname == null ? "宋体" : fontname, encoding, size, style);
}
};
document.addAuthor("pdf作者");
document.addCreator("pdf创建者");
document.addSubject("pdf主题");
document.addCreationDate();
document.addTitle("pdf标题,可在html中指定title");
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
InputStream inputStream=null;
worker.parseXHtml(pdfWriter, document, new ByteArrayInputStream(htmlString.getBytes("UTF-8")),inputStream,Charset.forName("UTF-8"),fontProvider);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
**第一种方法 :用wxhtmltopdf 工具将HTML转成PDF **
下载wxhtmltopdf 工具
在这里插入代码片public class HtmlToPDF{
//该路径为wkhtmltopdf在电脑中的路径
//private static final String toPdfTool = "D:\\zurich\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
public static void htmlToPDF(String htmlString,String pdfPath,String htmlFileName) {
try {
//该处是把html的字符串写入htmlFileName这个地址处
OutputStream outputStream = new FileOutputStream(htmlFileName);
outputStream.write(htmlString.getBytes("UTF-8"));
HtmlToPDF.convert(htmlFileName,pdfPath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* html转pdf
* @param srcPath html路径,可以是硬盘上的路径,也可以是网络路径
* @param destPath pdf保存路径
* @return 转换成功返回true
*/
public static boolean convert(String srcPath, String destPath){
File file = new File(destPath);
File parent = file.getParentFile();
StringBuilder cmd = new StringBuilder();
cmd.append(toPdfTool);
cmd.append(" ");
cmd.append(srcPath);
cmd.append(" ");
cmd.append(destPath);
boolean result = true;
try{
Process proc = Runtime.getRuntime().exec(cmd.toString());
}catch(Exception e){
result = false;
e.printStackTrace();
}
return result;
}
}
注意:因项目需要html转成pdf后,需要拿到转换好的pdf的地址,取到文件,给pdf中添加水印,可是如果html转pdf的文件偏多时,会存在时间差,文件还没有完全转换完成。所以添加了一个等待时间。记录一下!
//判断PDF文件是否生成成功
File file = new File(input);//input为存储pdf的地址
Calendar calendar = Calendar.getInstance();
long time = System.currentTimeMillis();
//如果没有文件则等待5秒再检查,如果4分钟还未检测到,则返回提示超时请重新打印
while(!file.exists()){
logger.info("没有检测到PDF文件进入等待状态"+System.currentTimeMillis());
logger.info("文件名:"+file.getName());
try {
Thread.currentThread().sleep(5000);//5秒
long timeNow = System.currentTimeMillis();
long minute=(timeNow-time)/(1000*60);//转化minute
if(minute>=4){
throw new Exception("打印超时,请重新打印或联系管理员!");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
logger.info("生成文件名:"+file.getName());
logger.info("是否生成:"+file.exists());