从web方法下载动态pdf [英] Download dynamic pdf from web method
问题描述
我有这个Web服务C#,其中我有一个web方法,当通过jquery ajax请求调用时,使用iTextsharp库创建一个PDF文件,html数据作为参数传递给web方法。
这是代码
$(' #articlelink')。on(' click' , function (){
var articletitle = $( document )。find(' #articleTitle')。text();
var filename = articletitle;
var articletext = $( document )。find(' div#articleText')。html ();
var articledetails = $( document )。find(' DIV#articleDetails' 跨度>)HTML();
var htmlcontent = ' < div style =color:#ffffff; text-align:center; background-color:#4F9EC9; width:100%;>' + articletitle + ' < / div>< br />< br />< div>' + articletext + ' < / div>< br />< br />< div>' + articledetails + ' < / div>< br />';
var param = {filename:filename,HtmlContent:htmlcontent};
$ .ajax({
type: POST,
url: ../ MyWebService.asmx + / + GenerateArticlePDF,
data: JSON .stringify(param),
contentType: application / json; charset = utf-8,
dataType: json,
成功: function (响应){
var aid = getParameterByName(' aid');
var eid = readC ookie(' empid');
ArticleDownloadByUser(aid,eid);
alert(' PDF生成成功。');
},
错误: function (响应){
alert(' PDF生成失败。');
},
超时: 60000
});
});
这是网络方式
[WebMethod(Description = 文章PDF生成)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GenerateArticlePDF( string filename, string HtmlContent)
{
string status = ;
尝试
{
字符串 DocContent = < span class =code-string> ;
if (HtmlContent!= null )
{
DocContent = HtmlContent.Replace( ../ cklibrary /, http:// localhost:81 / MyApp / cklibrary /);
}
HttpContext.Current.Response.ContentType = application / pdf 跨度>;
HttpContext.Current.Response.AddHeader( content-disposition, attachment; filename = + filename + PDF跨度>)。
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
// HtmlTextWriter hw = new HtmlTextWriter(sw);
// articleText.RenderControl(hw);
StringReader sr = new StringReader(DocContent.ToString());
文件pdfDoc = 新文件(PageSize.LETTER,15f,15f,15f,15f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
// HttpContext.Current.Response.End();
HttpContext。 Current.ApplicationInstance.CompleteRequest();
status = success;
}
catch (例外情况)
{
status = ex.Message.ToString();
}
返回状态;
}
问题是动态生成的PDF文件没有下载。
我需要做些什么更改来下载pdf文件。
请指教。
提前致谢
我尝试过:
我是使用iTextsharp 5.5.8从传递给Web方法的html内容生成PDF。正确生成pdf文件但文件未下载。而是将pdf的内容作为json提供给ajax请求。
(' a#articlelink')。on(' click', function (){
var articletitle =
< blockquote>( document )。find(' #articleTitle' )。text();
var filename = articletitle;
var articletext =
( document )。find(' div#articleText')。html();
var articledetails =
I have this webservice C# in which i have a web method which when called via a jquery ajax request creates a PDF file using the iTextsharp library and the html data is passed as parameters to the web method.
This is the code
$('a#articlelink').on('click', function () {
var articletitle = $(document).find('#articleTitle').text();
var filename = articletitle;
var articletext = $(document).find('div#articleText').html();
var articledetails = $(document).find('div#articleDetails').html();
var htmlcontent = '<div style="color:#ffffff; text-align:center; background-color:#4F9EC9; width:100%;">' + articletitle + '</div><br/><br/><div>' + articletext + '</div><br/><br/><div>' + articledetails + '</div><br/>';
var param = { filename: filename, HtmlContent: htmlcontent };
$.ajax({
type: "POST",
url: "../MyWebService.asmx" + "/" + "GenerateArticlePDF",
data: JSON.stringify(param),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var aid = getParameterByName('aid');
var eid = readCookie('empid');
ArticleDownloadByUser(aid, eid);
alert('PDF generated successfully.');
},
error: function (response) {
alert('PDF generation failed.');
},
timeout: 60000
});
});
This is the web method
[WebMethod(Description = "Article PDF Generate")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GenerateArticlePDF(string filename, string HtmlContent)
{
string status = "";
try
{
string DocContent = "";
if (HtmlContent != null)
{
DocContent = HtmlContent.Replace("../cklibrary/", "http://localhost:81/MyApp/cklibrary/");
}
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
//HtmlTextWriter hw = new HtmlTextWriter(sw);
//articleText.RenderControl(hw);
StringReader sr = new StringReader(DocContent.ToString());
Document pdfDoc = new Document(PageSize.LETTER, 15f, 15f, 15f, 15f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
//HttpContext.Current.Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
status = "success";
}
catch (Exception ex)
{
status = ex.Message.ToString();
}
return status;
}
The issue is that the dynamically generated PDF file is not downloading.
What changes do i need to make to download the pdf file.
Please advise.
Thanks in advance
What I have tried:
I am using iTextsharp 5.5.8 to generate the PDF from the html content that is passed to the web method. The pdf file is generated correctly but the file is not downloading. Instead the content of the pdf are supplied as json to the ajax request.
('a#articlelink').on('click', function () { var articletitle =
(document).find('#articleTitle').text(); var filename = articletitle; var articletext =
(document).find('div#articleText').html(); var articledetails =
这篇关于从web方法下载动态pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!