简体   繁体   English

将服务器端生成的PDF发送到浏览器

[英]Send server side generated PDF to the browser

I have an AJAX call that calls a static WebMethod on the Server side. 我有一个AJAX调用,它在服务器端调用了静态WebMethod。 The Server side Method returns a MemoryStream containing the byte stream of the PDF file. 服务器端方法返回一个包含PDF文件字节流的MemoryStream。 How can I then use this PDF byte stream on the client side in the success method of the AJAX call, to somehow trigger the PDF file download. 然后,如何在AJAX调用成功方法的客户端上使用此PDF字节流,以某种方式触发PDF文件下载。 I would also don't what to do a full postback of the page. 我也不会做该页面的完整回发。

I've used this as reference: http://forums.asp.net/t/1377154.aspx?Download+from+Javascript But I would like to have a full example achieving this. 我已经将此作为参考: http : //forums.asp.net/t/1377154.aspx?Download+from+Javascript但是我想举一个完整的例子来实现这一点。

function generatePDF(param1, param2, param3) {
    $.ajax({
        type: 'POST',
        url: 'Page.aspx/GeneratePDF',
        data: '{ "param1" : "' + param1+ '", "param2" : "' + param2+ '", "param3" : "' + param3+ '" }',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (pdf) {

            //from here somehow, download the generated PDF file
        }
    });
}

I haven't personally done this, but the first thing I would try would be: 我个人还没有这样做,但是我要尝试的第一件事是:

On the server side dynamically build the appropriate HTML document and CSS Use phantomJS to render that document Tell phantomJS to convert that document to PDF, saved in a temp file Send the PDF back as the HTTP response by writing the temp PDF file to the response body Delete the temp file If you are struggling with content-type, content-disposition, etc, you shouldn't have to worry about that if you have a valid pdf file on disk and just write that data to the HTTP response. 在服务器端动态生成适当的HTML文档和CSS使用phantomJS呈现该文档告诉phantomJS将该文档转换为PDF,保存在临时文件中通过将临时PDF文件写入响应主体将PDF作为HTTP响应发送回删除临时文件如果您在处理内容类型,内容处置等问题,则不必担心磁盘上是否有有效的pdf文件并将其写入HTTP响应中。 With the right headers, you should be able to ask the browser to either display the PDF or treat it as a file to be saved as a download. 使用正确的标题,您应该能够要求浏览器显示PDF或将其视为要保存为下载文件的文件。

Here I would suggest you to use 在这里我建议您使用

Window.open('~/path/name.pdf');

using this you can display the pdf on browser and can save and print from browser. 使用此工具,您可以在浏览器上显示pdf并可以从浏览器保存和打印。

try this, Selected bytes from database and give to content. 试试这个,从数据库中选择字节并赋予内容。

byte[] content = "Select Your Bytes From Database";
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AppendHeader("content-disposition","attachment;filename=" + fileName);
HttpContext.Current.Response.ContentType = "Application/pdf";
//Write the file content directly to the HTTP content output stream.
HttpContext.Current.Response.BinaryWrite(content);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();

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

相关问题 创建PDF服务器端并在新的浏览器窗口或选项卡中打开 - Create PDF server side and open in new browser window or tab 将浏览器窗口的打印部分打印为PDF并发送到服务器进行传真 - Print Portion of the Browser Window to PDF and Send To Server to be Faxed 在服务器上保存生成的pdf - save generated pdf on server IE不会在浏览器中显示生成的PDF - IE will not display generated PDF in browser 服务器端HTML到PDF - Server Side HTML to PDF iTextSharp生成的PDF在浏览器中有效,但下载后不起作用 - iTextSharp generated PDF works in browser but not after download 通过Resposne发送内存中生成的.PDF文件 - Send .PDF file generated in memory via Resposne Send PDF to the browser rather than saving to the server - ASP.NET iText 7 C# Web Forms - Send PDF to the browser rather than saving to the server - ASP.NET iText 7 C# Web Forms iTextSharp生成的PDF:如何将pdf发送给客户端并添加提示? - iTextSharp generated PDF: How to send the pdf to the client and add a prompt? 如何在新浏览器 window 或服务器端 Blazor 应用程序的选项卡中的 Google.Protobuf.ByteString 中显示 pdf? - How can I display a pdf in a Google.Protobuf.ByteString in a new browser window or tab in a server side Blazor App?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM