轉自:http://blog.csdn.net/is_zhoufeng/article/details/8244454
最近有個需求轉html爲pdf 。
用過itext 、 pd4ml ,都不理想,不是樣式有問題,就是頁面大小有問題。 或字體有問題。
解決辦法是通過wkhtmltopdf工具 , 下載地址爲:http://code.google.com/p/wkhtmltopdf/
(有windowx版本,和linux版本。) 解壓後是一個可執行文件。用法爲 dir/wkhtmltopdf htmlpath pdfpath
在linux上面,可以將該可執行文件 軟連接到path文件夾下面入: sudo ln -s /home/zhoufeng/wkhtmltopdf /usr/bin/wkhtmltopdf
package etds.report.util;
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.annotation.Resource;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import freemarker.template.Template;
/**
* 生成PDF,通過freemarker模板
* @author zf
*/
@Component("PDFTool")
public class PDFTool {
@Resource(name="webfreemarkerconfiguration")
private FreeMarkerConfigurer freemarkerconfiguration;
private final String dzorderftl = "pdftemplete/dzorder.ftl";
private static String tmpdir = PDFTool.class.getResource("/").getPath() + "tmpdir";
{
tmpdir = tmpdir.replaceAll("^/", "");
}
public InputStream generationPdfDzOrder(Map<String ,Object> params) throws Exception{
final Template template = freemarkerconfiguration.getConfiguration().getTemplate(dzorderftl);
String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, params);
String tmpFileName = UUID.randomUUID().toString(); //生成隨機文件名
File dir = new File(tmpdir);
if(!dir.exists())
dir.mkdirs();
String htmlFileName = tmpdir + "/" + tmpFileName + ".html" ;
String pdfFileName = tmpdir + "/" + tmpFileName + ".pdf" ;
File htmlFile = new File(htmlFileName); //html文件
File pdfFile = new File(pdfFileName); //pdf文件
IOUtils.write(htmlText, new FileOutputStream(htmlFile)); //將內容寫入html文件
String command = getCommand(htmlFileName , pdfFileName);
Runtime.getRuntime().exec(command);
TimeUnit.SECONDS.sleep(3);
return new FileInputStream(pdfFile);
}
public String getCommand(String htmlName , String pdfName){
String system = System.getProperty("os.name");
if("Windows XP".equalsIgnoreCase(system)) //xp系統
return "D:/Program Files/wkhtmltopdf/wkhtmltopdf.exe " + htmlName + " " + pdfName;
else if("Linux".equalsIgnoreCase(system)) //linux 系統
return "wkhtmltopdf-amd64 " + htmlName + " " + pdfName;
return "" ;
}
}
這是原博客作者給我的回覆,所以這是在框架中的用法,普通的用法很簡單,以下是我寫的普通用法,很簡單
public String getCommand(String htmlName , String pdfName){
return "C:\\Program_Files\\wkhtmltopdf\\wkhtmltopdf.exe "+ htmlName + " " + pdfName;//前半段是我的安裝路徑,根據自己的安裝路徑換上即可
}
public static void main(String[] args){
PDFToolTest pdfTool = new PDFToolTest();
String command = pdfTool.getCommand("http://www.baidu.com", "E:/test2.pdf");
System.out.println(command);
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
相信這個用法就不用多做解釋了,getCommand的第一個參數就是目標url,第二個就是目標pdf的路徑名。
補充一點注意的:GBK或者GB***中文會亂碼,但是utf-8不會亂碼。