简体   繁体   English

如何在ASP.NET MVC中最好地将网格导出为PDF

[英]How best to export a grid to PDF in ASP.NET MVC

I don't know that this is necessarily important, but I'm using Infragistics iggrid for my grid and their Reports stuff to export to PDF. 我不知道这是否一定很重要,但是我正在对网格使用Infragistics iggrid,并将其Reports内容导出为PDF。

The underlying issue I have is that my data that I want to export is in the browser and I would prefer that I don't have to create a server-side file to download. 我的根本问题是我要导出的数据在浏览器中,我希望不必创建要下载的服务器端文件。 We have an icon on the screen that the user clicks to download the PDF. 用户在屏幕上单击可以下载PDF的图标。

So what I'm doing on the client, is collecting all the data. 所以我在客户端上所做的就是收集所有数据。 This has to be done client-side because I want to export the data as the user has it sorted, filtered, and column-ordered (otherwise I could just collect the data server-side which would make this simpler). 这必须在客户端完成,因为我想在用户对数据进行排序,过滤和按列排序时导出数据(否则,我可以只收集服务器端的数据,这将使此过程更简单)。 I then send the data to the server via a POST. 然后,我通过POST将数据发送到服务器。

On the server-side I generate the PDF file. 在服务器端,我生成PDF文件。 Now, obviously, I could save the PDF server-side and redirect to the generated file, but that adds maintenance of temporary files which I'd prefer to avoid (but worst case, I can go there. Just fishing for options right now). 现在,显然,我可以保存PDF服务器端并重定向到生成的文件,但这增加了我希望避免的临时文件的维护(但最坏的情况是,我可以去那里。现在就寻找选项) 。

I tried returning the data base64 encoded and then doing: 我尝试返回编码的base64数据,然后执行以下操作:

window.open("data:application/pdf;base64," + encodedData);

This doesn't work (at least in IE) because the URL limit is a bit over 2K. 这不起作用(至少在IE中如此),因为URL限制超过2K。

I tried using the downloadDataURI javascript function here: http://code.google.com/p/download-data-uri/ 我尝试在此处使用downloadDataURI javascript函数: http : //code.google.com/p/download-data-uri/

But that only appears to work with Chrome (even after commenting out the webkit check) and I'm apparently not clever enough to figure out why. 但这似乎只能与Chrome一起使用(即使在注释掉Webkit检查之后),而且我显然还不够聪明,无法弄清原因。

I'm sure I'm missing some obvious possibility that doesn't require creating a server-side file, but I'm just not seeing it. 我确定我遗漏了一些不需要创建服务器端文件的明显可能性,但我只是没有看到它。 (disclaimer: My daughter woke me up horribly early this morning so the answer could be really trivial and I will feel stupid tomorrow when my brain is working). (免责声明:我的女儿今天早晨很早地把我吵醒了,所以答案可能真是微不足道,明天我的大脑运转时,我会感到愚蠢)。

On the server-side I generate the PDF file. 在服务器端,我生成PDF文件。 Now, obviously, I could save the PDF server-side and redirect to the generated file, but that adds maintenance of temporary files which I'd prefer to avoid (but worst case, I can go there. Just fishing for options right now). 现在,显然,我可以保存PDF服务器端并重定向到生成的文件,但这增加了我希望避免的临时文件的维护(但最坏的情况是,我可以去那里。现在就寻找选项) 。

You don't need to save it on the server. 您无需将其保存在服务器上。 You can simply stream the PDF File (I assume you have it in some sort of Stream or byte[]) to the user. 您可以简单地将PDF文件流式传输给用户(我假设您以某种Stream或byte []的形式拥有它)。 All you need to do is something like 您需要做的就是

 Response.ContentType = "application/pdf";
 Response.AddHeader("Content-Disposition", "filename.pdf");
 Response.BinaryWrite(bytes);
 Response.Flush();
 Response.Close();
 Response.End();

And this will prompt the user to either save the file or open it in Adobe Reader. 这将提示用户保存文件或在Adobe Reader中打开文件。 The file won't be created on the server at all. 完全不会在服务器上创建该文件。

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

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