html转pdf工具类
- /**
- * HTML转PDF的工具类
- */
SystemPath(获取路径,需要自己处理下)- public class ConverterHTMLToPDF {
- private static Logger logger = Logger.getLogger(ConverterHTMLToPDF.class.getName());
- public void converterHTMLToPDF(String content, String myRandom)
- {
- String toPdfExeHome = "";
- com.whir.component.config.ConfigXMLReader reader=new com.whir.component.config.ConfigXMLReader();
- toPdfExeHome = reader.getAttribute("topdftools", "topdfhome");
- String osName = System.getProperties().getProperty("os.name").toUpperCase();
- String htmlFile_temp_path = SystemPath.getRootFilePath()+File.separator+"platform"+File.separator+"custom"+File.separator+"custom_form"+File.separator+"run"+File.separator+"export2html_temp.html";
- htmlFile_temp_path=SystemPath.getRootFilePath()+File.separator+"platform"+File.separator+"custom"+File.separator+"custom_form"+File.separator+"run"+File.separator+"export2html_govtemp.html";
- logger.debug("临时HTML文件"+htmlFile_temp_path);
- String fileContent = "";
- try {
- fileContent = org.apache.commons.io.FileUtils.readFileToString(new File(htmlFile_temp_path), "UTF-8");
- } catch (IOException e) {
- e.printStackTrace();
- }
- fileContent=fileContent.replace("[htmlcontent]",content);
- String temppath=SystemPath.getRootFilePath()+File.separator+"upload"+File.separator+"tempfile";
- String htmlName=myRandom+".html";
- String pdfName=myRandom+".pdf";
- //pdfName="export2pdf_temp.pdf";
- String htmlFile_path=temppath+File.separator+"temphtml"+File.separator+htmlName;
- String pdfFile_path=temppath+File.separator+"temppdf"+File.separator+pdfName;
- logger.debug("HTML的路径:"+htmlFile_path+",PDF文件路径"+pdfFile_path);
- try {
- org.apache.commons.io.FileUtils.writeStringToFile(new File(htmlFile_path), fileContent, "UTF-8");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Runtime r = Runtime.getRuntime();
- try {
- Process pro = null;
- if (osName.startsWith("WIN")){
- pro = r.exec( toPdfExeHome+"\\wkhtmltopdf.exe "+htmlFile_path+" "+pdfFile_path);
- }else{
- logger.debug("linux tttt:"+toPdfExeHome+File.separator+"wkhtmltopdf "+htmlFile_path+" "+pdfFile_path);
- pro = r.exec( toPdfExeHome+File.separator+"wkhtmltopdf "+htmlFile_path+" "+pdfFile_path);
- }
- new DoOutput(pro.getInputStream()).start();
- new DoOutput(pro.getErrorStream()).start();
- try {
- pro.waitFor();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } catch (IOException eee) {
- eee.printStackTrace();
- }
- }
/**
* 把自定义流程的html转换成pdf
* @param htmlFile html文件
* @param pdfFile pdf文件
*/
public void converterArchivesFileHTMLToPDF(File htmlFile,File pdfFile)
{
String toPdfExeHome = "";
com.whir.component.config.ConfigXMLReader reader=new com.whir.component.config.ConfigXMLReader();
toPdfExeHome = reader.getAttribute("topdftools", "topdfhome");
String osName = System.getProperties().getProperty("os.name").toUpperCase();
String htmlFile_path= htmlFile.getAbsolutePath();
String pdfFile_path= pdfFile.getAbsolutePath();
logger.debug("HTML的路径:"+htmlFile_path+",PDF文件路径"+pdfFile_path);
Runtime r = Runtime.getRuntime();
try {
Process pro = null;
if (osName.startsWith("WIN")){
pro = r.exec( toPdfExeHome+"\\wkhtmltopdf.exe "+htmlFile_path+" "+pdfFile_path);
}else{
logger.debug("linux tttt:"+toPdfExeHome+File.separator+"wkhtmltopdf "+htmlFile_path+" "+pdfFile_path);
pro = r.exec( toPdfExeHome+File.separator+"wkhtmltopdf "+htmlFile_path+" "+pdfFile_path);
}
new DoOutput(pro.getInputStream()).start();
new DoOutput(pro.getErrorStream()).start();
try {
pro.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException eee) {
eee.printStackTrace();
}
}
以上就是Html转pdf(JAVA)的详细内容,更多关于Html转pdf(JAVA)的资料请关注九品源码其它相关文章!