Images Not Showing in PDF

I am unable to get the image to render in the PDF output. I am using an explicit url, as I thought that would work, but it still doesn’t produce any output. below is my code. Everything works except the image is not in the PDF output. The image appears if I point the image src to our development environment, but I have elevated permissions on that server. Do you need more than read permissions on that directory to be able to view the images?


Image in html:


C# to generate PDF:

Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense(“Siepe.Portal.Web.Aspose.Pdf.lic”);
license.Embedded = true;

string url = Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host + “:” + Request.Url.Port + “/Wires/Wire/Print/” + id;
System.Net.HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 3000;
request.Credentials = System.Net.CredentialCache.DefaultCredentials;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Encoding encoding = Encoding.GetEncoding(1252);
StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);

Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();

pdf.PageSetup.Margin.Left = pdf.PageSetup.Margin.Right = pdf.PageSetup.Margin.Bottom = pdf.PageSetup.Margin.Top = 5;
pdf.CompressionLevel = 9;

Aspose.Pdf.Generator.Section section = pdf.Sections.Add();
section.TextInfo.FontName = “Helvetica”;
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, reader.ReadToEnd());

text2.IsHtmlTagSupported = true;
text2.IsFitToPage = true;


section.Paragraphs.Add(text2);

byte[] buffer = pdf.GetBuffer();
return File(buffer, “application/pdf”);

Hi Michael,


Thanks for your inquiry. I’m afraid, All Aspose .NET components are recommended to run with Full Trust permission set. This is because Aspose .NET component sometimes need to access registry settings and files located in places other than the virtual directory e.g. for reading fonts etc. So you need to set the necessary permission for Aspose.Pdf. Hopefully it will resolve your issue.

Best Regards,

Hopefully?


So you don’t know if that is the issue or not and you are just saying to try that? The pdf renders fine, no security exception, everything else works except the image isn’t showing.

And why would it work if I simply point the img src to our development machine, seems like that wouldn’t be an issue of trust level at all since it is still running on the production server?

I would expect a more definitive answer than “hopefully that will fix it”

Hi Michael,


Thank you for you feedback. Please check Aspose.Pdf declaration about full trust permission. Usually your reported issue arises in web applications due to right issues.

Moreover, We have tested the scenario with an image from Aspose web server and it is working fine. Can you please try following image src and share the results?



We are sorry for the inconvenience caused.

Best Regards,

I was able to get this working by giving the IIS user full permission on the image directory, but why does that user need full permission just to read an image from a fully qualified url?


Typically you want to give this user the least rights possible, so why does this need elevated permission just to load an image?

Hi Michael,


We are further looking into the details of this problem and will get back to you soon.

Hi Michael,


It is good to know that you have managed to resolve your issue. However, We have logged a ticket, PDFNEWNET-36202, in our issue tracking system for further investigation regarding full permission issue of image loading. We will keep you updated about issue progress via this forum thread.

Best Regards,

Hi Michael,

Thanks for your patience. We would like to update you that, in shared server environment the user doesn’t need Full trust but at least High trust. As the user calls WebRequest.Create(url) method in the code therefore Medium level trust (usually used) is not enough. Please check the [MSDN link] for more information.

Medium trust:
It specifies a medium level of code access security, which means that, in addition to the restrictions for High, the ASP.NET application cannot do any of the following things by default:
Access files outside the application directory.
Access the registry.
Make network or Web service calls (using the System.Net.HttpWebRequest class, for example).

Hopefully it would help to understand the subjected scenario. Please feel free to contact us for any further assistance.

Best Regards,

Still not convinced this is the issue.

The page renders fine when requesting the page and it is on the same server, however the only way to get the image to render was to make it a fully qualified url that points to the image on a separate server.

For example:

http://production.example.com/default.html - This page renders fine, but the image on the page that lives at http://production.example.com/img/xyz.png does not render when converted to pdf even when using the full url to the image in the img tag.

Above page renders in pdf if I change the image path to http://beta.example.com/img/xyz.png

Hi Michael,


We are working over this query and will get back to you soon.

Hi Michael,


We are truly sorry for the inconvenience caused. We will appreciate if you please share a sample test project with us(with web.config file) to replicate the issue and provide you information accordingly.

Best Regards,

Hi Michael,


Thanks for your patience. Our development team is investigating the issue and needs following information from you for the purpose.

  • Please send us your test solution (or just part of it but web.config file should exist).
  • What is the value of request.Credentials = System.Net.CredentialCache.DefaultCredentials in your code snippet?
  • What are the trust permissions policies set on production/beta servers?
We are looking forward to your help in this regard.

Best Regards,