2017-11-28 15 views
1

MVC를 사용하여 PDF를 생성하려고합니다. 문제는 내가 어떻게 표시할지 또는 결국 제대로 작동하는지 모르겠다는 것입니다. ActionResult를 사용하고 있습니다. 저장 프로 시저를 실행하고 결과로 데이터 테이블을 채우십시오. 나중에 PDF 생성기에서 사용하려고 "시도"합니다. 지금까지는 문제가되지 않습니다. 뷰에 결과를 보내면 오류가 발생하고 돈을받지 못합니다. 그것을 해결하는 방법을 알지 못한다. 이것은 삶의 상황이다. 그래서 나를 도울 수 있다면 정말 고맙겠다. 나쁜 영어로 유감스럽게 생각한다.MVC를 사용하여 PDF 파일 생성

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은 액션에 의해 호출됩니다.

JQuery와-3.2.1.min.js :

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

그것을 실행

는 콘솔이 오류를 도시 4 중단] 동기 XMLHttpRequest 객체는 메인 스레드 때문에 그것의 해로운 효과되지 않으며 최종 사용자의 경험. 도움이 더 필요하면 https://xhr.spec.whatwg.org/을 확인하십시오.

+3

아약스 호출에서 'async : false'를 설정했기 때문입니다. 그 경고는 jQuery의 향후 버전에서 그렇게하지 않는다고 말하고 있습니다. 그 설정을 사용하는 특별한 이유가 있습니까? – jmoerdyk

+0

문제는 결국 * 부분 뷰 *가 ActionResult에서 File (workStream, "application/pdf")을받지 못하기 때문에 아무 것도 표시하지 않는다는 것입니다. – Aceman

답변

0

드디어 이렇게하여 한 :

ID가 나는 HomeController의 ActionResult에 전송하는 데 필요한 매개 변수입니다
<a href='Home/PDFGenerator/'+ id+' "><img src="/Content/ImagesOwner/if_pdf_3745.png"/></a> 

. 나는 그것이 결국 바보 같았다고 생각한다.