我正在使用 wkhtmltopdf 将 html 转换为 pdf。我正在使用 mvc 4。我能够将 html 转换为 pdf。我遇到的唯一问题是图像无法渲染。应该出现图像的地方有一个小矩形。我的图像在数据库中,因此当我在控制器中获取 html 字符串时,这就是在将此字符串传递给转换器之前显示图像的方式:
<img src="/Images/Image/GetImageThumbnail?idImage=300" alt=""/>
所以我认为这种方法不起作用,因为我将字符串传递给转换器,因此无法渲染图像。如果图像在数据库中,有什么想法如何解决这个问题?
我通过替换 src 解决了类似的问题src="/img/derp.png"
to src="http://localhost/img/derp.png"
。我从控制器收到的请求中获取主机部分。
// 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;
这意味着服务器还必须能够直接从 URL 中吐出图像。
我想如果你的 HTML 在字符串中,也可以用 string.Replace 来完成
string host = request.Headers["host"];
html = html.Replace("src=\"/", "src=\"http://"+host+"/"); // not tested
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)