使用MVC生成PDF文件

时间:2023-02-10 22:18:10

I'm trying to generate a PDF using MVC, the problem I have is that I'm not sure how to display or make it work at the end, I'm using an ActionResult where I execute a Stored Procedure and fill a datatable with the result, which I later "try" to use in the PDF generator, so far it goes well, the problem is when I send the result to the View, where I get a error and don't know how to solve it, this is a life situation so I'd be very grateful if you could help me, sorry for my bad english.

我正在尝试使用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");
        }
    }

Partial View:

     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);
         }
     });

};

It's called by this URL Action.

它由此URL动作调用。

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

When I execute it, the console shows this error:

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

jquery-3.2.1.min.js:4 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

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

1 个解决方案

#1


0  

I finally did it by doing this:

我终于通过这样做了:

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

Where id is the parameter I need to send to the ActionResult in the HomeController. I guess it was very silly at the end.

其中id是我需要发送到HomeController中的ActionResult的参数。我觉得最后很傻。

#1


0  

I finally did it by doing this:

我终于通过这样做了:

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

Where id is the parameter I need to send to the ActionResult in the HomeController. I guess it was very silly at the end.

其中id是我需要发送到HomeController中的ActionResult的参数。我觉得最后很傻。