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;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值