wkhtmltopdf 网页转PDF

Wu?? 2017-01-06 10:09:34
动态网页,带背景图和图片。现在只能转出文本内容。
...全文
228 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_43709033 2020-05-13
  • 打赏
  • 举报
回复
package com.htmlToPdf.cn; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.util.UUID; class wkthmltopdf { // wkhtmltopdf在系统中的路径 // private static final String toPdfTool = "D:\\wkhtmltox-0.12.5-1.mxe-cross-win64\\bin\\wkhtmltopdf.exe"; /** * html转pdf * * @param srcPath * html路径,可以是硬盘上的路径,也可以是网络路径 * @param destPath * pdf保存路径 * @return 转换成功返回true */ public static boolean convert(String srcPath, String destPath) throws IOException { Process ps = Runtime.getRuntime().exec("where wkhtmltopdf.exe"); BufferedReader br = null; br = new BufferedReader(new InputStreamReader(ps.getInputStream(), Charset.forName("GBK"))); //wkhtmltopdf在系统中的路径 String path =""; String text =""; while ((text = br.readLine())!=null){ path =text; } File file = new File(destPath); File parent = file.getParentFile(); // 如果pdf保存路径不存在,则创建路径 if (!parent.exists()) { //true代表拥有读写权限 file.setWritable(true); parent.mkdirs(); } StringBuilder cmd = new StringBuilder(); if (!System.getProperty("os.name").contains("Windows")) { // 非windows 系统 //toPdfTool = FileUtil.convertSystemFilePath("/home/ubuntu/wkhtmltox/bin/wkhtmltopdf"); } cmd.append(path); cmd.append(" "); //页眉下面的线 cmd.append(" --header-line"); //设置页面上的边距 cmd.append(" --margin-top 3cm "); // (设置页眉和内容的距离,默认0) cmd.append(" --header-spacing 5 "); //设置在中心位置的页脚内容 cmd.append(" --footer-center 第[page]页/共[topage]页"); // * 显示一条线在页脚内容上) cmd.append(" --footer-line"); // (设置页脚和内容的距离) cmd.append(" --footer-spacing 5 "); //要转换的URL链接地址 cmd.append(srcPath); cmd.append(" "); //pdf存放路径 cmd.append(destPath); boolean result = true; try { Process proc = Runtime.getRuntime().exec(cmd.toString()); HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream()); HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream()); error.start(); output.start(); proc.waitFor(); } catch (Exception e) { System.out.println("pdf转换出错"); result = false; e.printStackTrace(); } return result; } public static void main(String[] args) throws IOException { wkthmltopdf.convert("https://www.baidu.com", "D:\\test201905\\13.pdf"); } } 亲测可用
assky124 2019-10-10
  • 打赏
  • 举报
回复
cef 直接保存为PDF
qq_16805359 2019-10-10
  • 打赏
  • 举报
回复
九云图文档转换接口, 支持word转pdf、pdf转网页、ppt转pdf、html转pdf、html转pdf等各种格式的文档转换,保留原有格式和布局,矢量缩放,简单调用即可轻松实现在线转换。想体验的可以到九云图官网看下
Jeffery Chan 2017-06-23
  • 打赏
  • 举报
回复
楼主教教我
Wu?? 2017-01-06
  • 打赏
  • 举报
回复
string fileNameWithOutExtention = HttpContext.Current.Server.MapPath("..\Content\pdf\" + Guid.NewGuid() + ".pdf"); string wkhtmltopdfPath = HttpContext.Current.Server.MapPath("..\App_Start\wkhtmltopdf.exe"); if (string.IsNullOrEmpty(html)) { return "false"; } ProcessStartInfo si; StringBuilder paramsBuilder = new StringBuilder(); paramsBuilder.Append("--page-size A4 "); paramsBuilder.AppendFormat("\"{0}\" \"{1}\"", "-", fileNameWithOutExtention); si = new ProcessStartInfo(); si.CreateNoWindow = true; si.FileName = wkhtmltopdfPath; si.Arguments = paramsBuilder.ToString(); si.UseShellExecute = false; si.RedirectStandardError = true; si.RedirectStandardInput = true; using (var process = new Process()) { process.StartInfo = si; process.Start(); using (var stream = process.StandardInput) { byte[] buffer = Encoding.UTF8.GetBytes(html); stream.BaseStream.Write(buffer, 0, buffer.Length); stream.WriteLine(); } process.WaitForExit(); } if (File.Exists(fileNameWithOutExtention)) { //把文件读进文件流 FileStream fs = new FileStream(fileNameWithOutExtention, FileMode.Open); byte[] file = new byte[fs.Length]; fs.Read(file, 0, file.Length); fs.Close(); } else { throw new Exception("文件不存在!"); } return fileNameWithOutExtention;
Wu?? 2017-01-06
  • 打赏
  • 举报
回复
引用 1 楼 qq_33035717的回复:
求助大神,急。代码如下:
有没有,有没有人回答我啊啊啊
Wu?? 2017-01-06
  • 打赏
  • 举报
回复
求助大神,急。代码如下:

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧