Free Support Forum - aspose.com

Html to PDF issue content overwriting

Hi,
I have HTML data in string format but PDF file is note generating properly, So could you please provide any solution. File is being created but the format of data is not correct in file.
Here is my code and sample of PDF file. Please find zip file for HTML sample.
Thanks

        using (MemoryStream docStream = new MemoryStream())
        {
            bool IsLandscape = basedOnTemplateId == 26 ? true : false;
            Aspose.Pdf.License license = new Aspose.Pdf.License();
            //Set license
            license.SetLicense("Aspose.Total.lic");

            //Set the properties for PDF file page format        
            Aspose.Pdf.HtmlLoadOptions objLoadOptions = new Aspose.Pdf.HtmlLoadOptions();
            if (basedOnTemplateId == 26)
            {
                objLoadOptions.PageInfo.Margin.Bottom = 0;
                objLoadOptions.PageInfo.Margin.Top = 20;
                objLoadOptions.PageInfo.Margin.Left = 100;
                objLoadOptions.PageInfo.Margin.Right = 100;
                objLoadOptions.PageInfo.Height = 500;
                objLoadOptions.PageInfo.Width = 500;
                objLoadOptions.PageInfo.IsLandscape = IsLandscape;
            }
            else
            {
                objLoadOptions.PageInfo.Margin.Bottom = 20;
                objLoadOptions.PageInfo.Margin.Top = 20;
                objLoadOptions.PageInfo.Margin.Left = 20;
                objLoadOptions.PageInfo.Margin.Right = 20;
            }               
            Aspose.Pdf.Document doc = new Aspose.Pdf.Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), objLoadOptions);
            doc.Info.Title = !string.IsNullOrEmpty(title) ? title : "Document";
            doc.Save(docStream, Aspose.Pdf.SaveFormat.Pdf);
            doc.Dispose();
            return docStream.ToArray();
        }

Worng fromating.PNG (73.8 KB)

HTML sample1.zip (1.8 KB)

@CS_Developer

We used the following code snippet to test the scenario with Aspose.PDF for .NET 20.11 and did not notice any issue.

var objLoadOptions = new HtmlLoadOptions();
objLoadOptions.PageInfo.Margin.Bottom = 0;
objLoadOptions.PageInfo.Margin.Top = 0;
objLoadOptions.PageInfo.Margin.Left = 0;
objLoadOptions.PageInfo.Margin.Right = 0;
objLoadOptions.PageInfo.IsLandscape = false;

var doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(dataDir + "HTML sample (2).html"))), objLoadOptions);
doc.Save(dataDir + "output.pdf");

files.zip (84.5 KB)

Also, we have modified the HTML file during testing. The modified file is also attached. Would you please make sure to use the latest version of the API and in case you still face any issue, please let us know.

Hello,
I was expecting result as per image attached in attachment named “PDF_ActualResult”. Currently, I am using version 18.8.0.0 for Aspose.Pdf and 18.3.0.0. for Aspose.HTML . HTML sample already added in first message.
Thanks. PDF_ActualResult.PNG (75.9 KB)

@CS_Developer

We saved your shared HTML string as .html file and opened in a browser. It rendered like in the attached image. renderedhtml.png (19.9 KB)

Would you please share the correct HTML file with us which can be rendered similar to the image that you have shared. We will try to convert it into PDF and share our feedback with you.

Here is the Edit HTML file and HTML Text file,
EditHtml.zip (1.7 KB)
EditHtml_Text.zip (1.8 KB)

Could you please convert it into PDF file, because i am facing same issue with above mentioned code.(In first message).
image.png (60.4 KB)

@CS_Developer

Please check attached code snippet which we used with Aspose.PDF for .NET 20.11 in order to convert your HTML to PDF.

var objLoadOptions = new HtmlLoadOptions();
objLoadOptions.PageInfo.Margin.Bottom = 0;
objLoadOptions.PageInfo.Margin.Top = 0;
objLoadOptions.PageInfo.Margin.Left = 0;
objLoadOptions.PageInfo.Margin.Right = 0;
objLoadOptions.PageInfo.IsLandscape = false;

var doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(dataDir + "EditHtml.html"))), objLoadOptions);
doc.Save(dataDir + "output.pdf");

output.pdf (52.7 KB)

Would you please share what value is assigned to basedOnTemplateId variable at your side? We will try to replicate the issue using same code snippet and share our feedback with you.

Thanks for your valuable replies,
Here is my code sample, which is provided by you but i am facing error like “Illegal characters in path.”. Could you please provide solution for this.

image.png (46.5 KB)

"Would you please share what value is assigned to basedOnTemplateId variable at your side? We will try to replicate the issue using same code snippet and share our feedback with you*
*basedOnTemplateId this is just for handle the condition but for the current scenario we always use ‘else’ part.

If you want to replicate the issue then please use this code.I have modify my old code.
public static byte[] ConvertHtmlToPDF(string html, string title = null)
{
using (MemoryStream docStream = new MemoryStream())
{
////Set the properties for PDF file page format
Aspose.Pdf.HtmlLoadOptions objLoadOptions = new Aspose.Pdf.HtmlLoadOptions();

            objLoadOptions.PageInfo.Margin.Bottom = 0;
            objLoadOptions.PageInfo.Margin.Top = 0;
            objLoadOptions.PageInfo.Margin.Left = 0;
            objLoadOptions.PageInfo.Margin.Right = 0;
            objLoadOptions.PageInfo.IsLandscape = false;   
          
            Aspose.Pdf.Document doc = new Aspose.Pdf.Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), objLoadOptions);
            doc.Info.Title = "Document";
            doc.Save(docStream, Aspose.Pdf.SaveFormat.Pdf);
            doc.Dispose();
            return docStream.ToArray();
        }

}

@CS_Developer

We tested using the exact code snippet that you have shared and did not notice any issue. We tested it using Aspose.PDF for .NET 20.11. Code.png (17.5 KB)

Would you please try to create a sample console application and if it is able to replicate the issue using the latest version, please share it with us in .zip format. You can remove *packages and *.dlls from the application to reduce its size and we will restore them at our end.

Yes, I tried with your code sample by creating console project. ‘HTML string to PDF’ conversion working fine with latest version of Aspose.PDF (20.11). So we need to update our Aspose.PDF version “18.8.0” to “20.11”.
Thanks

@CS_Developer

Thanks for your feedback.

Please upgrade to the latest version (as always recommended) and feel free to tell us if you need further assistance.