views:

332

answers:

3

We currently send an email notification in plain text or html format. Our environment is C#/.NET/SQL Server.

I'd like to know if anyone recommends a particular solution. I see two ways of doing this:

  • dynamically convert current email to pdf using a third party library and sending the pdf as an attachment

or

  • use SSRS to allow users to export pdf report (could eventually have SSRS push reports)

I'm open to third party libraries (especially if they are open source and free). It seems that SSRS is the simplest and easiest way to go. Anyone have any tips?

A: 

iText-Sharp
Works very much like the java version, and has both great documentation and multiple books available.

Joel Meador
in iText-Sharp, what would be the syntax for taking an html div and converting it to a pdf? Is it a single method call or do I have to recreate the document?
mson
A: 

Ugh. I loathe PDFs. That being said, what's wrong with your current plain text or HTML emails? PDFs are inefficient and rather large file formats.

GregD
It's much easier to provide consistent formatting in a pdf, for one thing. No idea what the author's answer is, of course. :)
Joel Meador
my preference is for plain text. but they want what they want... i loathe pdfs...
mson
+2  A: 

You can use iTextSharp to convert your html pages to pdf. Here's an example:

class Program
{
    static void Main(string[] args)
    {
        string html = 
@"<html>
<head>
  <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />
</head>
<body>
  <p style=""color: red;"">Hello World</p>
</body>
</html>";

        Document document = new Document(PageSize.A4);
        using (Stream output = new FileStream("out.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
        using (StringReader htmlReader = new StringReader(html))
        using (XmlTextReader reader = new XmlTextReader(htmlReader))
        {
            PdfWriter.GetInstance(document, output);
            HtmlParser.Parse(document, reader);
        }

    }
}
Darin Dimitrov
Thank you sir! That was quite helpful.
mson
the base string is just a div section <div>stuff</div>.the file gets created, but cannot be opened.does the code you posted work or was it just mockup?
mson