HMTL to PDF conversion

Hi Team,

I am working on convert HTML to PDF, my issue is style are not applying from style block block.

My code:

        foreach (string contentPart in contentParts)
        {
            Aspose.Pdf.Page page = mergedPdfDocument.Pages.Add();

            if (pageSettingData != null)
            {
                PageSettingPdf(mergedPdfDocument, page, pageSettingData, pageIndex);
            }

            fullHtmlContent = part1[0] + contentPart + part2[1];
            HtmlFragment htmlFragment = new HtmlFragment(fullHtmlContent);
            page.Paragraphs.Add(htmlFragment);

            pageIndex++;
        }

        // Save merged PDF to memory
        MemoryStream memoryStream = new MemoryStream();
        mergedPdfDocument.Save(memoryStream);
        memoryStream.Position = 0;
        // Set up the PDF options
        HtmlLoadOptions options = new HtmlLoadOptions();

        Aspose.Pdf.Document newPdfDocument = new Aspose.Pdf.Document(memoryStream, options);
        AddPageNumber(newPdfDocument);


        // Save final document to response stream
        MemoryStream pdfStream = new MemoryStream();
        newPdfDocument.Save(pdfStream);

@kkumaranil485

Cause

The issue you’re experiencing with styles not applying from the style block during HTML to PDF conversion is likely due to the limitations of how the HTML content is processed. When using HtmlFragment, it may not fully support all CSS styles, particularly those defined in a <style> block.

Solution

To ensure that your styles are applied correctly, use HtmlLoadOptions with the HtmlFragment or load the HTML content directly into the PDF document. This method provides better handling of CSS styles.

Code

Here’s an updated version of your code that uses HtmlLoadOptions to load the HTML content directly into the PDF document:

foreach (string contentPart in contentParts)
{
    Aspose.Pdf.Page page = mergedPdfDocument.Pages.Add();

    if (pageSettingData != null)
    {
        PageSettingPdf(mergedPdfDocument, page, pageSettingData, pageIndex);
    }

    fullHtmlContent = part1[0] + contentPart + part2[1];

    // Use HtmlLoadOptions to load HTML content
    HtmlLoadOptions options = new HtmlLoadOptions();
    options.PageInfo.Margin = new MarginInfo(10, 10, 10, 10); // Set margins if needed

    // Create a new document from the HTML content
    Aspose.Pdf.Document htmlDocument = new Aspose.Pdf.Document(new MemoryStream(Encoding.UTF8.GetBytes(fullHtmlContent)), options);
    
    // Add the pages from the HTML document to the merged PDF document
    foreach (var htmlPage in htmlDocument.Pages)
    {
        mergedPdfDocument.Pages.Add(htmlPage);
    }

    pageIndex++;
}

// Save merged PDF to memory
MemoryStream memoryStream = new MemoryStream();
mergedPdfDocument.Save(memoryStream);
memoryStream.Position = 0;

// Save final document to response stream
MemoryStream pdfStream = new MemoryStream();
mergedPdfDocument.Save(pdfStream);

Next Steps

  1. Ensure that your HTML content includes all necessary styles within the <style> block.
  2. Test the updated code to verify that the styles are now applied correctly.
  3. If issues persist, review the complexity of your HTML and CSS, as some advanced styles may require additional handling.

Please let me know if you need further assistance!

Sample HTML for your reference

yes this not works.

  1. Ensure that your HTML content includes all necessary styles within the <style> block.
  2. Test the updated code to verify that the styles are now applied correctly.
  3. If issues persist, review the complexity of your HTML and CSS, as some advanced styles may require additional handling.

everything is proper

@kkumaranil485

Can you please share your sample HTML in .zip format along with the generated output PDF for our reference? We will test the scenario in our environment and address it accordingly.

TEST_PDFHMTL AND OUTPUT.zip (40.2 KB)

Same htlm file need to be exported as PDF.

@kkumaranil485

We received below output in our environment using your sample file. Can you please confirm if you are receiving similar output and it is not acceptable as per your requirements?
output.pdf (2.0 MB)

yes i got same output, styles not added.

I need to get the pdf as per shared html with styles applied.

@kkumaranil485

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-60215

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.