Rotativa ActionAsPdf background-image - PullRequest
       84

Rotativa ActionAsPdf background-image

0 голосов
/ 06 апреля 2020

Я использую Rotativa ActionAsPDF для создания PDF представления, но свойство background-image не будет работать. Однако, это работает, если я возвращаю Действие как обычное представление.

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 имеет UN C, сохраненный в базе данных в формате \\ ipaddress \ websitename \ logos \ mtulo go .png

Действие GetImage:

public ActionResult GetImage(string path)
{
    string contentType = MimeMapping.GetMimeMapping(Path.GetFileName(path));
    return File(@path, contentType);
}

Опять же, если я возвращаю только обычный просмотр, у меня нет проблем, это когда я меняю его на return new ActionAsPDF, когда у меня начинаются проблемы.

1 Ответ

0 голосов
/ 06 апреля 2020

Я получил исправление путем преобразования пути UN C в байты.

Обновленное действие:

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;
}
...