在MVC 4使用wkhtmltopdf图像HTML到PDF
我使用wkhtmltopdf将HTML转换为PDF。我使用MVC 4.我可以将HTML转换为PDF。我唯一的问题是图像不渲染。有一个地方的形象应该出现小矩形。我有我的数据库中的图像,所以当我在我的控制器得到HTML字符串这是图像的显示方式之前,我这个字符串传递给转换器:
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 have is that images do not render. There is small rectangle where image should appear. I have my images in database so when I get html string in my controller this is how image is shown right before I pass this string to converter:
<img src="/Images/Image/GetImageThumbnail?idImage=300" alt=""/>
所以,我想,这种做法是行不通的监守我通过串转换器这样的图像无法呈现。任何想法如何,如果图像是分贝解决这个问题?
So I am thinking that this approach is not working becuase I pass string to converter so image cannot be rendered. Any ideas how to solve this problem if images are in db?
我解决由 SRC =更换SRC类似的问题/ IMG / derp.png
来 SRC =HTTP://localhost/img/derp.png
。我得到的主机部分从我的控制器接收请求。
I solve a similar issue 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 be also be able to vomit images directly from URLs like that.
我想这可能与与string.replace做,以及如果你的HTML是一个字符串
I guess it could be done with string.Replace as well if your HTML is in a string
string host = request.Headers["host"];
html = html.Replace("src=\"/", "src=\"http://"+host+"/"); // not tested