Generate and download pdf file

Asked

Viewed 1,792 times

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 go up to the server the file is not generated, the page is processing eternally 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 implemented this solution but data type conversion error occurs:

cannot implicity convert type

inserir a descrição da imagem aqui

1 answer

6


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

There are two ways you can resolve this correctly:

  1. Making GerarPDF return a byte[];
  2. Do GerarPDF return a FileAction. In this case, GerarPDF would be a 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 the 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 thing, only you will not separate into a function called GerarPDF. He’ll call Pechkin and return the file, all in the same Action.

  • hello Gypsy gives type conversion error, I edited the question and added error

  • Oops, I messed up. Look now.

  • Blz! It works by running my local project, but after I publish and upload it to the server it does not generate the pdf and does not download.

  • But does it make a mistake? Or does nothing happen?

  • Nothing happens.

  • Then I think it’s a question of debug. I don’t know what can be.

  • OK thanks, the situation is difficult because during the debug in my note works correctly but after the site is published does not work, so I do not need any configuration in IIS beyond the Pechkin dlls to be able to generate the pdf.

  • It depends on where you post it. If you go to Azure on Web Site it won’t work. It’s the same problem as Rotary.

  • It works correctly, but the PDF generates more than one page and when I download the page is not numbered, this is a normal behavior when PDF is generated by Pechkin ?

  • 1

    I think so. But since Pechkin is a wkhtmltopdf envelope, there must be some option to define page numbering by Pechkin itself.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.