如何使用 Reserved.ReportViewerWebControl.axd.调用导出 PDF.来自后端 C# 代码



我的目标是调用ExportBaseUrl Link,这是由Rdlc Exprot PDF按钮给出的,在C#端,我想这样做,因为有285个报告,每个报告都有diff参数,所以这将花费很多时间。

我已经研究过一个解决方案,但需要 15 分钟才能将 2 页 RDLC 加载到 pdf。 由于响应迟到或发生一些僵局,它需要时间,

这就是我正在做的。 JS文件

var reportViewerName = ControlName; //Name attribute of report viewer control.
var src_url = $find(reportViewerName)._getInternalViewer().ExportUrlBase + 'PDF';
var contentDisposition = 'AlwaysInline'; //Content Disposition instructs the server to either return the PDF being requested as an attachment or a viewable report.
var src_new = src_url.replace(/(ContentDisposition=).*?(&)/, '$1' + contentDisposition + '$2');

window.open("/printPDF.asx?url=" + encodeURIComponent("http://localhost:5402"+src_new));

打印PDF.aspx文件是这样的

using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace WebApp.WebAPI
{
/// <summary>
/// Summary description for printPDF
/// </summary>
public class printPDF : IHttpHandler
{
public  void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");

string url = context.Request.QueryString["url"];
// Download data.
DownloadFile(url, context.Server.MapPath("~/pdf/pdffile.pdf"), context.Request).Wait();
PdfDocument pdfDoc = new PdfDocument(new PdfReader(context.Server.MapPath("~/pdf/pdffile.pdf")), new PdfWriter(context.Server.MapPath("~/pdf/pdffileAuto.pdf")));
// add content
PdfAction action = PdfAction.CreateJavaScript("this.print({bUI: true, bSilent: true, bShrinkToFit: true});");
pdfDoc.GetCatalog().SetOpenAction(action);
pdfDoc.Close();
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition",
"AlwaysInline;filename="FileName.pdf"");
context.Response.BinaryWrite(File.ReadAllBytes(context.Server.MapPath("~/pdf/pdffileAuto.pdf")));
context.Response.Flush();
context.Response.End();
}
public async Task DownloadFile(string url, string destinationPath, HttpRequest req)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
var encoding = new UTF8Encoding();
request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-IN");
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
request.Accept = "text/html, application/xhtml+xml, image/jxr, */*";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
request.KeepAlive = true;
request.Proxy = null;
request.CookieContainer = new CookieContainer();
Uri target = new Uri("http://localhost:5402/");
foreach (String item in req.Cookies)
{
request.CookieContainer.Add(new Cookie(item, req.Cookies[item].Value) { Domain = target.Host });
}
await request.GetResponseAsync().ContinueWith(t1 =>   
{
using (var responseStream = t1.Result.GetResponseStream())
{

if (responseStream == null)
return;
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
using (FileStream fileStream = File.Create(destinationPath))
{
while ((bytesRead = responseStream.Read(buffer, 0, bufferSize)) != 0)
{
fileStream.Write(buffer, 0, bytesRead);
}
}
}
t1.Result.Close();
});
}
public bool IsReusable
{
get
{
return false;
}
}
}
}

线等待请求。GetResponseAsync((.继续(t1 =>

使用异步和不异步大约需要 15 分钟的时间一次,第二次进入死锁/Freez

此外,由于 URL 抛出 500 个内部服务器错误,我不得不添加 cookie。

如果我直接在浏览器上调用 url,它会在 1 秒内运行。

因此,如果有人知道问题所在或可以提供帮助,那将是非常大的帮助。

提前感谢您的帮助。

还行

我发现了什么问题,

问题是选项卡请求打印PDF.aspx并且该页面请求同一站点上的其他URL。 因此,直到 PrintPDF.aspx响应完成到选项卡 (HttpWebRequest( 不会被调用。

有什么原因吗? 我在web.config中设置了maxconnection。

我必须通过制作 2 个差异文件来修复它,第一个 ASPX 文件显示页面并在线程中生成 pdf,并在页面加载调用 Ashx 文件时检查文件是否已生成,如果生成则返回文件。

没有其他方法,所以我通过 2 个链接重定向调用修复了它。

感谢所有帮助过我的人。

最新更新