Elim99
Elim99

Reputation: 673

ASP.NET MVC Converting HTML page containing Form to PDF

I'm working on a view in a ASP.NET MVC 2 application. This view will contain fixed text but it will also contain text boxes, checkboxes, and perhaps a Telerik Grid that can be updated from a user. ** This form isn't in a fixed format since there could be a 1...N number of items listed. ** We want to be able to print this view to a PDF. The PDF we want to print off will just look like the view but preferrably it will have just the text from the text boxes and not the text box border. The same goes for the Telerik grid.

How to you recommend I go about doing this? Preferrably I like to see a print button on the view that will directly print it to a PDF. i.e. No secondary window that pops up. That may not be a deal breaker though.

** Update ** Let's forget about the form elements for a second. Let's say my view is displayed in the same format that I want in my PDF. How to print that view into a PDF?

Upvotes: 2

Views: 2828

Answers (3)

Niraj
Niraj

Reputation: 1842

You can make an rdlc report as desired and call at the click of a print button/link in your view, through a controller function.

in your view

    Html.ActionLink("Print", "Print", new { id = c.sid }) 

in you controller

    public ActionResult Print(int id)
            {
                string unitc = Session["unit"].ToString();

                ctid= unitc;//class level variable used in detailreport function  
                brid = id;//class level variable used in detailreport function
                return DetailsReport();

            }



    FileContentResult DetailsReport()
        {

            LocalReport localReport = new LocalReport();

            localReport.ReportPath = Server.MapPath("~/Reports/rptinvoice.rdlc");

            InvoiceRepository ivr = new InvoiceRepository();

            if (localReport.DataSources.Count > 0)
            {
                localReport.DataSources.RemoveAt(0);
                localReport.DataSources.RemoveAt(1);
                localReport.DataSources.RemoveAt(2);

            }
            localReport.Refresh();

            ReportDataSource reportDataSource = new ReportDataSource("DataSet1", ivr.GetSales(ctid));

            localReport.SetParameters(new ReportParameter[] { new ReportParameter("ct_id", ctid.ToString()), new ReportParameter("ct_br_id", brid.ToString()) });


            localReport.DataSources.Add(reportDataSource);


            string reportType = "PDF";

            string mimeType;

            string encoding;

            string fileNameExtension;




            string deviceInfo =

            "<DeviceInfo>" +

            "  <OutputFormat>PDF</OutputFormat>" +

            "  <PageWidth>8.5in</PageWidth>" +

            "  <PageHeight>11in</PageHeight>" +

            "  <MarginTop>0.2in</MarginTop>" +

            "  <MarginLeft>0.05in</MarginLeft>" +

            "  <MarginRight>0.05in</MarginRight>" +

            "  <MarginBottom>0.1in</MarginBottom>" +

            "</DeviceInfo>";



            Warning[] warnings;

            string[] streams;

            byte[] renderedBytes;

            localReport.EnableExternalImages = true;

            //Render the report

            try
            {

            renderedBytes = localReport.Render(

            reportType,

            deviceInfo,

            out mimeType,

            out encoding,

            out fileNameExtension,

            out streams,

            out warnings);



            }
            catch (Exception Ex)
            {
                ViewData["ResultP"] = Ex.Message + ",<br>" + Ex.InnerException.Message;
                throw;
            }


            return File(renderedBytes, mimeType);

        }

Upvotes: 0

Steve
Steve

Reputation: 2997

Most free open source PDF .dlls are difficult to programatically create HTML in a PDF (mostly due to limited support for HTML tags etc.).

A paid for one is much more simple eg. http://www.html-to-pdf.net/ With this you can just point the converter to a template page and it will work. Even javascript and Flash content etc will also be parsed and included (statically) in the final PDF.

Upvotes: 0

nikmd23
nikmd23

Reputation: 9113

The simplest way to do this is to create a separate Print action that returns a FileResult of a PDF generated on the fly with a library like iTextSharp

You would not be able to completely reuse the HTML form as is in the PDF document, since you don't want to use text boxes, but your could generate an HTML view that matches your desired PDF and then use iTextSharp to save that HTML as a PDF.

Alternatively, you could use the iTextSharp library to build up a PDF from scratch and have much more control, but this might be a bit more difficult.

From your controller the simplest way to return the PDF without a secondary window is to have your action method return:

return File(iTextSharpByteArray, "application/pdf", "nameOfFileUserWillDownload.pdf");

Upvotes: 3

Related Questions