html转pdf
作者:互联网
html转pdf,多网页拼接
1.由于公司业务需要,想要将一些网页进行组装封卷。原有的html2pdfjs 在网页过多或者404下会报错无法使用。
2.寻找后台转换工具 wkhtmltopdf 官网链接
根据需要下载自己需要的版本(windows,centos)
3.将需要转换网页链接转为数组,利用URL。openStream判断网页是否可以访问,避免在转换过程中出现错误。
4.利用IText包里pdf拼接方法对转换好的pdf进行拼接
5.最后对生成临时文件进行删除(避免占用服务器存储空间)
String htmltext="1.html,2.html";
String[] paths=htmltext.split(",");
String pdfpaths="";
String tmppath=System.getProperty("java.io.tmpdir")+"\\pdfsave\\";
for (int i=0;i<paths.length;i++){
if(!"".equals(paths[i])&&paths[i]!=null){
String pdfpath=tmppath+ Utils.uuid()+".pdf";
if (!new File(tmppath).isDirectory()) {
boolean b=new File(tmppath).mkdirs();
System.out.println("新建上传临时文件夹");
}
if(checkUrlContent(paths[i])){
htmlToPdf(paths[i],pdfpath);
if(pdfpaths.equals("")){
pdfpaths=pdfpath;
}else {
pdfpaths=pdfpaths+","+pdfpath;
}
}else {
continue;
}
}
}
String[] all=pdfpaths.split(",");
String pdfpath=System.getProperty("java.io.tmpdir")+"\\pdfsave\\"+ Utils.uuid()+".pdf";
boolean a=mergePdfFiles(all,pdfpath);
if(a){
DownloadFileUtils.getExcel(pdfpath,"卷宗.pdf",response);
File f=new File(pdfpath);
f.delete();
}
}
public static void htmlToPdf(String htmpath,String pdfpath){
try{
Process process=Runtime.getRuntime().exec(getCommand(htmpath,pdfpath));
//调用cmd命令根据自己方式写 我写的不一定适用(getCommand)
process.waitFor();
}catch(Exception e){
throw new RuntimeException;
}
}
public static boolean checkUrlContent(String urlString){
long lo=System.currentTimeMillis();
URL url;
try {
url=new URL(urlString);
InputStream in=url.openStream();
return true;
} catch (Exception e) {
return false;
}
}
public static boolean mergePdfFiles(String[] files, String newfile) {
boolean retValue = false;
Document document = null;
try {
PdfReader reader1 = new PdfReader(files[0]);
document = new Document(reader1.getPageSize(1));
PdfCopy copy = new PdfCopy(document, new FileOutputStream(newfile));
document.open();
for (int i = 0; i < files.length; i++) {
PdfReader reader = new PdfReader(files[i]);
int n = reader.getNumberOfPages();
for (int j = 1; j <= n; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
reader.close();
}
reader1.close();
copy.close();
retValue = true;
} catch (Exception e) {
System.out.println(e);
} finally {
System.out.println("执行结束");
document.close();
for (int i=0;i<files.length;i++){
File f=new File(files[i]);
f.delete();
}
}
return retValue;
}
标签:pdfpath,String,System,html,new,pdf,document 来源: https://blog.csdn.net/qq_38069781/article/details/113973153