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

Free Support Forum - aspose.com

HTML to PDF- and add header/footer to PDF problems

Hi,

I am trying to get high-fidelity HTML to PDF rendering.

Meaning - HTML should look as close to IE as possible.

And also to add header / footer to several existing PDF files.

I am facing 2 problem:1. following this guide http://www.aspose.com/documentation/.net-components/aspose.pdf-for-.net/how-to-convert-html-to-pdf-using-inlinehtml-approach.html

using this code:

Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Section s = pdf.Sections.Add();
Aspose.Pdf.Generator.Text txtHtml = new Aspose.Pdf.Generator.Text(s, html);txtHtml.IsHtmlTagSupported = true;
s.Paragraphs.Add(txtHtml);pdf.HtmlInfo.ImgUrl = this.resourcesFolder + "\\";
pdf.Save("c:\\temp\\pdfgenerator6.pdf");

I get an empty PDF file (0 bytes) and it is locked by the process. I have to do IISRESET to be able to delete it.Is there a need to flush or close the stream? the HTML code exists and it is valid as I can open it in browsers.

2. Adding a header/footer - following this guide: http://www.aspose.com/documentation/.net-components/aspose.pdf-for-.net/add-header-in-a-pdf-file-facades.html

I am able to add the header and footer, but opening the PDF in acrobat reader shows an error, that the document is not formatted correctly and is corrupt. see the error attached.

Can you pelase provide help on these 2 issues?

thanks.

Hi Shai,

ShaiPetel:

I am trying to get high-fidelity HTML to PDF rendering.

Meaning - HTML should look as close to IE as possible.

And also to add header / footer to several existing PDF files.

I am facing 2 problem:1. following this guide http://www.aspose.com/documentation/.net-components/aspose.pdf-for-.net/how-to-convert-html-to-pdf-using-inlinehtml-approach.html

using this code:

Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Section s = pdf.Sections.Add();
Aspose.Pdf.Generator.Text txtHtml = new Aspose.Pdf.Generator.Text(s, html);txtHtml.IsHtmlTagSupported = true;
s.Paragraphs.Add(txtHtml);pdf.HtmlInfo.ImgUrl = this.resourcesFolder + "\\";
pdf.Save("c:\\temp\\pdfgenerator6.pdf");

I get an empty PDF file (0 bytes) and it is locked by the process. I have to do IISRESET to be able to delete it.Is there a need to flush or close the stream? the HTML code exists and it is valid as I can open it in browsers.

Please provide us with the HTML file which you are using to generate the PDF, this will help us figure out the issue soon.

ShaiPetel:

2. Adding a header/footer - following this guide: http://www.aspose.com/documentation/.net-components/aspose.pdf-for-.net/add-header-in-a-pdf-file-facades.html

I am able to add the header and footer, but opening the PDF in acrobat reader shows an error, that the document is not formatted correctly and is corrupt. see the error attached.

Please share the complete code with your template file to show the issue. We will check it and get back to you soon.

Thank You & Best Regards,

Hi,

I posted the code to generate pdf above, here is the string value of the html variable (attached as a txt file).

This html is produced by xsl transform of an info path form. in the zip you will find the html and its resource folder.Also posted in the zip - the pdf file i am trying to add the header to.

Running this code produces the error you see above:

Aspose.Pdf.Facades.PdfFileStamp fileStamp = new Aspose.Pdf.Facades.PdfFileStamp(sourceFiles[f], sourceFiles[f]);
Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText("Header text...");
fileStamp.AddHeader(formattedText, 2);
formattedText = new Aspose.Pdf.Facades.FormattedText("Footer text...");
fileStamp.AddFooter(formattedText, 2);
fileStamp.Close();

Hi,

Thanks for sharing the resource files.

I have tried converting the attached HTML file into PDF format and as per my observations, the PDF is being generated but in resultant PDF the borders of table are bolder than the source HTML. For the sake of correction, I have logged this problem as PDFNEWNET-31536 in our issue tracking system.

Now concerning to the error when adding Header/Footer, as per my observations, when I have used Aspose.Pdf for .NET 6.3.0, the contents of second page are removed. I have separately logged this problem as PDFNEWNET-31537 in our issue tracking system.

We will further look into the details of these problem and will keep you updated on the status of correction. Please be patient and spare us little time. We apologize for your inconvenience.

Thanks for the reply.

We can definately wait for fixes, but:

for some reason, i can't get the html to print into PDF file using hte code above, from ASP.NET procees.

instead, I am getting a 0 bytes pdf file, that is used by the asp.net process and locked.

cant view, or delete it untill i kill my process.

can you please verify you are able to do that in ASP.NET? using my code?

thanks!

Hi Shai,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I just tested the HTML2PDF process in an ASP.NET application using your template file and the PDF file is generated with around 24 kb size(as shared by Nayyer in the previous post). Please create a sample ASP.NET application and post it here to show the issue. This will help us figure out the issue soon.

Thank You & Best Regards,

are you using hte same code as i am in converting to PDF?

notice, i am passing a string as html, not file stream or bytes. does this make a difference?

I verified my string html is exactly the same as the file, i just buid it, save it as the file you got, and try to convert it to HTML ( so it is actually the source of the file, so it must be the same).

Hi Shai,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Please share the exact string you are using which is causing the issue at your end to replicate the issue. We will check it and get back to you.

Thank You & Best Regards,

I have shared the exact string!

Like I said, the html file I attached is that exact string.

When I hit this problem, I just saved that string into a file and sent it to you, so it is 100% accurate.

I even tried saving it to file, reading it back from file - got the same result.

Is it possible for you to send me the exact code example you are using when you got it working? I will try to run that same code on my dev machine and let you know.

Thanks, Shai.

Hi Shai,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Below is the exact code which I have used with your provided Html file and the generated file is also attached:

// Instantiate an object PDF class

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(@"E:\AP DATA\Shared\Shared.htm");

//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);

//Save the pdf document

pdf.Save(@"E:\AP DATA\Shared\HTML2pdf.pdf");

Please try the above code at your end and let us know if you still face any issue.

Thank You & Best Regards,

Yeah, this code works on the HTML attached, but dies not even begin to resemble my original HTML...

First off, you are missing a dispose call on StreamReader, I added it in my example.

Now, rendering wise:

  1. header not aligned to center
  2. Table borders are visible, when my table has no borders in HTML
  3. Labels are covering the text of the textboxes
  4. Image has very thick border to it, in HTML there is no border

But a bigger problem presented itself, when I am trying to run this code on other pages, I get an exception on the pdf.save command saying "object reference not set to an instance of an object".

That happens only on some specific pages. Any thoughts on why that happens?

Should I send all the different types of html pages we plan to work with? there are thousands...

Waiting for instructions.

One more thing, any news on this issue: PDFNEWNET-31537

Only the first page remain in PDF after adding header/footer?

Thanks.

Hi Shai,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Yes, the issues regarding formatting are already registered in our issue tracking system as issue id: PDFNEWNET-31536. For both your issues i.e. PDFNEWNET-31536 and PDFNEWNET-31537, I have asked our development team to share the ETA. You will get updated via this forum post once I get a response from development team.

Regarding “Object Reference …” issue, please share some sample html (not all), we will investigate the issue and get back to you.

Sorry for the inconvenience caused,

Thanks, your response time is amazing so far.

I shared the HTML in the post about object not set to referecen... error.

https://forum.aspose.com/t/103620v

please see if you can make it work. this is all html i create from InfoPath by using xml/xsl conversion.

Thanks.

Hi Shai,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for sharing the template file.

We have found your mentioned issue after an initial test. Your issue has been registered in our issue tracking system with issue id: PDFNEWNET-31693. I will update you regarding any progress related to your issues as soon as I get the feedback from the development team.

Sorry for the inconvenience,

I see PDFNEWNET-31537 is marked as resolved.

Where / when can I get the fix to test? We are planned to release this weekend.

Thanks.

Hi Shai,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Your reported issues are resolved and will be part of our next official release of Aspose.Pdf for .NET v6.5. However, I tested your issue: PDFNEWNET-31537 with our latest hot fix version of Aspose.Pdf for .NET v6.4.2 and it is working fine. Please download and try it and let us know if it resolves the issue at your end too.

Thank You & Best Regards,

Thanks, that is good news.

When is version 6.5 expected? Will I get an alert on it?

Also - can you please confirm that 6.5 API will be backwards compatible? I mean no namespace / classes names changed?

It is very hard when you not support backward versions - since I cannot deploy using binding redirect.

We currently have 2 customers asking for refund to our product because of these issues - this is why it is critical for me to get a quick fix.

Thanks!

I have hit another wall.

This html (attached) throws a strage error, resulting again in empty pdf file (0 bytes) which cannot be deleted before iisreset.

See the html and the error attached.

Can you please confirm this will be resolved as well in the upcoming version?

Error: {"Stack empty."} comfing from Pdf.Save() method.

Just noticed:

The PDF ignores the nowrap statement:

nowrap="nowrap"

I tried replacing it with stype="white-space:nowrap" but didnt help.

Is there a fix of this as well?