7

I'm using the Microsoft ReportViewer that comes with ASP.NET and have a report parameter that should be setting the value (path) of an image in my report. I'm providing the path as a complete URL right now, starting with http:// but have also tried this as an app relative path, site rooted path, etc. and for some reason the image is always showing as the red X when it exports to PDF. I'm just creating an instance of a control in code, setting the properties and exporting directly to the response stream so it acts a download.

I'm just not sure what the problem could be with the image not showing up, so if anyone has any ideas please let me know.

UPDATE 1

I've determined that I can embed the image with a URL if it is on my public web server but when I'm running in localhost the image won't embed. I have confirmed for localhost that if I paste the same URL into my browser the image will open fine. As far as I know, I don't have a proxy. So I can work around my issue, but I still don't understand what the problem is with localhost.

UPDATE 2

Forgot to mention that when the URL to the image is opened from a browser it works fine.

Jarrett Widman
  • 6,329
  • 4
  • 23
  • 32

7 Answers7

15

It is not possible for a PDF to contain a reference an external image (at least from my understanding). In order for an image to appear in the PDF, it must be embedded into the document. Therefore, to use an external image, your app must retrieve the image and store it in the document. The report viewer will try to do this for you.

Two possible answers:

First, in order for your app to package the image into the PDF, it must be able to retrieve the image from the URL you are specifying. If that URL is behind a proxy (from the perspective of your app server) and/or requires credentials to access, this will present a challenge with the default configuration of the report viewer.

If a proxy server is the issue, please see the settings to your web.config you can add below. You may also need to supply network credentials, so your app can authenticate to the proxy. There are lots of ways to solve this, but one of the easiest is to run your application as a service account on your domain that has rights to traverse your proxy. You can test this by running the site as you temporarily (should be temporary because this is a horrible security practice).

The image you are using could require credentials to access (try pulling up the image in Firefox with empty cookies and verifying whether credentials were required to access it). If it requires Windows authentication, the same solution to proxy security may apply to authentication required on the remote image. If it requires some other form of authentication, you may be better off downloading and embedding the image into your project.

It is also possible to download the image using other means in your code and convert it to a byte array for inclusion in the report. There are lots of examples of this on the web, including a Stack Overflow here.

Second, take a look at the following page:

http://msdn.microsoft.com/en-us/library/ms251715%28VS.80%29.aspx

Using external images in a ReportViewer report is not enabled by default. To use an external image, you must set the EnableExternalImages property in your code. Depending on your network configuration, you might also need to bypass proxy settings to allow the external image to appear. You can add the following settings to the Web.config file to bypass the local proxy. When modifying your Web.config file, be sure to specify the name of the proxy server that is used in your network:

<system.net>
<defaultProxy>
<proxy usesystemdefault = "false" bypassonlocal = "true" proxyaddress = "http://< proxyservername >:80/" />
<defaultProxy>
</system.net>

Hope one or both of these helps.

Jerry

Community
  • 1
  • 1
Jerry Bullard
  • 6,116
  • 18
  • 27
  • I forgot to mention that if your image is in your local app and you use Windows authentication, you may be running into a common problem where Windows authentication fails locally: http://support.microsoft.com/kb/896861 You should also troll the event log on your server for any kind of error or authentication failure to aid your diagnosis of this problem. Jerry – Jerry Bullard Aug 15 '09 at 21:13
  • What kind of authentication do you have on your local site? Did you pull up the image in IE or test with Firefox as I suggested? Do you have any relevant errors in your event log? – Jerry Bullard Aug 17 '09 at 23:58
  • I think I mentioned the image shows up fine in the browser. Also, there is nothing in the event logs when I generate the report. I appreciate your suggestions, but none of them seem to resolve the problem. I agree that the image could probably be retrieved and converted to a byte array to show, but I'd prefer to avoid that particularly since this is only a problem locally and not in live use. – Jarrett Widman Aug 19 '09 at 16:23
  • Editing question to mention that image URL works fine in browser as I didn't explicitly say so. – Jarrett Widman Aug 19 '09 at 16:25
  • Note that if you are using Internet Explorer, it will automatically pass your NT credentials to your local site by default. Did you try this in Firefox, Chrome, etc.? If you can rule out security as an issue, we can move on from this onto the next step. – Jerry Bullard Aug 19 '09 at 20:50
  • Firefox worked for viewing the image, didn't try anything other browsers – Jarrett Widman Aug 20 '09 at 00:19
  • I should have asked this before. Are you using local or remote processing (as described in the following link)? http://msdn.microsoft.com/en-us/library/ms251671%28VS.80%29.aspx If you are using remote processing, the remote server will have an entirely different "localhost", so it will in fact not find the image your report is requesting. To solve that, you should use a URL that can be accessed off your box such as http://your-machine-name/image... – Jerry Bullard Aug 20 '09 at 21:19
  • Could you be having some kind of firewall issue? Also, if you are running under ASP.NET web server, please try to host your app and the site serving the image in IIS locally. If/when you are in IIS, please check the IIS logs to see if your app is actually requesting the correct URL. It may be IIS will log a more appropriate error that will lead you to the solution. – Jerry Bullard Aug 21 '09 at 23:21
3

When passing external image filenames to ReportViewer parameters, pass the format like this: file://C:\app\images\pic.jpg. Anything else usually doesn't work well when deployed.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Roy Oliver
  • 338
  • 3
  • 10
2

Okay, so this was our solution. The web server did not recognize its own qualified DNS name as a URL, so we had to edit the Hosts file in the C:\Windows\System32\drivers\etc folder and add the host name as localhost. The line we added to the file was:

ourserver.ourdomain.com 127.0.0.1

pebcakcity
  • 21
  • 1
1

I don't think Adobe Reader (or maybe the PDF specification itself?) allows external content to be loaded for security purposes. I vaguely remember having a similar issue that had nothing to do with reporting services (I was dynamically generating PDFs and using variable logos and had to embed them).

Chris
  • 27,596
  • 25
  • 124
  • 225
1

Did you try a regular file path (c:/temp/somefile.bmp)? Reporting services local report reads the file from the disk and embeds it in the pdf file produced. Make sure that the identity of the app pool in IIS has read permission on the image file.

We are doing it and our images are placed in an img folder under the web site, along withe the rest of the web sites images. We avoid hard coding the path by using Server.MapPath(relative path).

Hope this helps

Andre
  • 41
  • 4
1

I fixed my problem with this:

//For local relative paths
string imgUrl = new Uri(HttpContext.Current.Server.MapPath("~/images/mylocalimage.jpg")).AbsoluteUri;

// OR

// For complete URLs
{
    ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; // This allows reportViewer to download image from url
    string imgUrl = /* Your image URL ("http://") */;
}
//Then pass imgUrl parameter as external source of your image. 
  • This one worked for me. Images wouldn't show because hosting provider disabled the default version of TLS that RDLC uses. Thank you. – Naruto Feb 17 '20 at 08:41
0
  1. Can the report viewer get an image from a relative url? I've never used it, so best to check that assumption.
  2. Have you tried using the Html.Content() helper to set the URL? Whenever I have issues with my urls its because I didn't use this to generate the correct url for the view.
  • The URL with http:// is definitely correct and using a relative URL doesn't seem to be making a difference, no matter what it is relative to. – Jarrett Widman Aug 12 '09 at 22:31