ASP.NET MVC生成字节PDF [] [英] ASP.NET MVC Generate PDF from byte[]

查看:316
本文介绍了ASP.NET MVC生成字节PDF []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发布这条消息我SO但是很少在这里完全封闭的说,他们需要看到FAQ看了很多帖子之前,很少给使用iTextSharp的还是其他什么东西的解决方案。但没有解决我的问题。我的问题是我有一个字节[]和我需要生成新的子窗口中的PDF 我们只是使用ASP.NET MVC 4,无iTextSharp的或相似的。请让我知道如果已经有一个职位,完全符合这一点。我确定创建新的局部视图

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. But none solves my issue. My issue is I have a byte[] and I need to generate a PDF in new child window. We are just using ASP.NET MVC 4 and no iTextSharp or similar. Please let me know if there is already a post that exactly matches this. I am ok to create new Partial Views

我在我的局部视图一个PDF图标形象。当用户点击它,我需要显示在新的浏览器窗口中的PDF。我可以成功调用JavaScript函数调用从另一台服务器获取文件控制器。我甚至可以转换为字节数组的文件。我想显示PDF格式的字节数组中一个新的浏览器窗口。

I have one PDF Icon image in my Partial View. When user clicks it I need to display PDF in new browser window. I can successfully call a JavaScript function that calls the controller that gets the file from another server. 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

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

ShowCDinPDF是我的JavaScript像下面

ShowCDinPDF is in my javascript like below

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

在我的控制器,下面我有ShowPDF方法

In my Controller, I have below ShowPDF method

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**

}

请让我知道如何创建PDF文件。

Please let me know how to create the PDF.

更新

我取得什么进展。现在我的code看起来像下面。一个新窗口打开,我得到的消息文件开头不是错误弹出'%PDF - 。我试图找到解决这个,但没有成功。

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-'. 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();
 } 

先谢谢了。

更新2

我试过了很多,但没有用。由于我们正在接近我们的PROD的最后期限,我们的团队决定在我们的服务器上创建的PDF文件,并​​以IE浏览器文件。

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.

注意

我真的不知道这是为什么下来投了反对票。有没有人可以渲染PDF格式的文件,而不存储/创建任何物理位置的PDF文件。
为什么要投下来?

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. Why down vote?

推荐答案

假设你已经重新$ P $字节数组psents有效的PDF,然后在你的控制器动作,你可以通过使用正确的返回正确的ActionResult服务于这个PDF Content-Type的响应头:

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:

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

这篇关于ASP.NET MVC生成字节PDF []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆