Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

i have developed a webpage which allow the user to enter the values just like a registration form.while saving the data i need to give an option for downloading the page as pdf format(which i have entered values).

what i did?
* i tried using itextsharp dll. the problem is i don't get all the content in the pdf files. example images and styles are not converted properly.
* i tried some third party tools like( SautinSoft's PdfVision,ConvertApi,pdfcrowd)assemblies. all the dll are getting the url as a parameter and converting to pdf. in that way the server side html code is converted into pdf.the values i have entered is not in the pdf.


sample code i got from net for (sautinsoft).
C#
SautinSoft.PdfVision v = new SautinSoft.PdfVision();
       v. PageStyle.PageSize.Letter();
       byte[] pdf = v.ConvertHtmlFileToPDFStream(@"http://en.wikipedia.org/wiki/Main_Page");
      
       if (pdf != null)
       {
           Response.Buffer = true;
           Response.Clear();
           Response.ContentType = "application/PDF";
           Response.AddHeader("Content-Disposition:", "attachment; filename=Result.pdf");
           Response.BinaryWrite(pdf);
           Response.Flush();
           Response.End();
}


for (itextsharp)
protected override void Render(HtmlTextWriter writer)
{
MemoryStream mem = new MemoryStream();
StreamWriter twr = new StreamWriter(mem);
HtmlTextWriter myWriter = new HtmlTextWriter(twr);
base.Render(myWriter);
myWriter.Flush();
myWriter.Dispose();
StreamReader strmRdr = new StreamReader(mem);
strmRdr.BaseStream.Position = 0;
string pageContent = strmRdr.ReadToEnd();
strmRdr.Dispose();
mem.Dispose();
writer.Write(pageContent);
CreatePDFDocument(pageContent);


}
public void CreatePDFDocument(string strHtml)
{

string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
// step 1: creation of a document-object
Document document = new Document();
// step 2:
// we create a writer that listens to the document
PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
StringReader se = new StringReader(strHtml);
HTMLWorker obj = new HTMLWorker(document);
document.Open();
obj.Parse(se);
document.Close();
ShowPdf(strFileName);



}
public void ShowPdf(string strFileName)
{
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
Response.ContentType = "application/pdf";
Response.WriteFile(strFileName);
Response.Flush();
Response.Clear();
}

note: i have taken code from several forums.

please let me know if u have better solution.

thanks.
dev
Posted

If you save the values you have entered to a database then try sending a query string with the saved id for instance, then just check on the page load if there is a query string , if there is , load the saved data and set the textbox values. In that way the page will be loaded with the values when itextsharp generates the pdf.

just remember when you do this
C#
byte[] pdf = v.ConvertHtmlFileToPDFStream(@"http://en.wikipedia.org/wiki/Main_Page");

its like opening up a new browser window and reloading the page. Nothing is entered
thats why you need to write a method to load the values first

Hope it helps
 
Share this answer
 
before converting to bytes we have to load the webpage.that done the tricks.


Thanks
Dev.
 
Share this answer
 

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