简体   繁体   English

ASP.NET MVC 从 byte[] 生成 PDF

[英]ASP.NET MVC Generate PDF from byte[]

Before posting this message I looked many posts in SO but few where closed outright saying they need to see FAQ, few gave solutions that use iTextSharp or something else.在发布这条消息之前,我在 SO 中看了很多帖子,但很少有人直接说他们需要查看常见问题解答,很少有人给出了使用 iTextSharp 或其他东西的解决方案。 But none solves my issue.但没有一个能解决我的问题。 My issue is I have a byte[] and I need to generate a PDF in new child window .我的问题是我有一个字节 [],我需要在新的子窗口中生成一个 PDF We are just using ASP.NET MVC 4 and no iTextSharp or similar.我们只是使用 ASP.NET MVC 4 而没有 iTextSharp 或类似的。 Please let me know if there is already a post that exactly matches this.请让我知道是否已经有与此完全匹配的帖子。 I am ok to create new Partial Views我可以创建新的局部视图

I have one PDF Icon image in my Partial View.我的部分视图中有一个 PDF 图标图像。 When user clicks it I need to display PDF in new browser window.当用户单击它时,我需要在新的浏览器窗口中显示 PDF。 I can successfully call a JavaScript function that calls the controller that gets the file from another server.我可以成功调用一个 JavaScript 函数,该函数调用从另一台服务器获取文件的控制器。 I can even get the file converted to a byte array.我什至可以将文件转换为字节数组。 I want to show this byte array in PDF format in a new browser window.我想在新的浏览器窗口中以 PDF 格式显示这个字节数组。

In View I have PDF Icon like below在视图中,我有如下所示的 PDF 图标

<img onclick="ShowCDinPDF('@Url.Action("ShowPDF", "MyController", new {personid= personid} )','', '920','500')" />

ShowCDinPDF is in my javascript like below ShowCDinPDF 在我的 javascript 中,如下所示

function ShowCDinPDF(popUpURL, windowProperties, w, h) {    
var childWindow = window.showModelessDialog(popUpURL, "", "");   
}

In my Controller, I have below ShowPDF method在我的控制器中,我有以下 ShowPDF 方法

public ActionResult ShowPDF(string personid)
{ 
   //call service and get data
   string fileContent = response.FileContent;
   byte[] data = Convert.FromBase64String(fileContent);

   **// Here using data I need to show PDF in new window**

}

Please let me know how to create the PDF.请让我知道如何创建 PDF。

UPDATE更新

I made little progress.我进步不大。 Now my code looks like below.现在我的代码如下所示。 A new window opens and I get error popup with message File does not begin with '%PDF-' .一个新窗口打开,我收到错误弹出消息File does not begin with '%PDF-' I tried to find solution to this but no success.我试图找到解决方案,但没有成功。

 public ActionResult ShowPDF(string personid)
 { 
     //call service and get data
     string fileContent = response.FileContent;
     byte[] data = Convert.FromBase64String(fileContent);

     using (MemoryStream memoryStream = new MemoryStream())
      {
          Response.ClearHeaders();
          Response.ClearContent();
          Response.Charset = "";
          Response.AddHeader("Content-Type", "application/pdf");
          memoryStream.Write(data, 0, data.Length);
          memoryStream.WriteTo(Response.OutputStream);
          Response.Flush();
          Response.Close();
          Response.End();
     }
    return View();
 } 

Thanks in advance.提前致谢。

UPDATE 2更新 2

I tried alot but no use.我尝试了很多但没有用。 Since we were approaching our PROD deadline, our team decided to create PDF file in our server and launch that file in IE browser.由于我们接近我们的 PROD 截止日期,我们的团队决定在我们的服务器中创建 PDF 文件并在 IE 浏览器中启动该文件。

NOTE笔记

I really have no idea why this is down voted.我真的不知道为什么这被否决了。 Did anybody can render a file in PDF format without storing/creating the PDF file in any physical location.是否有人可以在不将 PDF 文件存储/创建在任何物理位置的情况下以 PDF 格式呈现文件。 Why down vote?为什么投反对票?

Assuming that the byte array you have represents a valid PDF then in your controller action you could serve this PDF by returning the proper ActionResult with the correct Content-Type response header:假设您拥有的字节数组代表一个有效的 PDF,那么在您的控制器操作中,您可以通过使用正确的 Content-Type 响应标头返回正确的 ActionResult 来提供此 PDF:

public ActionResult ShowPDF(string personid)
{ 
    //call service and get data
    string fileContent = response.FileContent;
    byte[] data = Convert.FromBase64String(fileContent);

    **// Here using data I need to show PDF in new window**
    return File(data, "application/pdf");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM