tags:

views:

1472

answers:

9

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...

+2  A: 

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:

  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.

A: 

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.

Chris
A: 

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

marc_s
+3  A: 

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

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

+2  A: 

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.

crb
This product looks good, but it isn't really built well to be packaged with a .NET application. Looks like they have a .dll you can wrap at least.
I can't say I've tried - I've used it with MindTouch Deki, which calls the .exe directly. What I can say is the output is gorgeous - it's more like LaTeX than CutePDF from Internet Explorer :)
crb
+1  A: 

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

Gregory A Beamer
A: 

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

A: 

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.

A: 

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.

Constantine