我正在使用Rotativa ActionAsPDF创建视图的PDF,但是background-image属性不起作用。但是,如果我将Action作为常规视图返回,它确实可以工作。
ReportController:
public ActionResult CertificateAsPdf(int courseid, string type, int owningmtuid, int? attendeeid, int? agencyid, int? canineCertificationId)
{
return new ActionAsPdf("Certificate", new { courseid = courseid, type = type, attendeeid = attendeeid, owningmtuid = owningmtuid, agencyid = agencyid, canineCertificationId = canineCertificationId })
{
FileName = "Certificate.pdf",
PageOrientation = Rotativa.Options.Orientation.Landscape,
};
}
public ActionResult Certificate(int courseid, string type, int owningmtuid, int? attendeeid, int? agencyid, int? canineCertificationId)
{
// Logic to get data
return View("MTUCertificate", model);
}
MTUCertificateView:
<style>
.mtulogo {
background-image: url("@Url.Action("getimage", "mtu", new { area = "dataadministration", path = Model.MTULogoImage })");
background-size: contain;
background-position: center;
background-repeat: no-repeat;
height: 200px;
}
</style>
我遇到的问题是背景图片未加载。路径参数Model.MTULogoImage在数据库中保存了UNC,格式为\\ ipaddress \ websitename \ logos \ mtulogo.png
GetImage操作:
public ActionResult GetImage(string path)
{
string contentType = MimeMapping.GetMimeMapping(Path.GetFileName(path));
return File(@path, contentType);
}
同样,如果我只返回普通视图,则没有问题,这是当我开始遇到问题时将其更改为return new ActionAsPDF
。
我最终通过将UNC路径转换为字节来解决此问题。
更新的操作:
public ActionResult Certificate(int courseid, string type, int owningmtuid, int? attendeeid, int? agencyid, int? canineCertificationId)
{
//mtu.MTULogoImage below formatted like \\ipaddress\websitename\logos\mtulogo.png
model.MTULogoImage = System.IO.File.ReadAllBytes(mtu.MTULogoImage);
model.MTULogoImageContentType = MimeMapping.GetMimeMapping(Path.GetFileName(mtu.MTULogoImage));
return View("MTUCertificate", model);
}
更新的视图:
.mtulogo {
background-image: url(@string.Format("data:{0};base64,{1}", Model.MTULogoImageContentType, Convert.ToBase64String(Model.MTULogoImage)));
background-size: contain;
background-position: center;
background-repeat: no-repeat;
height: 200px;
}