H.Georges
H.Georges

Reputation: 11

Convert HTML to PDF using iText with special char fail

I'm facing a problem by using iText and XMLWorkerHelper for a specific case. I generate multiple PDF files with multiple pages without problem but sometimes, an error occur with special characters.

I tested my template and it's not a problem with my HTML, even if the exception say :

Exception thrown: 'iTextSharp.tool.xml.exceptions.RuntimeWorkerException' in itextsharp.xmlworker.dll

Additional information: Invalid nested tag tr found, expected closing tag td.

This error is due to the character : & which is added into my template.

<td>Launch C&O</td>

I don't exactly know how to resolve this error, is it an encoding error ? Should I specify an encoding mode when I create the PDF ?

This is the code which create a PDF :

public async Task Generate(Stream stream, List<string> contentPages)
        {
                try
                {
                    int cpt = 1;
                    Document document = new Document();
                    PdfWriter writer = PdfWriter.GetInstance(document, stream);
                    writer.CloseStream = false;
                    document.Open();

                    foreach (string pdfContentPage in contentPages)
                    {
                        try
                        {
                            document.NewPage();
                            using (StringReader srHtml = new StringReader(pdfContentPage ))
                            {
                                XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, srHtml);
                            }
                            ++cpt;
                        }
                        catch (RuntimeWorkerException ex)
                        {
                            Console.Write($"An error occured at PDF generation for cpt = {cpt}");
                            Console.Write(ex.Message);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine($"Content Error : pdfContentPage}");
                            throw;
                        }
                    }

                    document.Close();
                }
                catch (Exception)
                {
                    throw;
                }
}

If you have an advice, I'm glad to read it ! :)

Upvotes: 1

Views: 2391

Answers (2)

Evin Sebastian
Evin Sebastian

Reputation: 21

At the query binding field where & symbol comes i used a replace function for all describtion's For example at the bind grid s(2)=" TEST & TEST"

for loop
dim desc as string

desc="TEST & TEST"

desc=desc.replace("&"," ")

s(2)= desc

end of loop

Thus the issue was solved in my case

Upvotes: 2

Mohan Kumar Dg
Mohan Kumar Dg

Reputation: 11

Try with below logic

InputStream is = new ByteArrayInputStream(srHtml.getBytes(Charset.forName("UTF-8")));
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, is, Charset.forName("UTF-8"));

With, xmlworker 5.5.12 and itextpdf 5.5.12 version

Upvotes: 0

Related Questions