Html to pdf set margn issue

I am trying to set margin to my html to pdf document. but its not adding right margin. content also not showing fully.

        async Task<byte[]> ITransformHelper.ConvertHtmlToPdf(string htmlDocument, Manifest manifest, string header, string footer, string requestId)


<a class="attachment" href="/uploads/default/86449">image.png</a> (65.0 KB)


        { 
            string htmlFilePath = "";
            htmlFilePath = manifest.Output.OutputFileName?.Replace(".pdf", ".html"); 
            string modifiedHtml = htmlDocument.Replace("<body>", $"<body style='margin: 10px 20px 10px 30px;'>");
            File.WriteAllText(htmlFilePath, modifiedHtml); 

            var api = new HtmlApi("8bb39521-349d-415a-8712-8c6c8adb0b16", "31dab4a55d3ab3e2e1b89bdd8fd2d4f0").ConvertApi;
            var result = await api.ConvertAsync(htmlFilePath, manifest.Output.OutputFileName);

            // Load existing PDF document
            var pdfDocument = new Aspose.Pdf.Document(manifest.Output.OutputFileName);

            // Initialize HTMLLoadSave Options
            HtmlLoadOptions options = new HtmlLoadOptions();

            // Load HTML document using Aspose.Pdf.Document
            var doc = new Aspose.Pdf.Document(htmlFilePath, options);

            // Save the Aspose.Pdf.Document to PDF
            doc.Save(pdfDocument + "RenderContentToSamePage.pdf");

            pdfDocument.Save(manifest.Output.OutputFileName);
            byte[] fileBytes = File.ReadAllBytes(manifest.Output.OutputFileName);

            return fileBytes; 

        }

0000000000.png (73.8 KB)

@niku2023

Looks like you are modifying the HTML string using Aspose.HTML and then generating Aspose.PDF to create PDF document out of it. You can achieve this entire functionality using Aspose.HTML.

For converting HTML to PDF and setting the PDF margins, you can use below code snippet:

Aspose.Html.Saving.PdfSaveOptions options = new Html.Saving.PdfSaveOptions()
{
    JpegQuality = 100,
};
options.PageSetup.AnyPage.Margin = new Html.Drawing.Margin(30);
options.PageSetup.AdjustToWidestPage = true;
options.BackgroundColor = System.Drawing.Color.White;
using (var document = new Aspose.Html.HTMLDocument(file))
{
    Aspose.Html.Converters.Converter.ConvertHTML(document, options, dataDir + "HTMLToPDFTest.pdf");
}
1 Like

Thanks for the reply. Can you post full code with reference. becoause i am getting errors

@niku2023

The code snippet had minor issue (extra ; in it) that we have removed. Furthermore, please make sure that you are using the latest version of the API. In case you still face any issues, please share your complete environment details i.e. OS Name and Version, .NET Framework, Application Type, etc.