Rotativa / Wkhtmltopdf images not displaying
Asked Answered
A

7

9

I am currently creating an MVC 4 web application.

I have an action that has a base background image which is always the same then an arrow image which changes in degrees depending on the information that is collected within the action.

I call this action using

<img src="@Url.Action("trend-image", "NonReg_Reports")" alt="background" width="245" height="105" />

This works fine when it is called and is just displaying a HTML webpage. It gives me the HTML.

<img src="/nonreg-report/01495344/trend-image" alt="background" width="245" height="105">

I am then using Rotativa / Wkhtmltopdf to convert this HTML page into a PDF. This runs the same piece of code as above.

The problem is I am just getting a white box with the alternative text in it if I use the code above.

If I use <img src="~/Content/images/trend_back_medium.png" alt="astuff" /> which is the actual background image in my project it works fine.

The image from the code above can not be saved anywhere due to how it is used. I am thinking that it is a path problem, but after trying lots of different things I can still not get it to work.

Any suggestions or help would be greatly appreciated Thank you.

Annemarie answered 4/1, 2013 at 16:10 Comment(0)
B
19

I had the same problem. Normal html view was ok with pictures, pdf was not display it. I used Server.MapPath to point to picture and it was ok...

<img src="@Server.MapPath("~/Folder/SubFolder/Subfolder/ImageName.png")" />

I hope this will help

Adam

Badger answered 14/7, 2014 at 11:3 Comment(2)
Thanks! It helped me with a similar issue that I had while referencing css files in my layout!!! <link href="@Server.MapPath("~/Content/css/main.css")" rel="stylesheet" media="screen" />Luckett
Using the style attribute on a TD correctly showed an image on the development machine but would not work on the live server. I had to resort to this method to get the image to display in a PDF on the live box. Anonymous access (as some have suggested) is not an option for me on the live boxCraig
O
4

I faced the same problem and I found a workaround, not a nice one, but it suits my needs.

It seems that Rotativa(Wkhtmltopdf?) have a problem with images which are returned from http handler (at least my case). So to handle this situation, in controller action before return new ViewAsPdf... my code manually save file to some local 'temp' folder and replace 'src' of images in rendered view with paths to that local files in server path manner, eg: http://localhost/img/temp/trend-image.png. This is not a nice solution, if you already found better one, let me know.

Oleg answered 3/5, 2013 at 19:5 Comment(0)
C
2

Adding the attribute [System.Web.Mvc.AllowAnonymous] to the controller method returning the image solved the problem for me.

Clothesbasket answered 2/2, 2015 at 9:23 Comment(0)
E
1

I think it might come down to basically the ".png" bit being missing.

You could add a mapping like this /nonreg-report/01495344/trend-image.png » /nonreg-report/01495344/trend-image and have both of the urls active, why not? That way wkhtmltopdf is fooled and you still have your dynamic image without saving it anywhere on disk. Dynamic urls can work, I do that for live-converting gif2png for wkhtmltopdf.

Also it might help to have an absolute url, which I think you can do with something like this:

src="@Url.Action("trend-image", "NonReg_Reports", null, Request.Url.Scheme)"
Extrinsic answered 4/10, 2013 at 12:10 Comment(0)
M
1

It seems to work okay via css...

I was trying to display a logo at the top of the page and switched it with a div and set the height and width to that of the image.

CSS:

div.logo {
    background-image: url("/images/logo/logo.png");
    height: 53px;
    width: 185px;
}

HTML:

<div class="logo"></div>
Michaelson answered 24/11, 2014 at 14:22 Comment(0)
A
0

I found that with my pages, the images were not showing because they were .gif files. Rotativa can have problems with gifs. When I changed the images to .jpg or .png everything worked well.

Augmentative answered 3/10, 2013 at 23:11 Comment(0)
S
-1

If you are specifiying height and width then make sure you should write it in style tag like <some-tag style="height: 100px; width:100px">

Selinski answered 5/8, 2017 at 10:33 Comment(1)
How would this help?Highpitched

© 2022 - 2024 — McMap. All rights reserved.