未获取 MVC 上的 IText7 设置外部样式表

问题描述

我是 ASP.NET MVC 和 IText7 库的新手。有一个问题,我想将 HTML 代码转换为 pdf,但外部 CSS 没有放在 html 代码上。我在方法中设置了样式表并将其添加到 html 正文中。

using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;

[HttpPost]
public IActionResult GetPDF(string GridHtml)
{

    //var report = new ActionAsPdf("Index");
    //return report;
    using (MemoryStream stream = new MemoryStream())
    {
        string info = "<html>" +
            "<head>" +
            "<link href=\"https://fonts.googleapis.com/icon?family=Material+Icons\" rel=\"stylesheet\" >" +
            "<link rel=\"stylesheet\" href =\"./materialize/css/materialize.min.css\">" +
            "<link rel=\"stylesheet\" href=\"./css/site.css\" />" +
            " <link rel=\"stylesheet\" href=\"./css/layout.css\" />" +
            "<link rel=\"stylesheet\" href=\"./css/custom.css\" />" +
            "</head>" +
            "<body>";
        GridHtml = info + GridHtml + "</body> </html>";
Debug.WriteLine(GridHtml);
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter(stream));
        pdfDocument.SetDefaultPageSize(PageSize.A4.Rotate());
        ConverterProperties converterProperties = new ConverterProperties();
        converterProperties.SetBaseUri("Content//wwwroot//");
        HtmlConverter.ConvertToPdf(GridHtml,pdfDocument,converterProperties);

        return File(stream.ToArray(),"application/pdf","Grid.pdf");
    }
}

有一行 converterProperties.SetBaseUri("Content//wwwroot//"); 根据我的理解应该设置从哪里看的基础。但是添加这个没有用。什么是正确的形式,因为我的 wwwroot 文件结构如图所示:

Picture

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)