import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
/**
* 生成PDF
*
*/
public class PDFTool {
public static InputStream generationPdf(HttpServletRequest request,String htmlText ) throws Exception{
String path = request.getSession().getServletContext().getRealPath("pdf");
System.out.println("原path: " + path);
//把路径中的反斜杠转成正斜杠
path = path.replaceAll("\\\\", "/"); //
String tmpFileName = UUID.randomUUID().toString(); //生成随机文件名
/* File dir = new File(tmpdir);
if(!dir.exists())
dir.mkdirs(); */
System.out.println(path +";tmpdirtmpdirtmpdirtmpdirtmpdirtmpdirtmpdirtmpdir;"+tmpFileName);
String htmlFileName = path +"/" + tmpFileName + ".html" ;
String pdfFileName = path + "/" + tmpFileName + ".pdf" ;
File htmlFile = new File(htmlFileName); //html文件
File pdfFile = new File(pdfFileName); //pdf文件
deleteFile(htmlFile, 1000 * 60 * 10);
deleteFile(pdfFile, 1000 * 60 * 10);
IOUtils.write(htmlText, new FileOutputStream(htmlFile)); //将内容写入html文件
String command = getCommand(htmlFileName , pdfFileName);
System.out.println(command);
Runtime.getRuntime().exec(command);
TimeUnit.SECONDS.sleep(3);
return new FileInputStream(pdfFile);
}
public static String getCommand(String htmlName , String pdfName){
String system = System.getProperty("os.name");
if("Windows 7".equalsIgnoreCase(system)){//xp系统
System.out.println("xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 ");
return "\"C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe\"" +" "+ htmlName + " " + pdfName;
}else if("Linux".equalsIgnoreCase(system)) //linux 系统
return "wkhtmltopdf --encoding utf-8 " + htmlName + " " + pdfName;
return "" ;
}
public static void deleteFile(final File file, final long time) {
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
file.delete();
}
}).start();
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
/**
* 生成PDF
*
*/
public class PDFTool {
public static InputStream generationPdf(HttpServletRequest request,String htmlText ) throws Exception{
String path = request.getSession().getServletContext().getRealPath("pdf");
System.out.println("原path: " + path);
//把路径中的反斜杠转成正斜杠
path = path.replaceAll("\\\\", "/"); //
String tmpFileName = UUID.randomUUID().toString(); //生成随机文件名
/* File dir = new File(tmpdir);
if(!dir.exists())
dir.mkdirs(); */
System.out.println(path +";tmpdirtmpdirtmpdirtmpdirtmpdirtmpdirtmpdirtmpdir;"+tmpFileName);
String htmlFileName = path +"/" + tmpFileName + ".html" ;
String pdfFileName = path + "/" + tmpFileName + ".pdf" ;
File htmlFile = new File(htmlFileName); //html文件
File pdfFile = new File(pdfFileName); //pdf文件
deleteFile(htmlFile, 1000 * 60 * 10);
deleteFile(pdfFile, 1000 * 60 * 10);
IOUtils.write(htmlText, new FileOutputStream(htmlFile)); //将内容写入html文件
String command = getCommand(htmlFileName , pdfFileName);
System.out.println(command);
Runtime.getRuntime().exec(command);
TimeUnit.SECONDS.sleep(3);
return new FileInputStream(pdfFile);
}
public static String getCommand(String htmlName , String pdfName){
String system = System.getProperty("os.name");
if("Windows 7".equalsIgnoreCase(system)){//xp系统
System.out.println("xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 xp系统 ");
return "\"C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe\"" +" "+ htmlName + " " + pdfName;
}else if("Linux".equalsIgnoreCase(system)) //linux 系统
return "wkhtmltopdf --encoding utf-8 " + htmlName + " " + pdfName;
return "" ;
}
public static void deleteFile(final File file, final long time) {
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
file.delete();
}
}).start();
}
}
开始一直用的是itext 样式一直存在问题,最好在网上找到一个wkhtmltopdf 工具,感觉还不错吧
安装wkhtmltopdf
window 版 我的系统是64位的 链接:http://pan.baidu.com/s/1bLcuHg 密码:xqei
linux 版本 安装命令:yum install wkhtmltopdf
(linux 可能有个中文问题 把window 的字体文件上传到linux 的/usr/share/fonts目录)