public static String WKHtmlToPdf(String wktype,String formUrl,String outPath,int width,int height,double top,double right,double bottom,double left,double zoomscale,boolean isbd,boolean ishx) {
String opath = "";
Database zhsl_db = new Database();
Process process =null;
if (wktype.equals("pdf")) {
opath = "D:\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
if (System.getProperty("os.name").indexOf("Windows") == -1) {
opath = zhsl_db.firstOrDefault(String.class, "select DEFAULT_VALUE from st_parm where name='wk_path'");
}
int width = 588;
int height = 830;
double zoomscale = 1.30f;
StringBuilder cmd = new StringBuilder();
String w =String.valueOf(width * zoomscale);
String h = String.valueOf(height * zoomscale);
String zoom = String.valueOf(zoomscale);
cmd.append(opath);
cmd.append(" ");
cmd.append(" --margin-left "+left+"mm ");
cmd.append(" --margin-top "+top+"mm ");
cmd.append(" --margin-right "+right+"mm ");
cmd.append(" --margin-bottom "+bottom+"mm ");
cmd.append(" --page-width "+w+"px ");
cmd.append(" --page-height "+h+"px ");
cmd.append(" --zoom "+zoom+" ");
if (!isbd) {
cmd.append(" --custom-header Host "+" "+ip.split("http://")[1]);
cmd.append(" --custom-header Origin "+" "+ip);
cmd.append(" --custom-header Referer "+" "+formUrl);
}
if (ishx) {
cmd.append(" --orientation Landscape ");
}
cmd.append(" ");
cmd.append(formUrl);
cmd.append(" ");
cmd.append(outPath);
try {
process = Runtime.getRuntime().exec(cmd.toString());
ProcessRun error = new ProcessRun(process.getErrorStream());
ProcessRun output = new ProcessRun(process.getInputStream());
error.start();
output.start();
process.waitFor();
return outPath;
} catch (Exception e) {
AtLogManager.getInstance().debug("", "ycsslpt", "pdf转换出错:"+e.getMessage());
e.printStackTrace();
return "使用wkhtmltopdf出错:"+e.getMessage();
}finally {
if(process!=null) process.destroy();
}
} else {
opath = "D:\\wkhtmltopdf\\bin\\wkhtmltoimage.exe";
if (System.getProperty("os.name").indexOf("Windows") == -1) {
opath = zhsl_db.firstOrDefault(String.class, "select DEFAULT_VALUE from st_parm where name='wk_pathpng'");
}
StringBuilder cmd = new StringBuilder();
String w =String.valueOf(width * zoomscale);
String h = String.valueOf(height * zoomscale);
String zoom = String.valueOf(zoomscale);
cmd.append(opath);
cmd.append(" ");
if (!isbd) {
cmd.append(" --custom-header Host "+" "+ip.split("http://")[1]);
cmd.append(" --custom-header Origin "+" "+ip);
cmd.append(" --custom-header Referer "+" "+formUrl);
}
cmd.append(" ");
cmd.append(formUrl);
cmd.append(" ");
cmd.append(outPath);
try {
process = Runtime.getRuntime().exec(cmd.toString());
ProcessRun error = new ProcessRun(process.getErrorStream());
ProcessRun output = new ProcessRun(process.getInputStream());
error.start();
output.start();
process.waitFor();
return outPath;
} catch (Exception e) {
AtLogManager.getInstance().debug("", "ycsslpt", "pdf转换出错:"+e.getMessage());
e.printStackTrace();
return "使用wkhtmltopdf出错:"+e.getMessage();
}finally {
process.destroy();
}
}
}
package antu.bdcdj.task.action;
import antu.bdcdj.tool.DebugUtil;
import antu.log.AtLogManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ProcessRun extends Thread{
Logger logger = LogManager.getLogger(this.getClass().getName());
private InputStream is;
public ProcessRun(InputStream is){
this.is = is;
}
public void run(){
InputStreamReader isr =null;
try {
isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String line;
while ((line=br.readLine()) != null){
logger.debug(line);
}
}catch (Exception e) {
AtLogManager.getInstance().error("","",e.getMessage(),e);
e.printStackTrace();
}finally {
if(isr!=null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}