Convert HTML String to PDF Byte Array C#

Hi,

I am trying to convert HTML string to PDF byte array C# with Aspose.Words.Document and having problem with Table elements which width is defined as percent.

I have attached a text file with .txt extension that contains the source HTML string which needs to be replaced to .html and I am trying to convert it with this C# code snippet.

public static byte[] HtmlToPdf(string strFileName)
{
    byte[] htmlDocument = strFileName;
    byte[] downloadBytes = null;
    using (MemoryStream readStream = new MemoryStream(htmlDocument))
    {
        using (MemoryStream writeStream = new MemoryStream())
        {
            Document doc = new Document(readStream);
            foreach (Section sec in doc.Sections)
            {
                sec.PageSetup.PaperSize = PaperSize.A4;
                sec.PageSetup.LeftMargin = 0;
                sec.PageSetup.BottomMargin = 0;
                sec.PageSetup.RightMargin = 0;
                sec.PageSetup.TopMargin = 0;
            }
            // Save the document in PDF format.
            doc.Save(writeStream, SaveFormat.Pdf);
            downloadBytes = writeStream.ToArray();
        }
    }
    return (downloadBytes);
}

Also, please find attached files for the source file and result file which have two tables.

Problems are:

  1. The first table has 100% width of its parent div, but in PDF it doesn’t look like 100% width.
  2. The second table has 50% width of its parent div, but this is not aligned in a right position. Actually this table has gone out of its parent div container.
  3. Page number which is in div element with float to right is not floating to right hand side.

If I update table layout by using the doc.UpdateTableLayout(); method it actually adjust the table with into the percent of its page size, not its parent div size.

Does anyone have a solution for this?
Thanks.

Hi Alex,

You can use the latest version of Aspose.Words for .NET to convert HTML string to PDF byte array C# code:

using Aspose.Words.Loading;
using Aspose.Words.Saving;

namespace Aspose.Words.Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            License license = new License();
            license.SetLicense("path.lic");

            #region Convert HTML String to PDF Byte Array C#

            string htmlString = System.IO.File.ReadAllText("Text file containing HTML string.txt");
            byte[] htmlByteArray = System.Text.Encoding.UTF8.GetBytes(htmlString);

            // Load HTML content
            HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
            Aspose.Words.Document htmlDocument = null;
            using (System.IO.MemoryStream htmlStream = new System.IO.MemoryStream(htmlByteArray))
                htmlDocument = new Aspose.Words.Document(htmlStream, htmlLoadOptions);

            using (System.IO.MemoryStream pdfStream = new System.IO.MemoryStream())
            {
                PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
                htmlDocument.Save(pdfStream, pdfSaveOptions);

                byte[] pdfByteArray = pdfStream.ToArray();
                // Now send this PDF Byte Array to browser for Download
            }

            #endregion
        }
    }
}