在MVC 4使用wkhtmltopdf图像HTML到PDF [英] Images in Html to PDF using wkhtmltopdf in mvc 4

查看:571
本文介绍了在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

这篇关于在MVC 4使用wkhtmltopdf图像HTML到PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆