Generate PDF from ASP.NET from raw HTML/CSS conten

2020-02-05 04:10发布

I'm sending emails that have invoices attached as PDFs. I'm already - elsewhere in the application - creating the invoices in an .aspx page. I'd like to use Server.Execute to return the output HTML and generate a PDF from that. Otherwise, I'd have to use a reporting tool to "draw" the invoice on a PDF. That blows for lots of reasons, not the least of which is that I'd have to update both the .aspx page and the report for every minor change. What to do...

11条回答
叛逆
2楼-- · 2020-02-05 04:19

As long as you can make sure to use proper XHTML, you could also use a product like Alt-Soft's Xml2PDF to convert XML (XHTML) into PDF by means of XSLT/XSL-FO.

It takes a bit of a learning curve to master, but it works very well once you've "got" it!

Marc

查看更多
可以哭但决不认输i
3楼-- · 2020-02-05 04:23

I'd go a different route. Assuming you are using SQL Server, use SSRS and generate the PDF that way.

查看更多
趁早两清
4楼-- · 2020-02-05 04:25

We use a product called ABCPDF for this and it works fantastic.

http://www.websupergoo.com/abcpdf-1.htm

查看更多
不美不萌又怎样
5楼-- · 2020-02-05 04:26

wkhtmltopdf is a free and cool exe to generate pdf from html. Its written in c++. But nReco htmltopdf is a wrapper dotnet library for this awesome tool. I implemented using this dotnet library and it was just so good it does everything by its own you just need to give html as a data source.

/// <summary>
/// Converts html into PDF using nReco dll and wkhtmltopdf.exe.
/// </summary>       
private byte[] ConvertHtmlToPDF()
{
  HtmlToPdfConverter nRecohtmltoPdfObj = new HtmlToPdfConverter();
  nRecohtmltoPdfObj.Orientation = PageOrientation.Portrait;
  nRecohtmltoPdfObj.PageFooterHtml = CreatePDFFooter();
  nRecohtmltoPdfObj.CustomWkHtmlArgs = "--margin-top 35 --header-spacing 0 --margin-left 0 --margin-right 0";           
  return nRecohtmltoPdfObj.GeneratePdf(CreatePDFScript() + ShowHtml() + "</body></html>");
}

The above function is an excerpt from the below link post which explains it in detail. HTML to PDF in ASP.Net

查看更多
Lonely孤独者°
6楼-- · 2020-02-05 04:29

If you try to find some html to pdf software via GOOGLE you'll get a pile of this stuff. There are about 10 leaders but most of them use IE dlls in background mode. Just couple of them use their own parsing engine. Please try PDF Duo .NET component in your ASP.NET project if you wish to create a PDF programaticaly. It is light component for a cool generating of PDF invoces, reports e.g.

查看更多
Bombasti
7楼-- · 2020-02-05 04:32

There is no way to generate a PDF from an HTML string directly within .NET, but there are number of third party controls that work well.

I've had success with this one: http://www.html-to-pdf.net and this: http://www.htmltopdfasp.net

The important questions to ask are:

  1. Does it render correctly as compared to the 3 major browsers: IE, FF and Safari/Chrome?
  2. Does it handle CSS fine?
  3. Does the control have it's own rendering engine? If so, bounce it. You don't want to trust a home grown rendering engine - the browsers have a hard enough problem getting everything pixel perfect.
  4. What dependencies does the third party control require? The fewer, the better.

There are a few others but they deal with ActiveX displays and such.

查看更多
登录 后发表回答