Generate and download pdf file

6

Is there any specific configuration to generate pdf file on the server? The problem is that locally in my local project works perfectly and when I upload to the server the file is not generated, the page is rendered forever until it reaches the timeout, the code snippet is this:

protected string GerarPDF(string html)
    {
        //Nome arquivo
        string nome = "Laudo_" + System.DateTime.Now.ToString().Replace(" ", "_").Replace("/", "_").Replace(":", "_") + ".pdf";
        var path = Path.Combine(Server.MapPath("~/Content/Uploads/Laudos"), nome);

        try
        {
            var pechkin = Factory.Create(new GlobalConfig());

            var pdf = pechkin.Convert(new ObjectConfig()
                                .SetLoadImages(true)
                                .SetPrintBackground(true)
                                .SetScreenMediaType(true)
                                .SetCreateExternalLinks(true)
                                .SetAllowLocalContent(true), html);

            using (FileStream file = System.IO.File.Create(path))
            {
                file.Write(pdf, 0, pdf.Length);
            }

            //Return the PDF file to download
            Response.Clear();

            Response.ClearContent();
            Response.ClearHeaders();

            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename=" + nome + ", size={0}", pdf.Length));
            Response.BinaryWrite(pdf);

            Response.Flush();
            Response.End();

            return nome;
        }
        catch
        {
            nome = string.Empty;
            return nome;
        }


    }

I have implemented this solution but a data type conversion error occurs:

cannot implicity convert type

    
asked by anonymous 09.04.2016 / 00:15

1 answer

6

This is the very wrong way to do. Nothing guarantees that you are actually manipulating the request with this.

There are two ways for you to resolve this correctly:

  • Making GerarPDF return byte[] ;
  • Make GerarPDF return FileAction . In this case, GerarPDF would be Action of your Controller .
  • In the first form, it looks like this:

    protected byte[] GerarPDF(string html)
    {
        //Retire isso aqui
        // string nome = "Laudo_" + System.DateTime.Now.ToString().Replace(" ", "_").Replace("/", "_").Replace(":", "_") + ".pdf";
        // var path = Path.Combine(Server.MapPath("~/Content/Uploads/Laudos"), nome);
    
        try
        {
            var pechkin = Factory.Create(new GlobalConfig());
    
            var pdf = pechkin.Convert(new ObjectConfig()
                                .SetLoadImages(true)
                                .SetPrintBackground(true)
                                .SetScreenMediaType(true)
                                .SetCreateExternalLinks(true)
                                .SetAllowLocalContent(true), html);
    
            using (MemoryStream file = new MemoryStream())
            {
                file.Write(pdf, 0, pdf.Length);
            }
    
            return pdf.ToArray();
        }
        catch
        {
            nome = string.Empty;
            return nome;
        }
    }
    

    And Controller :

        public ActionResult GerarLaudo()
        {
            // Coloque aqui a lógica pra gerar o HTML.
            byte[] arquivo = GerarPDF(html);
            return File(arquivo, System.Net.Mime.MediaTypeNames.Application.Octet, "Laudo.pdf");
        }
    

    The second method is the same, except that you will not separate into a function called GerarPDF . It will call the Pechkin and return the file, all within the same Action .

        
    09.04.2016 / 01:00