java 转pdf 源码
来源:互联网 发布:服务器和客户端端口 编辑:程序博客网 时间:2024/06/07 01:43
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目录)
0 0
- java 转pdf 源码
- java生成pdf测试源码
- PDF转TXT文件源码
- html字符串转pdf源码
- Java Html 转Pdf
- Java Word转Pdf
- Java Html 转Pdf
- java pdf 转 swf
- Java html转PDF
- java html转pdf
- Java HTML 转 PDF
- office转pdf java
- Java Word 转 Pdf
- java玩转pdf
- java excle转pdf
- java word转pdf
- Java html转PDF
- java pdf转png
- CSS绘制三角形
- IOS实现图片的模糊效果
- 网络基础2
- 面向协议的MVVM
- 关于android studio与eclipse的比较
- java 转pdf 源码
- 友盟分享
- android在activity内禁止状态栏下拉
- Android APP设计教程与终极UI适配秘籍【图文版】
- Swift 算法实战之路:链表
- 解决ScrollView下嵌套ListView、GridView显示不全的问题
- jQuery根据元素值删除数组元素的方法
- 如何查看进程的线程数?
- Hbase 学习笔记二 》hbase with MR example