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...
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
The important questions to ask are:
- Does it render correctly as compared to the 3 major browsers: IE, FF and Safari/Chrome?
- Does it handle CSS fine?
- 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.
- 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.
This is probably a long shot, but what about loading the HTML into a WebBrowser control, then (somehow) getting a Bitmap of what the control rendered, and converting the bitmap image to an image as they're represented in PDFs?
The exact rendering will of course be tied to IE's engine, but it may be worth looking into.
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
We use a product called ABCPDF for this and it works fantastic.
This sounds like a job for Prince. It can take HTML and CSS and generate a PDF, which you can then present to your users. It supports CSS3 better than most web browsers (staff include Håkon Wium Lie, the inventor of CSS).
See the samples, especially the ones for Wikipedia pages, for the beautiful output it can generate. There's also an interesting Google Tech Talk with the authors.
Edit: There is a .NET wrapper available.
Since you are producing the answer, you can use a tool like Report.NET: http://sourceforge.net/projects/report/
I disagree with the answers that say you cannot convert directly from output to PDF, however, as you can "re-call" the page and get the HTML as a stream and convert it. I am not sure what tool you would want to use to do this, however. In other words, it is possible, but I am not sure it is worth it. The PDF creation libs, like Report.NET, even though they force reusing some logic and no automagic converrsion, it is easier.
I have not tried this component, but I have heard good things about it from those who have. The model is more like HTML, but I am not sure you can simply send a rendered ASPX to it to create PDF: http://www.websupergoo.com/abcpdf-8.htm
Hello everybody, I have tried with the solutions that you have provided, but until the moment I have not found a precise solution, I need to export an auto generated aspx page to format pdf. How could I do it using itextsharp or report.net? I can’t find in its examples like being able to do it.
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.