Generate PDF
We are currently sending email notification in plain text or html format. Our environment is C # /. NET / SQL Server.
I would like to know if anyone recommends a specific solution. I see two ways to do this:
- dynamically convert current email to pdf using third party library and send PDF as attachment
or
- use SSRS to allow users to export the pdf report (SSRS reports may eventually be transferred)
I am open to third party libraries (especially if they are open source and free). It looks like SSRS is the simplest and easiest way. Anyone have any hints?
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);
}
}
}
iText-Sharp
Works very similar to the java version and has both excellent documentation and several books available.
If you need a simple html -> pdf conversion, have a look at wkhtmltopdf . It is a free, open source, webkit-based command line utility.
If you need something more complex, like preparing multiple templates or keeping a PDF release history. Have a look at jsreport report server
Ugh. I hate PDF files. As the saying goes, what happened to your current text or HTML emails? PDF files are inefficient and rather large file formats.