Forum How do I...?

Images not showed in pdf ASP.NET C#

JorenWillems
Hi

Images aren't showed in my pdf. All the rest does. Here's my code

Target aspx:

...
<div id="header" runat="server"></div>
...

Target aspx.cs:

header.innerthtml = @"<img runat='server' id='myImage' src='images/header.jpg' />";

Convert:

Prince p = new ...;
p.convert("http://localhost:50377/Docubis/Target.aspx", this.Response.OutputStream);
mikeday
Are any errors or warnings showing up in the Prince output log?
JorenWillems
There isn't an output log....
It just shows the pdf without the images
mikeday
You can call p.SetLog("...filename...") before calling Convert() to specify where error and warning messages should be saved. Note that the ASP page needs to have permissions to access the filename that you specify.

I'm not sure when the innerhtml line is applied, if you access the http://localhost:50377/Docubis/Target.aspx URL in the browser and view source, is the <img> element correctly included?
JorenWillems
One of these?

p.SetLog("C:/Docubis/");
p.SetLog("C:/Docubis/logile.log");
p.SetLog("C:/Docubis/logfile.txt");
JorenWillems
I have this logfile:

Thu Apr 8 08:39:32 2010: ---- begin
Thu Apr 8 08:39:32 2010: Loading document...
Thu Apr 8 08:39:32 2010: http://localhost:50377/Docubis/images/logo_porsche.jpg: warning: Unknown image MIME type: text/html
Thu Apr 8 08:39:32 2010: http://localhost:50377/Docubis/logos/mcdonalds-logo.gif: warning: Unknown image MIME type: text/html
Thu Apr 8 08:39:32 2010: http://localhost:50377/Docubis/logos/KBC.jpg: warning: Unknown image MIME type: text/html
Thu Apr 8 08:39:32 2010: Converting document...
Thu Apr 8 08:39:32 2010: finished: success
Thu Apr 8 08:39:32 2010: ---- end

The mistake seems to be with the image type (text/html). How must I change this?

thx
mikeday
It depends on how the image is being served up. If it's a static file, the web server should be smart enough to specify the correct MIME type based on its file extension. If it's being generated by a dynamic page, that page will need to be reviewed to check the Content-type header that it is using for the HTTP response. Another possibility is that the URL requires authentication or cookies, and is redirecting to a login page which has type text/html. You can check this by using curl, wget, or just going to the URL in a new browser window with cookies cleared and seeing what you get.
JorenWillems
The image is loaded dynamically:

BLTempPart blTempPart = new BLTempPart();
TemplatePart tempPart = blTempPart.getTempPartByDocPartControlId(docParts[i].ControlId);

HtmlGenericControl genControl = (HtmlGenericControl)FindControl(tempPart.CssId);

string ImagePath = "../images/" + docParts[i].Value;
genControl.InnerHtml += @"<img height='100px' runat='server' width='500px' src='" + ImagePath + "' /><br />";
JorenWillems
I saw another solution this forum: turn off authorization. I did this and it worked! But isn't there another way because my security is gone. I know there's something like this:

<location path="Pdf.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>

But this isn't working
mikeday
Okay, if it works with authorization disabled then it must be a redirect problem. You can use HTTP basic authorization with Prince, but if it requires a login process and uses cookies then that gets very complicated, and probably isn't worth it. Another option is to disable authorization, but only for the images (assuming they don't contain sensitive information). This will require checking your server documentation, we can't really help with this sorry. A final option is to disable authorization if the request is coming from the local machine, as I assume you are running Prince on the same machine. But I don't know if your server will support this.
JorenWillems
I figured it out. I disabled formsauthentication and enbaled windowsauthentication. Then in my masterpage in the Page_Init event I check the value of the Session["user"]. Is this value null, then I redirect the user to Login.aspx.
Thanks for the help bro!
mikeday
Excellent! Glad to hear it works, and thanks for posting the solution, it may help other people too.