c# - 将 CSS 文件添加到 SelectPdf 转换器

标签 c# html-to-pdf converters selectpdf

如何向 SelectPdf 转换器添加/引入 CSS 文件?

我用过 SelectPdf用于将 HTML 字符串转换为 PDF 文件的库。

我的 CSS 文件在 cssFilePath

public byte[] Create(string htmlString)
{
   string cssFilePath = Path.Combine(Directory.GetCurrentDirectory(), "assets", "PruefReportDataTableFormat.css");

        string pdf_page_size = "A4";
        PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true);

        string pdf_orientation = "Portrait";
        PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), pdf_orientation, true);

        int webPageWidth = 1024;
        int webPageHeight = 0;

        HtmlToPdf converter = new HtmlToPdf();

        converter.Options.PdfPageSize = pageSize;
        converter.Options.PdfPageOrientation = pdfOrientation;
        converter.Options.WebPageWidth = webPageWidth;
        converter.Options.WebPageHeight = webPageHeight;

        PdfDocument doc = converter.ConvertHtmlString(htmlString);
        byte[] result = doc.Save();
        doc.Close();
        return result;
 }

最佳答案

我在 html 字符串的 header 标签中添加了 css 引用:


    <link rel='stylesheet' href='cssFilePath' type='text/css' media='all' />

关于c# - 将 CSS 文件添加到 SelectPdf 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54900429/

相关文章:

c# - 当从一个类继承以后会伤害到你?

c# - IQueryable<a> 到 list<b> 类型转换,当 a & b 使用 LINQ 具有相似的签名时

php - 如何将格鲁吉亚字体加载到 Dompdf

python - 如何在Python中解压结构体?

c - C 中的十进制到二进制转换

c# - 关于在 C# 中优化代码以提高速度的几个问题

c# - ADODB.Connection 包装器作为 System.Data.Common.DbConnection

wkhtmltopdf - tr wkhtmltopdf 中不需要的空间

javascript - 尝试在 javascript 中将图像转换为 Base64 字符串并尝试在 c# 中转换为字节数组,给出 Invalid character in base64 string 错误