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

Free Support Forum - aspose.com

While converting HTML to PDF the text is gets out of margin and also header gets blue

Hello,

I am trying to convert HTML to PDF using Aspose PDF using code below.

Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
// add the section to PDF document sections collection
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();

// Read the contents of HTML file into StreamReader object
StreamReader r = File.OpenText(@“D:/pdftest/HTML2pdf.html”);
//Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, r.ReadToEnd());
// enable the property to display HTML contents within their own formatting
text2.IsHtmlTagSupported = true;
//Add the text paragraphs containing HTML text to the section
section.Paragraphs.Add(text2);

Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
// add the section to PDF document sections collection
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();

// Read the contents of HTML file into StreamReader object
StreamReader r = File.OpenText(@“D:/pdftest/HTML2pdf.html”);
//Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, r.ReadToEnd());
// enable the property to display HTML contents within their own formatting
text2.IsHtmlTagSupported = true;
//Add the text paragraphs containing HTML text to the section
section.Paragraphs.Add(text2);

The problem is when I open the PDF . The margin of the generated PDF is not honoured even when I have specified margins for PDF as well as for sections. The text gets stretched and it crosses the right boundary. Even the Headers font change and color changes from Black to Blue… If this is a known issue , then kindly let me know if there are work arounds available for this, May be by using Aspose.Words.

If there is a solution please tell me how to add customized header and footer to an Aspose.Word document and how to import a multi page HTML as a section so that header and footer remain for all the pages.

Regards,

Kushagra

Hi Kushagra,


We are sorry for the inconvenience caused. Can you please share your sample HTML file here? So we will test the scenario at our end and will provide you more information.

Moreover, I’m moving your request to Aspose.Total forum. There one of my colleague support developer will guide you regarding Aspose.Words queries.

Best Regards,

I would be supplying the HTML file shortly

Hi Kushagra,

Thanks for your inquiry. You can convert html to Pdf by using Aspose.Words and also create header/footer according to your requirements. Please read following documentation links for your kind reference.

Moreover, each Header and Footer in a document is stored per section. Each header or footer is imported into Aspose.Words as a HeaderFooter node. This node is always a child of a Section. Most documents have header or footer content represented by the primary header or footer. This displays content on all pages of the section.

It would be great if you please share your html along with expected output document. Please create your expected document e.g. MS Word document using Microsoft Word and attach it here for our reference. We will then provide you more information on this along with code.

Hi Support,


Attached is the HTML and the resulting pdf with the file . I have tried the solution with 8.5.0 binaries also with the following code but the result remains the same. Please get it solved as a lot of customers need this solution and once this issue is solved only then we can upgrade to the latest version.

HtmlLoadOptions options = new HtmlLoadOptions();<o:p></o:p>

// use the new conversion engine<o:p></o:p>

options.UseNewConversionEngine = true;<o:p></o:p>

// load HTML file<o:p></o:p>

Document pdfDocument = new Document(@“C:\pdftest\AsposeSample.html”, options);<o:p></o:p>

// save output as PDF format<o:p></o:p>

pdfDocument.Save(@“C:\pdftest\AsposeSample.pdf”);


And even the older approach also doesnt work which is :

Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
// add the section to PDF document sections collection
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();

// Read the contents of HTML file into StreamReader object
StreamReader r = File.OpenText(@“D:/pdftest/HTML2pdf.html”);
//Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, r.ReadToEnd());
// enable the property to display HTML contents within their own formatting
text2.IsHtmlTagSupported = true;
//Add the text paragraphs containing HTML text to the section
section.Paragraphs.Add(text2);

Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
// add the section to PDF document sections collection
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();

// Read the contents of HTML file into StreamReader object
StreamReader r = File.OpenText(@“D:/pdftest/HTML2pdf.html”);
//Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, r.ReadToEnd());
// enable the property to display HTML contents within their own formatting
text2.IsHtmlTagSupported = true;
//Add the text paragraphs containing HTML text to the section
section.Paragraphs.Add(text2);


Please provide a QuickFix, Even a patch would be OK for us if you can fix this in QuickTime.

Regards,
Kushagra

Hi Kusharga,

Sorry for the inconvenience faced. While testing the scenario with Aspose.Pdf for .NET 8.5.0, I've managed to reproduce this issue on my side and logged it in our bug tracking system as PDFNEWNET-35897 for further investigation and resolution. I've also linked your request to this issue and you will be notified via this thread as soon as it is resolved.


Moreover thanks for your concern. Our development team is working hard to fix HTML to PDF conversion issue and introducing HTML to PDF conversion feature in new DOM is one step forward. Hopefully we will fix related issues very soon.

Please feel free to contact us for any further assistance.

Best Regards,

Hi Kusharga,


In addition to my above reply. As a workaround you may consider Aspose.Words. Please check sample output. Hopefully it will serve the purpose. However, we will keep you updated about above logged issue progress.

Aspose.Words.Document docw = new
Aspose.Words.Document(myDir + “AsposeSample.html”);<o:p></o:p>

docw.Save(myDir + "AspsoeSample.pdf", Aspose.Words.SaveFormat.Pdf);

Best Regards,

Hi,


The problem is that I want to insert an HTML in a section of PDF. Is that possible using Aspose.Words ?
i.e. if I have two htmls and I want to append them using Aspose.Word into 1 docx document and in such a way that the next HTML starts from the next section where the 1st ends. Can you suggest an approach to do it?

Regards,
Kushagra

Hi Kushagra,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word do. You can not insert an HTML fragment or whole HTML document into PDF document by using Aspose.Words.

However, you can use DocumentBuilder.InsertHtml to insert an HTML fragment or whole HTML document into MS Word document at any location in the document. DocumentBuilder has an internal cursor that you can navigate to a different location in a document using various DocumentBuilder.MoveToXXX methods such as DocumentBuilder.MoveToDocumentStart and DocumentBuilder.MoveToField . Please read the following documentation link for your kind reference.

Moreover, you can insert a whole html document into MS Word document at any position. Please read more detail from here:

Aspose Documentation

[image]

Aspose.Words for .NET

Aspose.Words for .NET is a class library that enables your applications to perform a great range of document processing tasks – generate, modify, convert, render, and print documents.

Once you have inserted the html/html document into Word document, you can easily convert it into Pdf file format. Please check following documentation link.
http://www.aspose.com/docs/display/wordsnet/How+to++Convert+a+Document+to+PDF

Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToDocumentEnd();

builder.InsertHtml("Paragraph right" +"**Implicit paragraph left**" +"Div center" +"# Heading 1 left.");

doc.Save(MyDir + "Out.pdf");

The issues you have found earlier (filed as PDFNEWNET-35897) have been fixed in Aspose.Pdf for .NET 9.1.0.

For further details, you may check this blog post.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

HI Kushagra,

We are pleased to inform you that your reported issue PDFNEWNET-35897 has been fixed. You may use new DOM approach for HTML to PDF conversion. Please try following code snippet, it will fix the issue.

HtmlLoadOptions options = new HtmlLoadOptions();<o:p></o:p>

// use the new conversion engine<o:p></o:p>

options.UseNewConversionEngine = true;<o:p></o:p>

// load HTML file<o:p></o:p>

Document pdfDocument = new Document(“Input.html”, options);

// save output as PDF format<o:p></o:p>

pdfDocument.Save(myDir + “Output.pdf”);

<o:p>
</o:p>

<o:p>Please feel free to contact us for any further assistance.</o:p>


Best Regards,