Html转pdf(JAVA)
html转pdf⼯具类
/**
* HTML转PDF的⼯具类
*/
SystemPath(获取路径,需要⾃⼰处理下)
public class ConverterHTMLToPDF {
private static Logger logger = Logger(Name());
public void converterHTMLToPDF(String content, String myRandom)
{
String toPdfExeHome = "";
fig.ConfigXMLReader reader=new fig.ConfigXMLReader();
toPdfExeHome = Attribute("topdftools", "topdfhome");
String osName = Properties().getProperty("os.name").toUpperCase();
String htmlFile_temp_path = RootFilePath()+File.separator+"platform"+File.separator+"custom"+File.separator+"custom_form"+File.separator+"run"+File.separator+"export2html_temp.html"; htmlFile_temp_RootFilePath()+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.apachemons.adFileToString(new File(htmlFile_temp_path), "UTF-8");
} catch (IOException e) {
java修改html文件e.printStackTrace();
}
place("[htmlcontent]",content);
String RootFilePath()+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.apachemons.io.FileUtils.writeStringToFile(new File(htmlFile_path), fileContent, "UTF-8");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Runtime r = Runtime();
try {
Process pro = null;
if (osName.startsWith("WIN")){
pro = r.exec( toPdfExeHome+"\\ "+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 InputStream()).start();
new ErrorStream()).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 = "";
fig.ConfigXMLReader reader=new fig.ConfigXMLReader();
toPdfExeHome = Attribute("topdftools", "topdfhome");
String osName = Properties().getProperty("os.name").toUpperCase();
String htmlFile_path= AbsolutePath();
String pdfFile_path= AbsolutePath();
logger.debug("HTML的路径:"+htmlFile_path+",PDF⽂件路径"+pdfFile_path);
Runtime r = Runtime();
try {
Process pro = null;
if (osName.startsWith("WIN")){
pro = r.exec( toPdfExeHome+"\\ "+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 InputStream()).start();
new ErrorStream()).start();
try {
pro.waitFor();
} catch (InterruptedException e) { e.printStackTrace();
}
} catch (IOException eee) {
eee.printStackTrace();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论