社区
C#
帖子详情
wkhtmltopdf 网页转PDF
Wu??
2017-01-06 10:09:34
动态网页,带背景图和图片。现在只能转出文本内容。
...全文
237
7
打赏
收藏
wkhtmltopdf 网页转PDF
动态网页,带背景图和图片。现在只能转出文本内容。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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
打赏
举报
回复
求助大神,急。代码如下:
wkhtmlto
pdf
Demo
网页
转
pdf
wkhtmlto
pdf
Demo
网页
转
pdf
wkhtmlto
pdf
最好用的
转
网页
转
PDF
工具64bit版本,调用非常简单
最好用的
网页
转
PDF
工具,包含
转
PDF
和
转
图片2个工具,只需一个批处理调用或者类DOS命令即可,本版本运行稳定已经
转
过上百万计的文件。
wkhtmlto
pdf
通过
wkhtmlto
pdf
免费组件,完成Html向
PDF
转
换。 该组件的下载地址:http://code.google.com/p/
wkhtmlto
pdf
/ 该组件既可以通过如下方式,直接将
网页
内容
转
换为
PDF
:
wkhtmlto
pdf
"http://www.verycd.com/", "verycd.
pdf
" 需要注意的是:使用
wkhtmlto
pdf
时,
PDF
保存的文件夹不能有非Ansi字符,如中文、日文等,且
转
换gb2312、韩文charset、日文charset等非utf-8\ansi等
网页
时,会出现乱码 http://www.cnblogs.com/luqingfei/archive/2010/12/03/1895503.html
wkhtmlto
pdf
导出
pdf
导出
网页
到
pdf
工具。。 public static bool HtmlTo
Pdf
(string url, string path) { try { if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path)) return false; Process p = new Process(); string str = System.Web.HttpContext.Current.Server.MapPath("~/
wkhtmlto
pdf
.exe"); if (!System.IO.File.Exists(str)) return false; string switches = "--print-media-type "; switches += "--margin-top 0mm --margin-bottom 4mm --margin-right 0mm --margin-left 0mm "; switches += "--page-size A4 "; // switches += "--no-background "; switches += "--redirect-delay 100%"; //p.SessionId == "1"; p.StartInfo.FileName = str; p.StartInfo.Arguments = switches + " " + url + " " + path; //p.StartInfo.Arguments = " \"" + url + "\" " + path; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); System.Threading.Thread.Sleep(800); // p.SessionId p.WaitForExit(); p.Close(); return true; } catch (Exception ex) { HttpContext.Current.Response.Write(ex); } return false; }
URL
转
PDF
/ HTML
转
PDF
实现一个
转
pdf
的功能。 phantomjs 相比
wkhtmlto
pdf
体积小,
转
换完整度高。 phantomjs 相比 IText ,支持URL
转
换
PDF
、
转
换完整度高。 经过比较,我觉得phantomjs是
转
化比较好的。 以上提供一个简易的demo,实现URL或HTML
转
换为
PDF
文件。
C#
110,539
社区成员
642,577
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章