/**
* @param mess 富文本编辑器里的html信息
* @param fileName 文件名
* @return
* @description html转Pdf[文件上传]
*/
public String htmlToPdfUpload(String mess, String fileName) throws IOException {
// 每次根据时间创建文件夹,作为文件和压缩文件保存目录
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String time = formatter.format(new Date());
File f = new File(filePath);
if (!f.exists()) {
f.mkdirs();
}
//一、先生成html文件
File dest = new File(filePath + File.separator + fileName + time + ".htm");
//字节输出流
FileOutputStream fos = new FileOutputStream(dest);
//接收字节输入流
InputStream is = org.apache.commons.io.IOUtils.toInputStream(mess, "UTF-8");
//为字节输入流加缓冲
BufferedInputStream bis = new BufferedInputStream(is);
//为字节输出流加缓冲
BufferedOutputStream bos = new BufferedOutputStream(fos);
int length;
byte[] bytes = new byte[1024 * 20];
while ((length = bis.read(bytes, 0, bytes.length)) != -1) {
fos.write(bytes, 0, length);
}
bos.close();
fos.close();
bis.close();
is.close();
//二、html文件转成pdf
try {
String HTML = dest.getPath();
Document document = new Document(PageSize.LETTER);
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(filePath + File.separator + fileName + time + ".pdf"));
document.open();
document.addAuthor("test");
document.addCreator("test");
document.addSubject("test");
document.addCreationDate();
document.addTitle("XHTML to PDF");
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
worker.parseXHtml(pdfWriter, document, new FileInputStream(HTML), (InputStream) null, new AsianFontProvider());
document.close();
} catch (Exception e) {
e.printStackTrace();
}
dest.delete();
return fileName + time + ".pdf";
}
01-13
4906
09-10
519