html转pdf与linux安装wkhtmltox
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.Semaphore;
public class Html2pdfUtil {
private static Logger logger = LoggerFactory.getLogger(Html2pdfUtil.class);
private static String wkhtmltopdfLocaton = "wkhtmltopdf";
/**
* html 转pdf
* time 2017.10.19 21:06
* @param htmlUrl
* @param pdfFilePath
* @return
*/
public static String Html2pdf(final String htmlUrl ,final String pdfFilePath) {
//linux版本
//final String whktmlExe = "wkhtmltopdf";
//windows版本
//final String whktmlExe = "D:\\htmlToPdf\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
final String whktmlExe = wkhtmltopdfLocaton;
// 只能5个线程同时访问
final Semaphore semaphore = new Semaphore(5);
try {
System.out.println(" inside for before " + semaphore.availablePermits());
// 获取许可
semaphore.acquire();
System.out.println(" inside for after " + " ---" + htmlUrl);
new Thread() {
@Override
public void run() {
try {
ProcessBuilder pb = new ProcessBuilder(whktmlExe, "--print-media-type", "--margin-left", "0mm", "--margin-right", "0mm", htmlUrl, pdfFilePath); // 使用print样式
sleep(1000);
pb.redirectErrorStream(true);
Process process = pb.start();
BufferedReader errStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = errStreamReader.readLine();
while (line != null) {
System.err.println(line); //or whatever else
line = errStreamReader.readLine();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("in finally releasing " + semaphore.availablePermits());
// 访问完后,释放
semaphore.release();
}
}
}.start();
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(" *** Error in pdf generation *** ");
return "error";
}
return "success";
}
public static String Html2pdf1(final String htmlUrl ,final String pdfFilePath) {
//linux版本
//final String whktmlExe = "wkhtmltopdf";
//windows版本
//final String whktmlExe = "D:\\htmlToPdf\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
String wkhtmltopdfLocaton = "";
final String whktmlExe = wkhtmltopdfLocaton;
run(whktmlExe,htmlUrl,pdfFilePath);
return "success";
}
public static void run(String whktmlExe,String htmlUrl,String pdfFilePath) {
try {
logger.info("1.进来");
ProcessBuilder pb = new ProcessBuilder(whktmlExe, "--print-media-type", "--margin-left", "0mm", "--margin-right", "0mm", htmlUrl, pdfFilePath); // 使用print样式
logger.info("2.");
pb.redirectErrorStream(true);
Process process = pb.start();
logger.info("3.");
int exitVal = process.waitFor();
logger.info("4." + exitVal);
BufferedReader errStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = errStreamReader.readLine();
while (line != null) {
System.err.println(line); //or whatever else
line = errStreamReader.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void run1(String whktmlExe,String htmlUrl,String pdfFilePath) {
try {
logger.info("1.进来");
Process project = Runtime.getRuntime().exec(whktmlExe + " --print-media-type --margin-left 0mm --margin-right 0mm "+ htmlUrl +" " + pdfFilePath);// 使用print样式
logger.info("2.");
int exitVal = project.waitFor();
logger.info("4." + exitVal);
project.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上是java工具类代码
下面是需要安装的插件linux安装wkhtmltox:
wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
tar xvfJ wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
sudo mv ./wkhtmltox/bin/wkhtmltopdf /usr/bin/wkhtmltopdf
sudo chmod +x /usr/bin/wkhtmltopdf
sudo cp /usr/bin/wkhtmltopdf /usr/local/bin/wkhtmltopdf
解决中文不显示的问题
网络上下载宋体(simsun.ttc,simsun.ttf)文件
上传到服务器的 /usr/share/fonts/chinese/TrueType/ 文件夹中(没有这些文件夹新建即可)
cd /usr/share/fonts/chinese/TrueType/
cd /usr/share/fonts
mkdir chinese
cd chinese
mkdir TrueType
cd TrueType
测试
wkhtmltopdf ww.baidu.com ./test.pdf
转载:https://www.cnblogs.com/fqybzhangji/p/11175563.html
浙公网安备 33010602011771号