Search code examples
c#html-to-pdfconvertersselectpdf

Adding CSS file to SelectPdf converter


How can I add/introduce a CSS file to SelectPdf converter?

I've used SelectPdf library for converting HTML string to PDF file.

my CSS file is in 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;
 }

Solution

  • I added the css reference in header tag in html string:

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