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();
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