HTML-to-PDF conversion produces an extra empty page

When creating the page:

<div class="page" style="width: 697px; height: 1026px; overflow: hidden; padding: 48px; font-family: tahoma, sans-serif; font-size: 10pt;"><p style="text-align: justify;"><font face="tahoma, sans-serif" style="" size="2"><b>​​​BIJLAGE 1. VASTSTELLING - Titel 7bis van de Politiecodex van de Stad Antwerpen</b></font></p><p style="text-align: justify;"><font face="tahoma, sans-serif" style="" size="2">Op <b>12-02-2019</b> om <b>13:34</b> stelde <b>Gert Jakobs&nbsp;Abraham van Helsing</b><b>&nbsp;ANPR</b> vast dat het voertuig&nbsp;Opel voorzien van de nummerplaat <b>95HTSV</b> zich op de openbare weg bevond ter hoogte van&nbsp;<b>Hoofdstraat 12a,&nbsp;7500&nbsp;thv. nummer x&nbsp;None&nbsp;</b>(2060 Antwerpen), en dit in overtreding met het volgende artikel van de Politiecodex van de stad Antwerpen:</font></p><p style="text-align: justify;"><font face="tahoma, sans-serif" style="" size="2"><b>- dat luidt als volgt:&nbsp;</b></font></p><p style="text-align: justify;"><font face="tahoma, sans-serif" style="" size="2">Parkeren bij een parkeerautomaat met kaarten terwijl parkeertijd is verstreken.</font></p><p style="text-align: justify;"><font face="tahoma, sans-serif" style="" size="2"><b>Houder van de nummerplaat:</b></font></p><p style="text-align: justify;"><font face="tahoma, sans-serif" style="" size="2">Armin van Buuren<br>Pantheon 14a0203<br>7651 NZ Enschede&nbsp;</font></p><p style="text-align: justify;"><font face="tahoma, sans-serif" style="" size="2"><b>Inlichtingen en bijkomende vaststellingen: </b>Dit is een notitie.</font></p><p style="text-align: justify;"><font face="tahoma, sans-serif" style="" size="2">Opgemaakt te Antwerpen, op 12-02-2019 <br>Politieagent Gert Jakobs&nbsp;Abraham van Helsing Niet van toepassing</font></p></div>

It creates one extra white page when using your code

I can recommend to use

            var options = new HtmlLoadOptions();
            options.PageInfo.Margin = new MarginInfo();
            options.IsRenderToSinglePage = true;

as a quick solution.

1 Like

when using options.IsRenderToSinglePage = true; it combines the empty page with the page earlier. I have tried to adjust the page size etc. didnt work

@alizera220
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-56863

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.

1 Like

Thankyou! appreciate your help!

@alizera220
To clarify, should all of your snippets create one page, or can some of them create two or more?

Every element of the List<string> HtmlDocument parameter represents 1 page:

 public int HtmlToPdf(List<string> htmlDocument, string filePath)
 {
     // Starting timer
     BenchmarkLogic bm = new BenchmarkLogic();
     bm.StartStopwatch();

     var options = new HtmlLoadOptions();
     options.PageInfo.Margin = new MarginInfo();
     options.IsRenderToSinglePage = true;

     var finalDocument = new Document();

     finalDocument.OptimizeSize = true;

     foreach (var htmlFragment in htmlDocument)
     {
         using var stringStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlFragment));
         using var documentStream = new MemoryStream();
         using var document = new Document(stringStream, options);
         document.Save(documentStream);
         finalDocument.Pages.Add(document.Pages);
     }

     // Save updated document
     finalDocument.Convert(new MemoryStream(), PdfFormat.PDF_A_1B, ConvertErrorAction.None);
     finalDocument.Save(filePath);
     // Stopping timer
     var result = bm.StopStopwatch();

     return result;
 }

Thank you! We will try to find a workaround while the Dev Team fixes a bug. I will inform you on Monday.

Sounds good, thankyou

@alizera220 Could you please check the following workaround?

        static long HtmlToPdf(List<string> htmlFragments, string filePath)
        {
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            var options = new HtmlLoadOptions();
            options.PageInfo.Margin = new MarginInfo();

            var finalDocument = new Document();

            foreach (var htmlFragment in htmlFragments)
            {
                using MemoryStream stringStream = GetHtmlContentAsStream(htmlFragment);
                using var documentStream = new MemoryStream();
                using var document = new Document(stringStream, options);
                document.Save(documentStream);
                finalDocument.Pages.Add(document.Pages[1]);
                if (document.Pages.Count > 1)
                {
                    Console.WriteLine("Wrong rendering");
                    //throw new Exception()
                }
            }

            // Save updated document
            finalDocument.Convert(new MemoryStream(), PdfFormat.PDF_A_1B, ConvertErrorAction.None);
            finalDocument.Save(filePath);
            stopwatch.Stop();
            return stopwatch.ElapsedMilliseconds;
        }

        private static MemoryStream GetHtmlContentAsStream(string htmlFragment)
        {
            var sb = new StringBuilder();
            sb.Append("<body style=\"position:fixed;top:0;left:0\">");
            sb.Append(htmlFragment);
            sb.Append("</body>");
            return new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()));
        }

Goodmorning.

Thankyou, it works fine now. Will this be fixed in the future?

Hi @alizera220 ! I will keep you updated on the task.