使用MVC生成PDF文件

时间:2017-11-28 17:49:45

标签: c# ajax asp.net-mvc blob

我正在尝试使用MVC生成PDF,我遇到的问题是我不知道如何在最后显示或使其工作,我正在使用ActionResult执行存储过程并填充一个带有结果的数据表,我后来“试着”在PDF生成器中使用,到目前为止一切顺利,问题是当我将结果发送到View时,我得到一个错误,不知道如何解决它,这是一种生活状况,所以如果你能帮助我,我会非常感激,抱歉我的英语不好。

的ActionResult:

public ActionResult PDFGenerator(string id
        )

    {
        using (Document document = new Document())
        {
            string idpago = "2";
            //while (id != null)
            int identificacionpago = Convert.ToInt32(idpago);
            DataTable dt = new DataTable();
            Database conex = Conexion.getInstancia();
            dt = conex.ExecuteDataSet("Usp_TraerPago", identificacionpago).Tables[0];
            string pago = dt.Rows[0]["ValorPago"].ToString();
            string aniopago = dt.Rows[0]["AnioPago"].ToString();
            DateTime fechapago = Convert.ToDateTime(dt.Rows[0]["FechaPago"]);
            //int pag = Convert.ToInt32(ViewBag.datos);
            int idusuario = Convert.ToInt32(dt.Rows[0]["IdUsuario"].ToString());

            DataTable dt1 = new DataTable();
            dt1 = conex.ExecuteDataSet("Usp_UsuarioPago", idusuario).Tables[0];
            string Numid = dt1.Rows[0]["NumIdentificacion"].ToString();
            string Tipoid = dt1.Rows[0]["TipoIdentificacion"].ToString();
            string Nombre = dt1.Rows[0]["NombresUsuario"].ToString();
            string apellidos = dt1.Rows[0]["ApellidosUsuario"].ToString();
            MemoryStream workStream = new MemoryStream();
            // Document document = new Document();
            PdfWriter.GetInstance(document, workStream).CloseStream = false;

            document.Open();

            document.Add(new Paragraph("                                                                                                                     " + DateTime.Now.ToString()));
            document.Add(new Paragraph("Certificado de participación"));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));

            document.Add(new Paragraph("                               por el año " + aniopago));
            document.Add(new Paragraph("El suscrito a  " + Nombre + " " + apellidos + " identificado con " + Tipoid + " " + Numid + " " + " En la fecha " + fechapago.Year + "/" + fechapago.Month + "/" + fechapago.Day + aniopago));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("___________________________ "));
            document.Add(new Paragraph("Firma del Revisor Fiscal   "));

            //document.SaveAs(workStream);
            document.Close();

            byte[] byteInfo = workStream.ToArray();
            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;

            return File(workStream, "application/pdf");
        }
    }

部分视图:

     function generarpdf(value) {

     $.ajax({
         url: '@Url.Action("PDFGenerator", "Home")',
         type: 'POST',
         async: false,
         data: { "id": value },
         dataType: "application/pdf",
         success: function (data) {
             var file = new Blob([data], { type: 'application/pdf' });
             var fileURL = URL.createObjectURL(file);
             window.open(fileURL);
         }
     });

};

由此URL操作调用。

    <a id="libro" href='@Url.Action("PDFGenerator","Home")'></a>

当我执行它时,控制台显示以下错误:

jquery-3.2.1.min.js:4 [弃用]主线程上的同步XMLHttpRequest因其对最终用户体验的不利影响而被弃用。如需更多帮助,请查看https://xhr.spec.whatwg.org/

1 个答案:

答案 0 :(得分:0)

我最后这样做了:

<a href='Home/PDFGenerator/'+  id+' "><img src="/Content/ImagesOwner/if_pdf_3745.png"/></a>

其中id是我需要发送到HomeController中的ActionResult的参数。我想最后这是非常愚蠢的。

相关问题