Humpy
Humpy

Reputation: 2012

PDF or Other "report viewer" options for Asp.net C#

I'm in a bind with my current project. It's a payroll application and I'm developing in ASP.net webforms with C#. My boss said that the ideal function of this site is to click on the pay check date, and a PDF opens and shows the paycheck information. I have done research for a few days to try to find the best solution. So far, I have had no luck. I have found a few different things such as iTextSharp but the license for one year is too expensive right now. I have also seen the tcpdf which is php based. I have also looked into CrystalReports, and Active Reports. To purchase the license is too expensive as well. I have looked into XML to PDF solutions also but I'm not finding anything definite. Since I'm pretty new to the business world of software development and don't have senior developers to rely on, I'm pretty much dead in the water. I know we will be downloading the paychecks into a CSV file from a DOS based application that does our clock-ins and outs. I will then be importing the CSV files into SQL Server 2012 Express. Your ideas are greatly appreciated as I don't know where to go from here! Thank you in advance!

Upvotes: 3

Views: 6129

Answers (2)

Jan Blaha
Jan Blaha

Reputation: 3095

I also agree that html -> pdf is the best option nowadays if you want to render complex report and stay productive.

I have implemented a wrapper for html -> pdf conversion called jsreport.

If you are using asp.net mvc, you can check out my post Rendering pdf from Asp.Net MVC views or more generic post Pdf reports in c#.

Disclaimer: I am the author of jsreport

Upvotes: 0

DaveDev
DaveDev

Reputation: 42185

A HTML to PDF converter that I've recently discovered is WKHTMLtoPDF

It's open source and uses WebKit to convert HTML to PDF so it's pretty standards compliant.

An example of how you might use it is

using (var pdfStream = new FileStream(dlg.FileName, FileMode.OpenOrCreate))
{
    // pass in the HTML you want to appear in the PDF, and the file stream it writes to
    Printer.GeneratePdf(htmlStream, pdfStream);
}

where GeneratePdf is defined as

    public static void GeneratePdf(Stream html, Stream pdf) 
    {
        Process process;
        StreamWriter stdin;
        var psi = new ProcessStartInfo();

        psi.FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Lib", "wkhtmltopdf.exe");
        psi.WorkingDirectory = Path.GetDirectoryName(psi.FileName);

        // run the conversion utility
        psi.UseShellExecute = false;
        psi.CreateNoWindow = true;
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;

        psi.Arguments = "-q -n --disable-smart-shrinking - -";
        process = Process.Start(psi);

        try
        {
            stdin = process.StandardInput;
            stdin.AutoFlush = true;
            //stdin.Write(html.ReadToEnd());
            stdin.Write(new StreamReader(html).ReadToEnd());
            stdin.Dispose();

            process.StandardOutput.BaseStream.CopyTo(pdf);

            process.StandardOutput.Close();
            pdf.Position = 0;

            process.WaitForExit(10000);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            process.Dispose();
        }
    }

In your case, instead of writing it to a file stream, you'd write it to the HTTP output stream as a PDF.

Please note however, that this example is more suitable to writing PDF files to disk, rather than the output stream so you'd need to do it differently slightly for it to work for you.

Upvotes: 2

Related Questions