From HTML to PDF in MVC 5 ASP.NET

0

Boys I have a view like this

that is supposed to be an invoice for clients, it already prints but I need the user to download a pdf and that has been impossible probe on the client side with several js libraries like jsPDF, but this option is downloading a pdf with a very ugly image of the document or the facutra

I really am very lost as I do so that the user can download that screen in a pdf but that it throws exactly the same that you are seeing?

    
asked by Steven Hernández 19.10.2018 в 00:03
source

2 answers

0

Thank you very much for helping me, I commented that I could finally do what I wanted to put the code in case someone someday needs this:

    public ActionResult CreatorPDF(string consecutive)
    {
        Dictionary<string, string> cookieCollection = new Dictionary<string, string>();
        foreach (var key in Request.Cookies.AllKeys)
        {
            cookieCollection.Add(key, Request.Cookies.Get(key).Value);
        }
        string nameFile = consecutive.ToLower().Contains("cot") ? string.Format("{0}.pdf", consecutive) :
            string.Format("{0}-{1}.pdf",ConfigurationManager.AppSettings["PrefixEstablishment"],consecutive);
        var pdf = new ActionAsPdf(string.Format("OrderInvoice/{0}", consecutive)) {
            Cookies = cookieCollection,
            PageSize =Rotativa.Options.Size.A4,
            CustomSwitches = "--print-media-type",
            PageMargins = {Left=1,Right=1},
            FileName = nameFile,
        };
        return pdf;
    }

observations: * the foreach, keeps the active session when generating the pdf, this prevents it from generating a pdf of the login page * the variable string nameFile can be ignored is something I have put to verify if the user generates a quote or a real invoice

Ready that's all boys.

    
answered by 22.10.2018 / 22:07
source
0

I have achieved this, partially: add the nuguet Rotativa.Mvc, and the code is this:

    public ActionResult GeneratePDF()
    {
        return new Rotativa.ActionAsPdf("aqui la vista" );
    }

the problem is that I throw a pdf but with the login page of the application haha seems that something loses the login you think?

    
answered by 19.10.2018 в 06:41