Set Margins when converting HTML to PDF

I am trying to convert html to pdf and want to set the margins . Here is the code I am using
The margins are not set. What is the approach to set the margins?
Thanks

 public byte[] ConvertHtmlToPdf(byte[] htmlBytes)

{

 //  Add timeout capability
  var timeoutMs = 120000; // 2 minute timeout
   var task = Task.Run(() =>
   {
       try
       {
           // Convert HTML bytes to string
           string htmlContent = System.Text.Encoding.UTF8.GetString(htmlBytes);
           string sanitizedHtml = SanitizeHtml(htmlContent);
           byte[] sanitizedBytes = Encoding.UTF8.GetBytes(sanitizedHtml);

           Console.WriteLine($"HTML size: {htmlBytes.Length} bytes");
           Console.WriteLine($"Sanitized HTML size: {sanitizedBytes.Length} bytes");


           // Create HTML load options with appropriate settings
           var htmlOptions = new HtmlLoadOptions
           {
               IsEmbedFonts = false,
               // Note: PageSetup.AnyPage can be used for uniform margins
               PageInfo = new PageInfo
               {
                   Width = 595, // A4 width in points
                   Height = 842, // A4 height in points
                   Margin = new MarginInfo
                   {
                       Top = 72,
                       Left = 72,
                       Right = 72,
                       Bottom = 72
                   }
               }
           };

           using (var htmlStream = new MemoryStream(sanitizedBytes))
           using (var pdfDocument = new Document(htmlStream, htmlOptions))
           using (var outputStream = new MemoryStream())
           {
               // Save options
               var saveOptions = new Aspose.Pdf.PdfSaveOptions();

               pdfDocument.Save(outputStream, saveOptions);
               return outputStream.ToArray();
           }
       }
       catch (Exception ex)
       {
           Console.WriteLine("Error converting HTML to PDF: " + ex.Message);
           if (ex.InnerException != null)
           {
               Console.WriteLine("Inner exception: " + ex.InnerException.Message);
           }
           throw;
       }
   });

   // Wait for the task to complete with timeout
   if (task.Wait(timeoutMs))
   {
       return task.Result;
   }
   else
   {
       throw new TimeoutException("HTML to PDF conversion timed out after " + timeoutMs / 1000 + " seconds");
   }

}

private string SanitizeHtml(string html)
{
if (string.IsNullOrEmpty(html)) return html;

 html = Regex.Replace(html, @"<script\b[^<]*(?:(?!</script>)<[^<]*)*</script>", "", RegexOptions.IgnoreCase);

 // Remove event handlers (onclick, onload, etc.)
 html = Regex.Replace(html, @"\s+on\w+\s*=\s*""[^""]*""", "", RegexOptions.IgnoreCase);

 // Remove javascript: protocol in href attributes
 html = Regex.Replace(html, @"href\s*=\s*[""']javascript:[^""']*[""']", @"href=""#""", RegexOptions.IgnoreCase);
 // Remove data: URIs which could contain JavaScript
 html = Regex.Replace(html, @"data:[^,]*base64,[^""']*", @"#", RegexOptions.IgnoreCase);
 // Remove iframe tags
 html = Regex.Replace(html, @"<iframe\b[^<]*(?:(?!</iframe>)<[^<]*)*</iframe>", "", RegexOptions.IgnoreCase);
 // Remove meta refresh tags
 html = Regex.Replace(html, @"<meta\s+http-equiv\s*=\s*[""']refresh[""'][^>]*>", "", RegexOptions.IgnoreCase);
 return html;

}

@kshah05

To set margins when converting HTML to PDF using Aspose.PDF, you need to configure the HtmlLoadOptions with the appropriate PageInfo settings, including the Margin property. It looks like you are already on the right track with your implementation.

Here’s a refined approach to ensure that the margins are applied correctly:

  1. Set the PageInfo Margins: Make sure you are defining the margins in the PageInfo object within the HtmlLoadOptions. You can specify the top, left, right, and bottom margins as needed.

  2. Check the Output: After setting the margins, ensure that the output PDF reflects these settings. If the margins are still not appearing as expected, you may want to verify the HTML content itself, as certain styles or elements in the HTML can affect how the PDF is rendered.

Here’s an example of how you can set the margins in your existing code:

var htmlOptions = new HtmlLoadOptions
{
    IsEmbedFonts = false,
    PageInfo = new PageInfo
    {
        Width = 595, // A4 width in points
        Height = 842, // A4 height in points
        Margin = new MarginInfo
        {
            Top = 72,    // 1 inch margin
            Left = 72,   // 1 inch margin
            Right = 72,  // 1 inch margin
            Bottom = 72  // 1 inch margin
        }
    }
};

Make sure that the values for the margins are set according to your requirements. The values are in points, where 72 points equal 1 inch.

If you continue to experience issues with the margins not being applied, consider checking the HTML content for any conflicting styles or elements that may be overriding the margin settings.

Feel free to reach out if you have further questions or need additional assistance!

Sources:
[1]: ConvertHTMLToPDFFormat.java

I do not see the option to upload the html file I am using. Where do I upload the sample html file
Thanks

@kshah05

You can add your HTML file to .zip and use upload button the post editor to upload the file. However, if file size is more than 10MB, you can upload it to Google Drive and share link here.

large_file.zip (2.0 MB)

Here is the file

@kshah05

We tested the scenario in our environment with latest version of the API and noticed that the program consumed all available memory and never generated any output. For the sake of further investigation, we have logged an issue as PDFNET-59505 in our issue tracking system. We will further look into its details and let you know once the ticket is resolved.

Since you are noticing another issue related to margins not being set, can you please share your sample generated output for our reference as well so that we can investigate from this perspective as well.

We are sorry for the inconvenience.