首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rotativa ActionAsPdf背景-图像

Rotativa ActionAsPdf背景-图像
EN

Stack Overflow用户
提问于 2020-04-07 00:29:49
回答 1查看 554关注 0票数 0

我正在使用Rotativa创建一个视图的ActionAsPDF,但是背景图像属性不起作用。但是,如果我将Action作为常规视图返回,它确实可以工作。

ReportController:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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>

我遇到的问题是背景图像不能加载。path参数UNC以\ipaddress\websitename\logos\mtulogo.png格式保存在数据库中

GetImage操作:

代码语言:javascript
复制
public ActionResult GetImage(string path)
{
    string contentType = MimeMapping.GetMimeMapping(Path.GetFileName(path));
    return File(@path, contentType);
}

同样,如果我只返回一个常规视图,我没有问题,当我开始有问题时,我将它更改为return new ActionAsPDF

EN

回答 1

Stack Overflow用户

发布于 2020-04-07 01:28:04

我最终通过将UNC路径转换为字节来修复这个问题。

更新的操作:

代码语言:javascript
复制
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);
}

更新的视图:

代码语言:javascript
复制
.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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61064483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档