Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
ASP.NET
<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>

JavaScript
$('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
C#
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"}


Please point out what i am doing wrong here

What I have tried:

I am using itextsharp core 5.5.8 to generate pdf
Posted
Updated 2-Mar-16 23:56pm

1 solution

You're missing quotes around the string values in your JSON object.

Try something like this:
JavaScript
$('a#articlelink').on('click', function () {
 var filename = 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: JSON.stringify(param),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function () { alert('PDF generated successfully.'); },
    error: function () { alert('PDF generation failed.'); }
 });
});
 
Share this answer
 
v2
Comments
Christopher Fernandes 3-Mar-16 6:16am    
I tried it doesn't work
Richard Deeming 3-Mar-16 6:33am    
And the error is?
Christopher Fernandes 3-Mar-16 6:57am    
{"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. (1409): {\"filename : Test,HtmlContent : \u003cdiv style=\\\"color:#ffffff; text-align:center; background-color:#4F9EC9; width:100%;\\\"\u003eTest\u003c/div\u003e\u003cbr/\u003e\u003cbr/\u003e\u003cdiv\u003e\u003cp\u003eChristopher\u003c/p\u003e\\n\\n\u003cp\u003e\u0026nbsp;\u003c/p\u003e\\n\\n\u003cp\u003eChristopher\u003c/p\u003e\\n\\n\u003cp\u003e\u003cimg alt=\\\"\\\" src=\\\"../cklibrary/images/mud-sling.png\\\" style=\\\"height:261px; width:650px\\\"\u003e\u003c/p\u003e\\n\\n\u003cp\u003eTest\u003c/p\u003e\\n\\n\u003cp\u003e\u0026nbsp;\u003c/p\u003e\\n\\n\u003cp\u003echristopher\u003c/p\u003e\\n\\n\u003ctable border=\\\"1\\\" cellpadding=\\\"1\\\" cellspacing=\\\"1\\\" style=\\\"width:200px\\\"\u003e\\n\\t\u003ctbody\u003e\\n\\t\\t\u003ctr\u003e\\n\\t\\t\\t\u003ctd\u003eFirst Name\u003c/td\u003e\\n\\t\\t\\t\u003ctd\u003eLast Name\u003c/td\u003e\\n\\t\\t\u003c/tr\u003e\\n\\t\\t\u003ctr\u003e\\n\\t\\t\\t\u003ctd\u003eChristopher\u003c/td\u003e\\n\\t\\t\\t\u003ctd\u003eFernandes\u003c/td\u003e\\n\\t\\t\u003c/tr\u003e\\n\\t\\t\u003ctr\u003e\\n\\t\\t\\t\u003ctd\u003eCalvin\u003c/td\u003e\\n\\t\\t\\t\u003ctd\u003eFernandes\u003c/td\u003e\\n\\t\\t\u003c/tr\u003e\\n\\t\u003c/tbody\u003e\\n\u003c/table\u003e\\n\\n\u003cp\u003e\u0026nbsp;\u003c/p\u003e\\n\u003c/div\u003e\u003cbr/\u003e\u003cbr/\u003e\u003cdiv\u003e\\n \u003cdiv class=\\\"articledownload bg-color\\\"\u003e\\n \u003ca id=\\\"articlelink\\\" class=\\\"articledownloadlink\\\" target=\\\"_blank\\\" download=\\\"\\\"\u003edownload\u003c/a\u003e\\n \u003c/div\u003e\\n \u003ch3\u003e\u003cu\u003eArticle Details : \u003c/u\u003e\u003c/h3\u003e\\n \u003ch3\u003eAuthors\u003c/h3\u003e\\n \u003cdiv id=\\\"articleAuthor\\\" style=\\\"min-height: 10px;\\\"\u003eChristopher Fernandes, Claire Cylkowski\u003c/div\u003e\\n \u003ch3\u003eCategory\u003c/h3\u003e\\n \u003cdiv id=\\\"articleCategory\\\" style=\\\"min-height: 10px;\\\"\u003edefault-category\u003c/div\u003e\\n \u003ch3\u003eTag\u003c/h3\u003e\\n \u003cdiv id=\\\"articleTag\\\" style=\\\"min-height: 10px;\\\"\u003edefault-tag\u003c/div\u003e\\n \u003c/div\u003e\u003cbr/\u003e\"}","StackTrace":" 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"}
Richard Deeming 3-Mar-16 7:04am    
You must have missed something, because the value you're passing to the AJAX call doesn't have quotes around the filename value. If you'd used the code that I posted, that wouldn't happen.
Christopher Fernandes 3-Mar-16 7:18am    
Thanks, I had missed the param changes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900