Reading images from localhost (or any intranet address)

Hi,


We are developing an application which will run on a secure intranet where users are authenticated using windows authentication. We are attempting to convert a HTML report to PDF. I can read images from external sites fine (such as bbc.co.uk) and these display in the PDF, however when I try to include an image from localhost when I run the application through IIS express no image displays. I tried using an image on our intranet and again no image displays. Below is very basic example we put together to try and isolate the problem.

C:\file.html


C# code:
Document doc = new Document(@“C:\file.html”, options);
doc.Save(@“C:\file.pdf”);
PDF is generated but no image is visible. Fiddler gives us the following response from our intranet server.

You are not authorized to view this page


You do not have permission to view this directory or page using the credentials that you supplied because your Web browser is sending a WWW-Authenticate header field that the Web server is not configured to accept.


Please try the following:

  • Contact the Web site administrator if you believe you should be able to view this directory or page.
  • Click the Refresh button to try again with different credentials.

HTTP Error 401.2 - Unauthorized: Access is denied due to server configuration.
Internet Information Services (IIS)

When I replace the image with http://localhost:60700/Content/Images/calendar.png it appears no request is even made. Our images are stored in a database and accessible through an endpoint so we need to be able to request these images through a URL (either a relative or absolute path).

Please could someone look into the issues we are having an give us a response ASAP.

Many thanks

Hi Pranav,


Thanks for contacting support.

I have tested the scenario using following code snippet and as per my observations, the image is properly appearing in resultant PDF document. In order to test the scenario, I have created a web application and have placed an image over the web page and then have referenced the image hosted over my application in sample HTML page. Following are the contents of HTML which I am trying to convert to PDF format.

Can you please share some details regarding your working environment or some sample application which an help us in replicating this issue, so that we can again test the scenario at our end. We are sorry for your inconvenience.

[HTML]

[C#]

Document doc = new
Document(@“C:\pdftest\4.html”,
new HtmlLoadOptions());<o:p></o:p>

doc.Save(@"C:\pdftest\4_Converted.pdf");

Our application is a MVC application. It is using Windows Authentication. <?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" />

If I change it to Anonymous authentication and disable windows authentication, it works fine.

How do I set credentials
for the call aspose is trying to make to get this image? Is there a
standard way of doing this?v

Hi Pranav,

In case you need to render the web page which requires authentication, you may consider using the following code line

Pdf.Credentials = new System.Net.NetworkCredential(“test”, “test”);

Or you may consider using the following code lines to provide authentication code when trying to read the contents of web page.

[C#]

// create Network credential object and specify the user name, password and domain name

System.Net.NetworkCredential network_credential = new System.Net.NetworkCredential(“nayyer”, “nayyer”,“mydomain”);

// Create a WebClient object

System.Net.WebClient wc = new System.Net.WebClient();

// Pass Network credentials object to WebClient object

wc.Credentials = network_credential;

Also please note that when using pdf.HtmlInfo.ExternalResourcesBasePath, you can either pass some drive path or enter the URL at which external resources are placed. In the event of any further query, please feel free to contact. We are sorry for your inconvenience.

We are facing similar issue with Images. We are generating the HTML which has images into it which are from external Resources. We are using Aspose.Pdf.Document class for saving the pdf
Code:
Document pdfDoc = new Document(HTMLMemoryStream, htmlLoadOptions)
we are not sending any html load options currently,
We also tried passing the htmlLoadOptions.ExternalResourcesCredentials options but that didnot worked.

@rajuwagh,

Kindly create a small project application which reproduces this problem in your environment, and then send us a Zip of this project. We will investigate and share our findings with you.