We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

HTML to PDF conversion generates extra blank page

When we convert following html file(zipped) into pdf then it generates extra blank page in the output (zipped).
HtmlIssueSpace.zip (27.9 KB)

This is the code used for conversion.

public FileIO HTMLToPdfUsingAsposeStream(string htmlContent, string fileName, string userGuid)
        {
            FileIO file = new FileIO
            {
                UniqueName = fileName,
                Name = fileName
            };
            try {
                var options = new AsposeHTMLDll.Saving.PdfSaveOptions();
                using (var streamProvider = new MemoryStreamProvider())
                {
                    Converter.ConvertHTML(htmlContent, ".", options, streamProvider);
                    // Get access to the memory stream that contains the result data
                    var memory = streamProvider.Streams.First();
                    memory.Seek(0, SeekOrigin.Begin);
                    file.FileByteArray = memory.ToArray();
                }
                IAzureBlobRepository azureBlobRepositoryObj = ObjectFactory.Container.GetInstance<IAzureBlobRepository>();
                azureBlobRepositoryObj.AddToBlobStorageFolder(userGuid, file);
            }
            catch(Exception ex)
            {
            }
            return file;
        }
		
class MemoryStreamProvider : AsposeHTMLDll.IO.ICreateStreamProvider
    {
        // List of MemoryStream objects created during the document rendering
        public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
         
        public Stream GetStream(string name, string extension)
        {
            // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats.
            MemoryStream result = new MemoryStream();
            Streams.Add(result);
            return result;
        }

        public Stream GetStream(string name, string extension, int page)
        {
            // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
            MemoryStream result = new MemoryStream();
            Streams.Add(result);
            return result;
        }

        public void ReleaseStream(Stream stream)
        {
            //  Here you can release the stream filled with data and, for instance, flush it to the hard-drive
        }

        public void Dispose()
        {
            // Releasing resources
            foreach (var stream in Streams)
                stream.Dispose();
        }
    }

@sumitworksimpli

You are using old version of Aspose.HTML. Please upgrade to the latest version of Aspose.HTML for .NET 22.6 and let us know how it goes on your side. Hope this helps you.

Hi @tahir.manzoor,

I have upgraded Aspose.HTML to latest version but issue is still there. I have created a console app and here is the html file HtmlIssueSpace.zip (27.9 KB)
for your reference.

using System;
using System.IO;
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Saving;

namespace ExtraSpaceInHtmlToPdf
{
class Program
{
    static void Main()
    {
        try
        {
            string DataDir = @"C:\AsposeTesting";

            // Prepare a path to a source HTML file
            string documentPath = Path.Combine(DataDir, "spring.html");

            // Prepare a path for converted file saving 
            string savePath = Path.Combine(DataDir, "spring-output.pdf");

            // Initialize an HTML document from the file
            using var document = new HTMLDocument(documentPath);

            // Initialize PdfSaveOptions 
            var options = new PdfSaveOptions();

            // Convert HTML to PDF
            Converter.ConvertHTML(document, options, savePath);
        }
        catch(Exception ex)
        {
            Console.WriteLine("Issue in extra space with Aspose!! "+ ex.Message);
        }
    }
}
}

@sumitworksimpli

We have logged this problem in our issue tracking system as HTMLNET-3922. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@sumitworksimpli

Please use the following code example to avoid this issue.

string file = MyDir + "HtmlIssueSpace.html";
MemoryStream ms = new MemoryStream(File.ReadAllBytes(file));
PdfSaveOptions options = new PdfSaveOptions()
{
    JpegQuality = 100,
};
options.PageSetup.PageLayoutOptions = PageLayoutOptions.FitToContentHeight;
using (var document = new Aspose.Html.HTMLDocument(ms, MyDir))
{
    Aspose.Html.Converters.Converter.ConvertHTML(document, options, MyDir + "ouptut.pdf");
}

Hi @tahir.manzoor,

This work around solves the problem but the issue is that, it locates all the content in the single page only. Can you please suggest other work around so that the pdf don’t lose it’s pages.

Regards,
Sumit Awasthi

@sumitworksimpli

It is to inform you that we have closed the issue HTMLNET-3922 as ‘Not a bug’. You can use properties of PageLayoutOptions class as suggested below to achieve your requirements.