Images in html to pdf using wkhtmltopdf in mvc 4

I am using wkhtmltopdf to convert html to pdf. I am using mvc 4. I was able to convert html to pdf. The only problem I am facing is that the images are not displayed. There is a small rectangle in which the image should appear. I have my images in the database, so when I get the html string in my controller, this shows how the image is displayed right before passing this string to the converter:

<img src="/Images/Image/GetImageThumbnail?idImage=300" alt=""/>

So, I think this approach does not work, because I pass the string to the converter so that the image does not display. Any ideas how to solve this problem if the images are in db?

+2
source share
1 answer

I am solving a similar problem by replacing src from src="/img/derp.png"to src="http://localhost/img/derp.png". I get the host part from the request that my controller receives.

// Here I'm actually processing with HtmlAgilityPack but you get the idea
string host = request.Headers["host"];
string src = node.Attributes["src"].Value;
node.Attributes["src"].Value = "http://" + host + src;

This means that the server must also be able to tear images directly from such URLs.

I assume this can be done using string.Replace if your HTML is in a string

string host = request.Headers["host"];
html = html.Replace("src=\"/", "src=\"http://"+host+"/"); // not tested
+2
source

All Articles