PDF Generated broken on inline styled HTML string

Hi Aspose,

I am trying to generate a PDF file using Aspose version 7.2 passing a styled HTML string, but when the PDF is generated styles appear broken.

The following is the piece of code I’m using.

public static byte[] GetPdfFromHtml(string html)
{
	// Get the license
	var license = new AsposeLicense();
	license.SetLicense(@"D:\Data\Aspose.Total.lic");
	
	// Instantiate an object PDF class
	var pdf = new Pdf();
	
	// add the section to PDF document sections collection
	var section = pdf.Sections.Add();
	
	// Create text paragraphs containing HTML text
	// and enable the property to display HTML contents within their own formatting
	var text = new Text(section, html);
	text.IsHtmlTagSupported = true;
	
	//Add the text paragraphs containing HTML text to the section
	section.Paragraphs.Add(text);
	
	// Specify the URL to find images
	pdf.HtmlInfo.ImgUrl = WebUtil.GetFullUrl("/");
	pdf.HtmlInfo.ExternalResourcesBasePath = WebUtil.GetFullUrl("/");
	
	// Return bytes
	using (var ms = new MemoryStream())
	{
		pdf.Save(ms);
		return ms.ToArray();
	}
}

This is Sample HTML string

<html>
<head>
    <style>
        body { margin: 0; padding: 0; }
        body * { box-sizing: border-box; }

        /* article pdf styling */
        .article-pdf { padding-top: 148px; box-sizing: border-box; }
        .article-pdf__logo-holder { position: absolute; top: 0; right: 40px; height: 143px; }
        .article-pdf__title { color: #ffffff; display: inline-block; margin: 0; padding: 6px 0; font-family: sans-serif; font-size: 28px; font-weight: bold; background-color: #E2171F; margin-bottom: 32px; padding: 10px 30px 10px 40px; box-sizing: border-box; }
        .article-pdf__content { font-family: sans-serif; font-size: 16px; }
        .article-pdf__description { color: #17365D; font-size: 24px; font-family: serif; font-weight: bold; font-style: italic; }
        .article-pdf__date { position: absolute; bottom: 0; right: 0; width: 100%; background-color: #ffffff; text-align: right; padding: 8px 40px; }

        /* article pdf cols */
        .article-pdf__col-wrapper { padding: 0 40px 40px; display: inline-block; width: 100%; }
        .article-pdf__col { float: left; }
        .article-pdf__col--left { width: 30%; padding-right: 20px; padding-top: 10px; }
        .article-pdf__col--right { width: 70%; }

        /* article copy */
        .article-pdf p,
        .article-pdf ul,
        .article-pdf h2, h3, h4, h5, h6 { margin-bottom: 23px; }
        .article-pdf ul,
        .article-pdf ol { margin: 0 0 23px; padding: 0; }
        .article-pdf ul li,
        .article-pdf ol li { margin-bottom: 5px; }
        .article-pdf h2, h3, h4, h5, h6 { font-family: sans-serif; font-weight: bold; color: #17365D; }
    </style>
</head>

<body>
    <article class="article-pdf">
        <h1 class="article-pdf__title">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</h1>

        <div class="article-pdf__col-wrapper">
            <div class="article-pdf__col article-pdf__col--left">
                <div class="article-pdf__description">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</div>
            </div>

            <div class="article-pdf__col article-pdf__col--right">
                <div class="article-pdf__content"><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum <a href="https://www.google.com">Google</a>.</p></div>
            </div>
        </div>

        <div class="article-pdf__date">29/10/2018</div>

        <footer class="article-pdf__disclaimer">
            This guidance was correct at publication 29/10/2018..
        </footer>
    </article>
</body>
</html>

@iresha

Would you please share the output generated at your end. Also, please try using latest version of the API with following code snippet:

Document doc = new Document();
Aspose.Pdf.Page asposepg = doc.Pages.Add();
HtmlFragment htmlFragment = new HtmlFragment(File.ReadAllText(dataDir + "HelloSample.html"));
asposepg.Paragraphs.Add(htmlFragment);
doc.Save(dataDir + "output.pdf");

Please note that you are using quite old version of the API and support is provided on the basis of latest version usage. Which is why we always recommend to us latest version. In case you notice any issue with 20.7v of the API, please let us know.

@asad.ali Thanks for your update.
This is the output generated
Action.pdf (4.0 KB)
Also I need to generated PDF make it downloadable (not saving in physical directory).

@iresha

The shared PDF is generated by older version of the API. Would you kindly use latest version as we requested you earlier and share your feedback with us. Please note that the support is provided on the basis of latest available version of the API.

Please check following code snippet to download the PDF file, after it is generated:

@asad.ali Thank for the update. That issue get sorted

But now images were missing in generated pdf.

I have updated the initializing load option as mentioned in the Image missing in generated PDF from HTML

But it is still missing the images.
My images are stored in “/asserts/images”

Aspose.Pdf.HtmlLoadOptions options = new Aspose.Pdf.HtmlLoadOptions("/asserts/images ");

When I check the html string (which passed to generate PDF), it returns rendered correctly.

@iresha

Please try to specify the path like “…/asserts/images” or “~/asserts/images” and if issue still persists, please share a sample HTML file in .zip format with us. We will test the scenario in our environment and address it accordingly.

Hi Asad,

I applied the code change as you requested but still have the issue.
Also noticed generated pdf not same as the html string provided.

I have attached the sample html template (HTML Template.zip (2.6 KB)) with the all styling and sample of the generated pdf (Generated.pdf (134.8 KB)).

This is the code what I used for generated the pdf.

 private static byte[] GeneratePDF(string html)
    {
        // Get the license
        var asposeLicenseFileName = ConfigurationManager.AppSettings["AsposeLicPath"];
        // instantiate the license class
        var license = new AsposeLicense();
        //pass only the name of the license file embedded in the assembly
        license.SetLicense(asposeLicenseFileName);

        var objLoadOptions = new HtmlLoadOptions("~/img");
        objLoadOptions.PageInfo.Margin.Bottom = 10;
        objLoadOptions.PageInfo.Margin.Top = 30;
        objLoadOptions.PageInfo.Margin.Left = 10;
        objLoadOptions.PageInfo.Margin.Right = 10;

        var doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), objLoadOptions);
        // Return bytes
        using (var ms = new MemoryStream())
        {
            doc.Save(ms);
            return ms.ToArray();
        }
    }

Please let me know if any information needed.

@iresha

We did not notice any missing image issue in output PDF while using your code snippet as following:

var objLoadOptions = new HtmlLoadOptions(dataDir + "img");
objLoadOptions.PageInfo.Margin.Bottom = 10;
objLoadOptions.PageInfo.Margin.Top = 30;
objLoadOptions.PageInfo.Margin.Left = 10;
objLoadOptions.PageInfo.Margin.Right = 10;

var doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(dataDir + "HtmlTemplate.html"))), objLoadOptions);
doc.Save(dataDir + "output.pdf");

dataDir is a path in the above code snippet where both HTML file and img folder are kept. We were able to notice though that output PDF was not as per the source HTML and for the sake of correction we have logged an issue as PDFNET-48633 in our issue tracking system. We will further look into details of it and keep you informed about its rectification status. Please be patient and spare us some time.

We are sorry for the inconvenience.

do we have an indicative timeline when this issue might be resolved ?

@ryanheiser

The ticket has been logged under normal support mode and will be resolved on first come first serve basis. The resolution time of the ticket may vary depending upon the number of issues logged prior to it and nature of complexity of the issues. We will however inform you as soon as we have some certain updates regarding ETA or resolution. Please give us some time.

HI Asad, I know that my company pays for the Aspose product and I think we have a support agreement what information do I need to provide to get this issue looked into under our support agreement ?

@ryanheiser

If you have a priority/paid or enterprise support subscription, you can login into paid support forum using the same email address which was used to subscribe. You can create a post there with the ticket reference i.e. PDFNET-48633 and it will be escalated to high priority accordingly.

Hi @asad.ali,

Can I know any update on this.
I’m still facing the styling issue.

Thanks,
Iresha

@iresha

The earlier logged ticket is currently under the investigation phase and as soon as its investigation is done, we will be able to share some updates about its ETA. We will surely inform you about its rectification status within this forum thread. Please give us some time.

We are sorry for the inconvenience.

Hi @asad.ali

Has there been any update on this?

Thanks,
Rich

@RichCodeehouse

The issue is still under investigation phase and we are afraid that we cannot provide any process update at the moment. As soon as the investigation is complete, we will let you know within this forum thread. Please give us some time.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFNET-48633) have been fixed in Aspose.PDF for .NET 20.11.