Tuesday, March 13, 2012

Convert HTML to PDF using Sautin Soft Tool
[Generate PDF from HTML, ASPX, Images into your ASP.Net,WinForms and SharePoint applications]


There was a requirement in, one of my previous project is to convert SharePoint Page (aspx) in to a PDF and tat page create dynamically (it's not a static one). Clients' IT team have recommended to use Sautin Software tool for that.
Is It a Commercial Tool ?? If So Why we Used it ??
this is the first two question we got into our mind when we asked to do the task using this tool. "YES" It is a commercial tool. even we can use dll like iTextSharp  client recommend to use this because it's easy to handle and you don't need to write bunch of code to get the same result as u do with iTextSharp .
[FYI : use this link http://www.sautinsoft.com/products/convert-html-pdf-and-tiff-pdf-asp.net/html-to-pdf-jpeg-tiff-gif-to-pdf-asp.net.php to download or get more idea about sautin soft dll ] 


How To Use ?? 
It very easy to use all you need to do send the URL of the page which you need to convert to the dll and the path u need to save it as a pdf file (also you can create pdf bite stream and show it through the browser itself or give open/save option to the user )
In my project we have used pdf stream instead of saving file into default location. please refer the following code.


   // create pdf version object using SautingSoft namespace which responsible for  conversion         
            SautinSoft.PdfVision v = new SautinSoft.PdfVision();
  //when you purchase the dll you'll get the serial number please assign it  
            v.Serial = "0000000000"; (please don't use this number it's not a valid one)
  //declare byte stream  
            byte[] pdfBytes = null; 
 //Assign URL which need to convert
            string url =          string.IsNullOrEmpty(SPContext.Current.Site.WebApplication.GetResponseUri(Microsoft.SharePoint.Administration.SPUrlZone.Internet).OriginalString) ?
                SPContext.Current.Site.WebApplication.GetResponseUri(Microsoft.SharePoint.Administration.SPUrlZone.Intranet).OriginalString :
                SPContext.Current.Site.WebApplication.GetResponseUri(Microsoft.SharePoint.Administration.SPUrlZone.Internet).OriginalString;
           
//convert URL to pdf stream
            pdfBytes = v.ConvertHtmlFileToPDFStream(url + "/pages/partner_profile2.aspx?partnerid=" + Request.QueryString["partnerid"].ToString());


//show PDF
            if (pdfBytes != null)
            {
                Response.Buffer = true;
                Response.Clear();
                Response.ContentType = "application/PDF";
                Response.AddHeader("Content-Length", pdfBytes.Length.ToString());
                Response.AddHeader("Content-Disposition", "inline;filename=" +  DateTime.Now.Millisecond.ToString() + ".pdf");                
                Response.BinaryWrite(pdfBytes);
                Response.Flush();
                Response.End();
            }
Please consider on the highlighted area. we have a issue on this functionality. when we try to use this dll with authoring pages you cannot convert those authoring pages into PDF(you'll get a pdf file with error page "Page Cannot be Found"). 
But in our case we had Anonymous site also and this conversion work with that perfectly. we have chat with the Sautin Team, also but it cannot be done.

so in here what I have done is, Instead of giving the actual URL of the page I check whether it has Other URLs (like Anonymous ) then assign it. [SPContext.Current.Site.WebApplication.GetResponseUri(Microsoft.SharePoint.Administration.SPUrlZone.Internet).OriginalString ]. Give anonymous (internet URL) of the application.

Final Result ... 
Page which hold user detail.

when you click download PDF button. convert the page into PDF.
ok That's It ... 
HAPPY CODING





2 comments:

  1. using iTextSharp also we can convert HTML to PDF very easily

    ReplyDelete
  2. yes I agreed. as i mentioned The core of this commercial tool also running under iTextSharp but with some layouts and CSS markup it hard to handle and to maintain that you need ti write bunch of code. so if you are ok with license cost, then it's good to move with product like that. you can save many dev hours which more valuble than license cost. so simply Why we are using this the same reason we are using Telerik (http://www.telerik.com/). but for the beginners iTextSharp is the one.

    ReplyDelete