How to convert .aspx url to PDF

HI

I am calling an url upon clicking pdf button…from which i would like to call another .aspx and the out put which we get from the internal call , i would like to export it to a pdf…

I am using Aspose.pdf and the .net version i am using is 2.0

can you please help me on this… Thank you very much in advance

Hi Aravind,

If your requirement is to convert an .aspx page to PDF, you can check the following documentation link for the details and sample code regarding how to convert HTML to PDF.

How to Convert HTML to PDF using InLineHTML approach

If your requirement is different, please share some more details regarding your requirement and we will get back to you.

Sorry for the inconvenience,

Hi Nausherwan Aslam,

Thank you very much for your reply…

Problem i am facing using the HTML to pdf is… The images are not getting added to pdf which are present as part of HTML page which i am calling, so can you please help me on this…

I think here we are binding only Text and how about images, since There is a line of code stating image URL as below:

// Specify the URL which serves as images database
pdf.HtmlInfo.ImgUrl …

What exactly is required to be placed here, since my images are getting dynamically bind to the URL which i am calling using HTTPWebRespose and which i am storing in a memorystream…

What went wrong Is what i am not getting…Below is a snippet of code which i am writing in my web page to call Export to PDF…

{
System.IO.MemoryStream mstream = new System.IO.MemoryStream();

Pdf pdf = new Pdf();
Random r = new Random();
// add the section to PDF document sections collection
Aspose.Pdf.Section section = pdf.Sections.Add();

servername = Request.ServerVariables[“server_name”].ToString();
_applicationName = Convert.ToString(ConfigurationManager.AppSettings[“ReplaceAppName”]);
varIP = ConfigurationManager.AppSettings[“ServerIP”].ToString();
varPort = long.Parse(ConfigurationManager.AppSettings[“ServerPort”]);
varPath = “D:\temp\” + “Audit " + Math.Round(Convert.ToDouble(r.Next(10000))) + 1 + “.pdf”;

// The address of the web URL which you need to convert into PDF format
string WebUrl = “http://” + servername + _applicationName + “/ExportToPDF/AuditPDFReport.aspx?auditId=” + _auditId;

// create a Web Request object to connect to remote URL
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(WebUrl);

// set the Web Request timeout
request.Timeout = 10000; // 10 secs

// Retrieve request info headers
HttpWebResponse localWebResponse = (HttpWebResponse)request.GetResponse();

// Windows default Code Page (Include System.Text namespace in project)
Encoding encoding = Encoding.GetEncoding(1252);

// Read the contents of into StreamReader object
StreamReader localResponseStream = new StreamReader(localWebResponse.GetResponseStream(), encoding);

// Read the contents of HTML file into StreamReader object
// StreamReader Stream = File.OpenText(Server.UrlDecode(”…/" + servername + _applicationName + “/ExportToPDF/AuditPDFReport.aspx?auditId=” + _auditId));

// Specify the URL which serves as images database

// pdf.HtmlInfo.ImgUrl = “http://localhost:4001/Audit/ExportToPDF/AuditPDFReport.aspx?auditId=882”;

pdf.HtmlInfo.ImgUrl = varpath;

Aspose.Pdf.Text txted = new Aspose.Pdf.Text(section, localResponseStream.ReadToEnd());

txted.IsHtmlTagSupported = true;
section.Paragraphs.Add(txted);

[//pdf.Save](https://pdf.save/)(varPath);

[//localWebResponse.Close](https://localwebresponse.close/)();
[//localResponseStream.Close](https://localresponsestream.close/)();

// Save the document
pdf.Save(mstream);

Response.Clear();
Response.Buffer = true;
Response.AddHeader(“cache-control”, “private”);
Response.AddHeader(“Content-disposition”, "attachment; filename=Audit " + Math.Round(Convert.ToDouble(r.Next(10000))) + 1 + “.pdf;”);
Response.AddHeader(“Content-type”, “application/x-pdf”);

mstream.WriteTo(Response.OutputStream);
}
``

Expecting a quick reply from Aspose Support team… Thank you very much in advance…

Hi Aravind,

Well, you need to provide the path of the folder/source to HtmlInfo.ImgUrl from where the images (specified in the HTML) can be retrieved to be included in the generated PDF file. Please provide the name of the folder where the source images are present. If you don’t specify the HtmlInfo.ImgUrl, images will not get loaded. If you still face any issue, please create a sample application and post it here to show the issue, we will check it and get back to you.

Sorry for the inconvenience,

Hi

Thanks for your reply.. But i am having a probelm here..

If you look at the code, i am internally calling an aspx page... i.e.,

string WebUrl = "http://" + servername + _applicationName + "/ExportToPDF/AuditPDFReport.aspx?auditId=" + _auditId;

And in that aspx page - i am binding images using a user control ( ImageHandler.ashxImageHandler.ashx) which dynamicaly binds images based on the category,

So when i call this url i am unable to get those images in the PDF file... So how can i bind images and data - both ?

do i need to use a different approch for this ? can you please do suggest on this...

I am here by attaching a sample page1, page2 images - from which you can understand how my pdf should look like after binding the data...

Hi Aravind,

Thank you for the feedback.

We would request you to please create a sample application and post it here so we can understand and test your issue and get back to you soon.

Sorry for the inconvenience,

Hi Aravind,

After further discussion internally in the team, I am sorry to inform you that Aspose.Pdf for .NET might not work in your specific scenario where you are loading the image into a user control dynamically. Aspose.Pdf for .NET only supports simple HTML files and the images (which you are loading dynamically in the user control inside your aspx) will not get included in the resultant PDF file.

We are sorry for the inconvenience.