Hey guys,
I am really tired running into problems on generating the PDF file form the HTML page.
Can any1 help me generate a PDF file from the HTML page along with the user input. My Aspx page does not have tables and cells,
It has combo box, text box and dropdowns just like a form used to enter details. I have a code right now working but it jus generates the PDF based on the URL. and my URL does not change for the values selected for each and every dropdowns, so it just simply generates the initial form without the inputs.
I have tried using all the possible HTML to PDF converts and nothing works even itextSharp, nw i am using WnVhtmltopdf and running into the above mentioned issue.
Can anyone look into my code and give me a solution on getting the PDF after the user input or suggest me with someother converter and working solution please..
PdfConverter pdfConverter = new PdfConverter();
pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
pdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait;
pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal;
pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = true;
pdfConverter.PdfDocumentOptions.ShowFooter = false;
pdfConverter.PdfDocumentOptions.ShowHeader = false;
pdfConverter.LicenseKey = "Q2hzY3Jjc2N3bXNjcHJtcnFtenp6eg==";
string a = Request.Url.ToString();
byte[] downloadBytes = pdfConverter.GetPdfFromUrlBytes(a);
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition","attachment; filename=" + "Rendered.pdf" + "; size=" + downloadBytes.Length.ToString());
response.Flush();
response.BinaryWrite(downloadBytes);
response.Flush();
response.End();
Thanks in advance
Aaditya
Loading
Micke BlomqvistPosted Mar 4, 2011, 2:11 AM
Of course, my intention is to help you, otherwise I wouldn't answer you question. I also hope you realize that your question doesn't come with a "one simple answer". Using the url of the page, as you do, alwas returns the page as it is constructed on the server. The page is created on the server, not on the client side.
string a = Request.Url.ToString(); byte[] downloadBytes = pdfConverter.GetPdfFromUrlBytes(a);
The above code just tells the server to output the url from scratch as the page is originally created on the server, not on the client side. To get the hold of what the user wrote in the form you need to post the page back to the server (or using some AJAX technic to pass values via the XMLHttpRequest back to the server) grabbing the form value pairs there and then create the page again, holding those values you want.
You can not call a static url unless you want a static html reply back from the server.
Howard MahaffeyPosted Aug 18, 2015, 5:29 AM
Micke BlomqvistPosted Mar 4, 2011, 3:09 PM
Could you just zip the two files with the form and the code behind?
I could just quickly look them thru and give you a hinch of how to proceed using iTextSharp, no need of the other pages. You could send them via e-mail if you don't want to upload the code here.
Thanks!
Aaditya GanesanPosted Mar 4, 2011, 2:12 PM
I recently used the below iTextSharp code and it returned a PDF that did not open cos of incorrect format.
string filename = "Customer";
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
Response.Charset = "";
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/pdf";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
form1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
have no clue what and where it is going wrong.
Thanks a lot again,
Adi
Micke BlomqvistPosted Mar 4, 2011, 12:52 PM
Would it be possible to upload your form (aspx) and your code behind (aspx.cs)?
I could give you some code example with iTextSharp if you like.
Cheers
Aaditya GanesanPosted Mar 4, 2011, 9:13 AM
Thanks for your reply, but the prob is just like micke said, the page is used by clients across the company and they need to send the pdf through mail on a button click. Just like you said I tried itextsharp but i am stuck with either the PDF unable to open due to incorrect format..
thanks,
Aaditya
Suthish NairPosted Mar 4, 2011, 2:51 AM
Aaditya GanesanPosted Mar 3, 2011, 5:37 PM
your idea is what I actually I thought initially, but I really doino to change the URL for every object activity.
Just help me in case you know hoe to do this one.
Thanks
Adi
Micke BlomqvistPosted Mar 1, 2011, 6:49 AM
Would it be possible to load the page based upon a querystring, retriving all the values first and then posting the page again with those values? Like this:
page.aspx?txtBox1=value_box1&txtBox2=value_box2&combo1=selected_combo1 etc, etc, etc for each control you would like to save the value from.
Then you could perhaps call the page again, like this: byte[] downloadBytes = pdfConverter.GetPdfFromUrlBytes(page.aspx?txtBox1=value_box1&txtBox2=value_box2&combo1=selected_combo1);
Would that work?