这篇文章主要介绍了c#免费组件html转pdf怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇c#免费组件html转pdf怎么实现文章都会有所收获,下面我们一起来看看吧。
如上有Select.HtmlToPdf和 Select.HtmlToPdf.netcore,两种的使用差不多,只是Select.HtmlToPdf.netcore支持css效果更好,不过Select.HtmlToPdf.netcore只支持win,不支持linux,这个有点坑,其他还好,接下来我们使用Select.HtmlToPdf.netcore进行演示
static void Main(string[] args)
{
try
{
string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "文件夹", "文件夹下的html文件");
string line = "";
var testStr = new StringBuilder();
using (StreamReader sr = new StreamReader(fullPath))
{
while ((line = sr.ReadLine()) != null)
{
testStr.Append(line);
}
}
SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf();
PdfDocument doc = new PdfDocument();
for (int i = 0; i < 10; i++)
{
testStr.Replace("#ImageUrl#", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "文件夹", "文件夹下的图片"));//由于html中图片,使用相对地址解析不出来,所以使用替换方式去解决
var docStr = converter.ConvertHtmlString(testStr.ToString());
doc.Append(docStr);
}
doc.Save("xxxx");保存到xxx路径下
doc.Close();
}
catch (Exception e)
{
//dosomething
}
Console.ReadLine();
}
}
如上一次性打印多张pdf,思路:
1.在本地找到要转换的html文件,当然你也可以配置在程序里面,通过流的形式读出来,也可用file的方法去读,拿到html字符串
2.创建一个html转pdf的对象,创建一个新的pdf文件对象
3.通过html转pdf对象的converthtmlstring去获取html字符串,另外还提供converurl的方法去把一个网页转换换成pdf,是不是很方便切功能强大。
4.save用来保存pdf的路径,关闭pdf对象,操作文成,即可看到
这样就是实现了html 转pdf,另外,这个组件还提供了很多api可用
public string HtmlToPdf(string url)
{
bool success = true;
// string dwbh = url.Split('?')[1].Split('=')[1];
//CommonBllHelper.CreateUserDir(dwbh);
//url = Request.Url.Host + "/html/" + url;
string guid = DateTime.Now.ToString("yyyyMMddhhmmss");
string pdfName = "1.pdf";
//string path = Server.MapPath("~/kehu/" + dwbh + "/pdf/") + pdfName;
string path = "D:\\" + pdfName;
try
{
if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path))
success = false;
string str = Server.MapPath("~\\bin\\wkhtmltopdf.exe");
Process p = System.Diagnostics.Process.Start(str, url+" "+path);
p.WaitForExit();
if (!System.IO.File.Exists(str))
success = false;
if (System.IO.File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
if (Request.UserAgent != null)
{
string userAgent = Request.UserAgent.ToUpper();
if (userAgent.IndexOf("FIREFOX", StringComparison.Ordinal) <= 0)
{
Response.AddHeader("Content-Disposition",
"attachment; filename=" + HttpUtility.UrlEncode(pdfName, Encoding.UTF8));
}
else
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName);
}
}
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
fs.Close();
System.IO.File.Delete(path);
}
else
{
Response.Write("文件未找到,可能已经被删除");
Response.Flush();
Response.End();
}
}
catch (Exception ex)
{
success = false;
}
return "";
}
protected void Page_Load(object sender, EventArgs e)
{
HtmlToPdf("http://www.deriva.cn");
}
关于“c#免费组件html转pdf怎么实现”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“c#免费组件html转pdf怎么实现”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
亿速云免费服务器限时活动,即开即用、新—代英特尔至强铂金CPU、SSD云盘,0元免费领,库存有限,领完即止! 点击查看>>点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。