从c#web服务生成pdf [英] Generate pdf from c# web services

查看:72
本文介绍了从c#web服务生成pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个asp.net c#web应用程序,我在其中使用itextsharp从页面上的html内容生成pdf。



我写了pdf在ac#webservice中生成代码

这是代码

I am developing an asp.net c# web application in which i am using itextsharp to generate pdf from html content on the page.

I have written the pdf generating code inside a c# webservice
this is the code

<div class="childbody_container corner-val">
 <div class="childheadtag corner-val bg-color">
 <span class="white-text font-val">
  <asp:Label ID="articleTitle" runat="server" Text="Article title"></asp:Label>
 </span>
</div>
<div class="child-content">
 <div id="articleText" style="min-height: 300px;"  runat="server">
</div>
<div id="articleDetails">
 <div class="articledownload bg-color">
  <a id="articlelink" class="articledownloadlink" href="#" target="_blank"  runat="server" download>download</a>
 </div>
 <h3>Article Details : </h3>
 <h3>Authors</h3>
 <div id="articleAuthor" style="min-height: 10px;"></div>
 <h3>Category</h3>
 <div id="articleCategory" style="min-height: 10px;"></div>
 <h3>Tag</h3>
 <div id="articleTag" style="min-height: 10px;"></div>
</div>
</div>
</div>




$('a#articlelink').on('click', function () {
 var filename = JSON.stringify(readCookie('winuname'));

  var articletitle = $(document).find('#articleTitle').text();
  var articletext = $(document).find('div#articleText').html();
  var articledetails = JSON.stringify($(document).find('div#articleDetails').html());
  var htmlcontent = '<div style="color:#ffffff; text-align:center; background-color:#4F9EC9; width:100%;">' + articletitle + '</div><br/><br/>' + articletext;
                
  var param = 'filename : ' + filename + ',HtmlContent : ' + htmlcontent;
  $.ajax({
  type: "POST",
  url: "../MyWebService.asmx" + "/" + "GenerateArticlePDF",
  data: "{" + param + "}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function () { alert('PDF generated successfully.'); },
  error: function () { alert('PDF generation failed.'); }
 });
});





这是网络服务代码



this is the webservice code

using iTextSharp.text;
using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

[WebMethod(Description = "Article PDF Generate")]
    [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public void GenerateArticlePDF(string filename,string HtmlContent)
    {
        try
        {
            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(HtmlContent.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();
        }
        catch (Exception ex)
        {

        }
    }





我收到以下错误



I get the following error

{"Message":"Invalid JSON primitive: .","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}





请指出我在这里做错了什么



我尝试过:



我使用的是itextsharp core 5.5 .8生成pdf



Please point out what i am doing wrong here

What I have tried:

I am using itextsharp core 5.5.8 to generate pdf

推荐答案

' a# articlelink')。on(' click' function (){
var filename = JSON .stringify( readCookie(' winuname'));

var articletitle =
('a#articlelink').on('click', function () { var filename = JSON.stringify(readCookie('winuname')); var articletitle =


document )。find(' #articleTitle')。text();
var articletext =
(document).find('#articleTitle').text(); var articletext =


document )。 find(' div#articleText')。html();
var articledetails = JSON .stringify(
(document).find('div#articleText').html(); var articledetails = JSON.stringify(


这篇关于从c#web服务生成pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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