Create PDF from HTML snippet using PdfSharp with external CSS classes included in HTML

I would like to know if there is a way by which I can

Create PDF from HTML snippet using PdfSharp having external CSS

classes included in the HTML

.

I found a way to generate a PDF using HTML content, but my css classes are not being applied to the PDF.

This is how I do it for a simple html string:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Drawing;
using TheArtOfDev.HtmlRenderer.PdfSharp;
using PdfSharp;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PdfDocument pdf = PdfGenerator.GeneratePdf("<h1>tests</h1><p>test para</p>", PageSize.A4,20,null,null,null);
        pdf.Save(@"C\testPDF.pdf");

    }
}

      

And so it is: enter image description here

But I want to include an HTML line like this:

<h1>tests</h1><p class='para1'>test para</p>

      

Is there a way to achieve this? Using a different library is not a problem only that the library used must be open source.

+3


source to share


1 answer


You can include your style in the chapter section and it will display correctly:

    var testHtml = @"<head>
        <style>
            .test {
                background-color: linen;
                color: maroon;
            }
        </style>
    </head>
    <body class=""test"">
        <p>
            <h1>Hello World</h1>
            This is html rendered text with css and image.
        </p>
        <p>
            <img src=""http://fc62.deviantart.com/fs11/i/2006/236/d/e/The_Planet_Express_Ship_by_gasclown.jpg"" height=""100"" width=""100"">
        </p>
    </body>";

      



What's more - you can just add the url to your stylesheet and it should work!

+4


source







All Articles