Does anyone know how to get a html content, convert it to pdf and save it to database?
有人知道如何获取html内容,将其转换为pdf并保存为数据库吗?
I've tried so many ways, but nothing seems to work. In some articles, it's written to use HTMLParse, in others HTMLWorker... sometimes throws an error "document has no pages"... sometimes, it just throws an exception but it didn't specify the error...
我尝试了很多方法,但似乎没有效果。在某些文章中,它被编写为使用HTMLParse,在其他文章中……有时抛出一个错误“文档没有页面”……有时,它只是抛出一个异常,但没有指定错误……
Does anyone know a good way to do this?
有人知道这样做的好方法吗?
I'm using C# 3.0, ASP.NET MVC and LinQToSQL.
我用的是c# 3.0, ASP。净MVC和LinQToSQL。
Thanks in advance and a lot!!
非常感谢您的帮助!!
2 个解决方案
#1
2
For HTML to PDF i tend to look at HTMLDoc. It's an exe and not a library, so it may not be exactly what you are looking for, but it'll probably get you over the hump.
对于HTML到PDF,我倾向于看HTMLDoc。它是一个exe而不是一个库,所以它可能不是你想要的,但它可能会让你渡过难关。
Looking at the iText book, Lowagie says:
看着iText的书,Lowagie说:
"One of the frequent questions on the iText mailing list is, 'Does iText provide HTML2PDF functionality?' The official answer is no; you're advised to use HtmlDoc or ICEBrowser." Page 456.
“iText邮件列表中经常出现的问题之一是,‘iText是否提供HTML2PDF功能?官方的答案是否定的;建议您使用HtmlDoc或ICEBrowser。456页。
He then goes on to show an example of HTMLParser, but stresses it's not up to the huge job of parsing and presenting HTML.
然后,他接着展示了HTMLParser的一个示例,但强调它并不取决于解析和呈现HTML的巨大工作。
#2
0
public class Pdf : IPdf
{
public FileStreamResult Make(string s)
{
using (var ms = new MemoryStream())
{
using (var document = new Document())
{
PdfWriter.GetInstance(document, ms);
document.Open();
using (var str = new StringReader(s))
{
var htmlWorker = new HTMLWorker(document);
htmlWorker.Parse(str);
}
document.Close();
}
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=MyPdfName.pdf");
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
HttpContext.Current.Response.OutputStream.Flush();
HttpContext.Current.Response.End();
return new FileStreamResult(HttpContext.Current.Response.OutputStream, "application/pdf");
}
}
}
#1
2
For HTML to PDF i tend to look at HTMLDoc. It's an exe and not a library, so it may not be exactly what you are looking for, but it'll probably get you over the hump.
对于HTML到PDF,我倾向于看HTMLDoc。它是一个exe而不是一个库,所以它可能不是你想要的,但它可能会让你渡过难关。
Looking at the iText book, Lowagie says:
看着iText的书,Lowagie说:
"One of the frequent questions on the iText mailing list is, 'Does iText provide HTML2PDF functionality?' The official answer is no; you're advised to use HtmlDoc or ICEBrowser." Page 456.
“iText邮件列表中经常出现的问题之一是,‘iText是否提供HTML2PDF功能?官方的答案是否定的;建议您使用HtmlDoc或ICEBrowser。456页。
He then goes on to show an example of HTMLParser, but stresses it's not up to the huge job of parsing and presenting HTML.
然后,他接着展示了HTMLParser的一个示例,但强调它并不取决于解析和呈现HTML的巨大工作。
#2
0
public class Pdf : IPdf
{
public FileStreamResult Make(string s)
{
using (var ms = new MemoryStream())
{
using (var document = new Document())
{
PdfWriter.GetInstance(document, ms);
document.Open();
using (var str = new StringReader(s))
{
var htmlWorker = new HTMLWorker(document);
htmlWorker.Parse(str);
}
document.Close();
}
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=MyPdfName.pdf");
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
HttpContext.Current.Response.OutputStream.Flush();
HttpContext.Current.Response.End();
return new FileStreamResult(HttpContext.Current.Response.OutputStream, "application/pdf");
}
}
}