Aspose PDF Header as Html string with CSS file reference

Aspose PDF header as Html string with CSS file reference.

I’m try in create a PDF header/footer with html string, styles classes are define in css file that is referenced inside html HEAD tag. PDF Document.Page Header is not supporting CSS file reference. Our goal is to insert different header as html template based on business rules

Example:

public static MemoryStream GeneratePdf2(MemoryStream memoryStream)
{

string htmlBody = @"<html>
                        <head>
                            <link href=""https://localhost:7067/css/site.css"" rel=""stylesheet"" />
                        </head>
                        <body >
                        <div class =""header-bg"">
                            <h1>Main Body</h1><br/>
                            <div>
                                <p>This is example pharagraph</p>
                            </div>
                            <div>
                                <p>Example content here!!!</p>
                            </div>
                        </div>
                        </body>
                    </html>";
var byteArray = Encoding.UTF8.GetBytes(htmlBody);
var hmtlStream = new MemoryStream(byteArray);

HtmlLoadOptions loadoptions = new HtmlLoadOptions();
loadoptions.PageInfo.Margin = new MarginInfo(0, 0, 0, 100);

            
Document pdfDocument = new Document(hmtlStream, loadoptions);

//----------------------------------------------------------------------------------------------------//

string htmlHeader = @"<html>
                        <head>
                            <link href=""https://localhost:7067/css/site.css"" rel=""stylesheet"" />
                        </head>
                        <body >
                        <div class =""body-bg"">
                            <h1>Main Header</h1><br/>
                            <h3>Sub Header</h3><p> Sub Title</p>
                        </div>
                        </body>
                    </html>";
// Add header and footer on all pages
foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
{
    page.Header = new HeaderFooter();
    page.Header.Paragraphs.Add(new HtmlFragment(htmlHeader)); ;
    page.Header.Margin = new MarginInfo(0,0, 0, 0);         
}
//----------------------------------------------------------------------------------------------------//

pdfDocument.Save(memoryStream);

return memoryStream;

}

@sathiya16

Please try to set the HtmlLoadOptions with the resource path as below:

HtmlLoadOptions options = new HtmlLoadOptions("https://localhost:7067/css/");

While using HtmlFragment, you need to specify its load options as well like below:

HtmlFragment hFrag = new HtmlFragment(html);
hFrag.HtmlLoadOptions = new HtmlLoadOptions("https://localhost:7067/css/");

Appreciate your message @asad.ali
But It doesn’t work. I tried base path as “https://localhost:7067/css/” and “https://localhost:7067/css/.site.css”. Both options are NOT working

@sathiya16

In that case, would you please share some more information which can help us in replicating the issue? Are you executing this code snippet in a Web Application that has CSS and other resources used in the HTML? Can you please provide a sample application in .zip format for our reference? It could help us in testing it accordingly and address the issue.