using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.IO;
using System.Text;
using SelectPdf;

namespace SelectPdf.Samples.Controllers
{
    public class CurrentWebPageToPdfController : Controller
    {
        //
        // GET: /CurrentWebPageToPdf/

        public ActionResult Index()
        {
            ViewData.Add("SampleText", "sample text");
            ViewData.Add("DropDownList1", "Value3");
            ViewData.Add("SampleCheckbox", "true");
            ViewData.Add("SampleText2", "1000");
            ViewData.Add("SampleText3", "800");

            return View();
        }

        [HttpPost]
        public ActionResult Convert(FormCollection collection)
        {
            RenderingEngine renderingEngine =
                (RenderingEngine)Enum.Parse(typeof(RenderingEngine), Request["DdlRenderingEngine"], true);

            // set data for the view
            ViewDataDictionary MyViewData = GetViewData(collection);

            // render view to get html
            StringWriter html = new StringWriter();

            ViewEngineResult viewEngineResult = ViewEngines.Engines.FindView(ControllerContext, "Index", null);
            ViewContext viewContext = new ViewContext(
                    ControllerContext,
                    viewEngineResult.View,
                    MyViewData,
                    new TempDataDictionary(),
                    html
                    );
            viewEngineResult.View.Render(viewContext, html);

            string htmlString = html.ToString();

            // get base url
            string baseUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri.Substring(
                0, this.ControllerContext.HttpContext.Request.Url.AbsoluteUri.Length - "CurrentWebPageToPdf/Convert".Length);

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();
            converter.Options.RenderingEngine = renderingEngine;

            // create a new pdf document converting a html string
            PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl);

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;
        }

        private ViewDataDictionary GetViewData(FormCollection collection)
        {
            ViewDataDictionary MyViewData = new ViewDataDictionary();

            MyViewData.Add("SampleText", collection["TxtSampleText"]);
            MyViewData.Add("DropDownList1", collection["DropDownList1"]);
            MyViewData.Add("SampleCheckbox", collection["ChkSampleCheckbox"]);

            MyViewData.Add("SampleText2", collection["TxtSampleText2"]);
            MyViewData.Add("SampleText3", collection["TxtSampleText3"]);

            return MyViewData;
        }


    }
}
