Open PDF in browser automatically after download

1

The PDF downalod is executed correctly (minimized in the bottom corner of the browser).

How do you open automatically on a new tab ?:

<a href="#" class="download" name="downloaditem" id="downloaditem3" target="_blank"><span style="cursor:pointer"><font color="red"><b>BAIXADA</b></font></span></a>

This question looks something like this How to open the RazorPDF report in a separate tab , however I am using another PDF generator Pechkin , I added the target="_blank" in the link as above, but it does not work.

This is the Action that generates the PDF:

[HttpPost]
        public ActionResult PDFUmDocSelecionado(ProcessamentoRegistros pProcessamentoRegistros)
        {
            try
            {
                string _nomeArquivo = "Meu_Documento_" + DateTime.Now.ToString().Replace(" ", "_").Replace("/", "_").Replace(":", "_") + ".pdf";
                DataTable _Dt = new DataTable();

                _Dt = _IRepositorio.ObterHTML(pProcessamentoRegistros);
                DataRow foundRow = _Dt.Rows[0];

                var pechkin = Factory.Create(new GlobalConfig());
                var pdf = pechkin.Convert(new ObjectConfig()
                                                .SetLoadImages(true).SetZoomFactor(1)
                                             .SetPrintBackground(true)
                                             .SetScreenMediaType(true)
                                             .SetCreateExternalLinks(true)
                                             .SetIntelligentShrinking(true).SetCreateInternalLinks(true)
                                             .SetAllowLocalContent(true), foundRow.ItemArray[0].ToString());
                using (MemoryStream file = new MemoryStream())
                {
                    file.Write(pdf, 0, pdf.Length);
                }

                byte[] arquivo = pdf;

                //Response.Clear();
                //Response.ClearContent();
                //Response.ClearHeaders();
                //Response.ContentType = "application/pdf";
                ////Response.AddHeader("Content-Disposition", string.Format("attachment;filename=arquivo.pdf, size={0}", _pdf.Length));
                //Response.AddHeader("Content-Disposition", string.Format("inline;filename=" + _nomeArquivo + ", size={0}", pdf.Length));
                //Response.BinaryWrite(pdf);
                //Response.Flush();
                //Response.End();

                ////return RedirectToAction("Index", "Documento");
                return File(arquivo, System.Net.Mime.MediaTypeNames.Application.Octet, _nomeArquivo);
            }
            catch
            {
                return RedirectToAction("Index", "ProcessamentoRegistros");
            }
        }
    
asked by anonymous 05.06.2017 / 16:43

1 answer

2

Instead of returning File , return FileStreamResult . Because the file is in byte[] , you need to do this to move to MemoryStream :

byte[] arquivo = pdf;
MemoryStream pdfStream = new MemoryStream();
pdfStream.Write(stream.ToArray(), 0, stream.ToArray().Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");

In the code you put as an example, it would look like this:

[HttpPost]
public ActionResult PDFUmDocSelecionado(ProcessamentoRegistros pProcessamentoRegistros)
{
    try
    {
        string _nomeArquivo = "Meu_Documento_" + DateTime.Now.ToString().Replace(" ", "_").Replace("/", "_").Replace(":", "_") + ".pdf";
        DataTable _Dt = new DataTable();

        _Dt = _IRepositorio.ObterHTML(pProcessamentoRegistros);
        DataRow foundRow = _Dt.Rows[0];

        var pechkin = Factory.Create(new GlobalConfig());
        var pdf = pechkin.Convert(new ObjectConfig()
                                        .SetLoadImages(true).SetZoomFactor(1)
                                     .SetPrintBackground(true)
                                     .SetScreenMediaType(true)
                                     .SetCreateExternalLinks(true)
                                     .SetIntelligentShrinking(true).SetCreateInternalLinks(true)
                                     .SetAllowLocalContent(true), foundRow.ItemArray[0].ToString());
        using (MemoryStream file = new MemoryStream())
        {
            file.Write(pdf, 0, pdf.Length);
        }

        byte[] arquivo = pdf;

        MemoryStream pdfStream = new MemoryStream();
        pdfStream.Write(stream.ToArray(), 0, stream.ToArray().Length);
        pdfStream.Position = 0;
        return new FileStreamResult(pdfStream, "application/pdf");
    }
    catch
    {
        return RedirectToAction("Index", "ProcessamentoRegistros");
    }
}

Below is another example using a snippet of an answer that I gave your other question:

[HttpPost]
public ActionResult OpenPdfFileInBrowser()
{
    var doc = new Document(PageSize.A4.Rotate());
    var stream = new MemoryStream();
    var pw = PdfWriter.GetInstance(doc, stream);
    var minhaStringHTML = @"<HTML><HEAD></HEAD><body><FORM method='post'><table><tr><td>Nome:</td><td>JOÃO DA SILVA</td></tr><tr><td>NOME:</td><td>MARCOS ALVES</td></tr></table></FORM></BODY></HTML>";

    doc.Open();
    using (var srHtml = new StringReader(minhaStringHTML))
    {
        //Convertendo o HTML
        XMLWorkerHelper.GetInstance().ParseXHtml(pw, doc, srHtml);
    }
    doc.Close();

    MemoryStream pdfStream = new MemoryStream();
    pdfStream.Write(stream.ToArray(), 0, stream.ToArray().Length);
    pdfStream.Position = 0;
    return new FileStreamResult(pdfStream, "application/pdf");
}

See working:

I used the same GitHub project as before and added an example to open the PDF in the browser. >

    
05.06.2017 / 17:44