iTextSharp的上试图解析HTML的PDF转换错误 [英] ItextSharp Error on trying to parse html for pdf conversion

查看:763
本文介绍了iTextSharp的上试图解析HTML的PDF转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的iTextSharp的模块下面列出的HTML转换到一个PDF页面。

I was using the ItextSharp module to convert the below listed html in to a pdf page.

        <div style="font-size: 18pt; font-weight: bold;">
    mma<br>mmar</div><br> <br>
    <div style="font-size: 14pt;">Click to View Pricing
    </div>
    <br>
    <div>
    <table>
    <tr><td> <a href="http://www.mma.com/fci" style="color: Blue; font-size: 10pt; text-decoration: underline;"> FCI</a>:</td> 
<td><a href="http://www.mma.com/access/?pn=78211-014" style="color: Blue; font-size: 10pt; text-decoration: underline;"> 78211-014</a></td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-009" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-009</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-006" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-006</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-007" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-007</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-003" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-003</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-005" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-005</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-008"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-008</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-004" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-004</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-012" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-012</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-007LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-007LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-015LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-015LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-003LF"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-003LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-009LF" 
style="color: Blue; font-size: 10pt; text-decoration:
underline;">78211-009LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-005LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-005LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-010LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-010LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-006LF"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-006LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-014LF"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-014LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-004LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-004LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-012LF"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-012LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-008LF"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-008LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-011LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-011LF</td></tr><tr><td></td> <td><a href="http://www.mma.com/access/?pn=78211-013LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-013LF</td></tr><tr><td></td> <td><a href="http://www.mma.com/access/?pn=78211-010" style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-010</td></tr><tr><td></td>
<td><a href="http://www.mma.com/access/?pn=78211-015"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-015</td></tr><tr><td> 
<a href="http://www.mma.com/souriau" 
style="color: Blue; font-size: 10pt; text-decoration: underline;"> Souriau</a>:</td>
 <td><a href="http://www.mma.com/access/?pn=24JR124-3" style="color: Blue; font-size: 10pt; text-decoration: underline;"> 24JR124-3</a></td></tr></table></div>

C#code生成HTML上面:

C# code to generate the html above :

 var html = new StringBuilder(@"<div style=""font-size: 18pt; font-weight: bold;"">Authorized Distributor</div><br> <br><div style=""font-size: 14pt;"">Click to View Pricing, Inventory, Delivery & Lifecycle Information:</div><br>");
            List<MfrBrandView> mfrBrands = MfrBrandView.Load(fileId);
            var uniquesuppliers = mfrBrands.GroupBy(t => new {t.Manufacturer,t.SupplierVirtualDirectory}).Select(g => g.First());
            html.Append("<div><table>");
            foreach (var uniquesupplier in uniquesuppliers)
            {
                var mfrPartNums =
                    mfrBrands.Where(t => t.ManufacturerId == uniquesupplier.ManufacturerId).Select(
                        t => t.ManufacturerPartNumber).ToList();
                html.AppendFormat(@"<tr><td> <a href=""http://www.mma.com/{0}"" style=""color: Blue; font-size: 10pt; text-decoration: underline;""> {1}</a>:</td> <td><a href=""http://www.mma.com/access/?pn={2}"" style=""color: Blue; font-size: 10pt; text-decoration: underline;""> {2}</a></td></tr>", uniquesupplier.SupplierVirtualDirectory, uniquesupplier.MouserizationLabel, mfrPartNums[0]);

                for (int i = 1; i < mfrPartNums.Count(); i++)
                {
                    html.AppendFormat(
                        @"<tr><td></td> <td><a href=""http://www.mma.com/access/?pn={0}"" style=""color: Blue; font-size: 10pt; text-decoration: underline;"">{0}</td></tr>",
                        mfrPartNums[i]);
                }
            }
            html.Append("</table></div>");
            return html.ToString();

形成HTML之后code生成页面:

Code to generate the page after html is formed:

PdfWriter writer = null;
            PdfDocument doc = null;
            //PdfReader reader = new PdfReader(content);
           // reader.Info["Title"]

            try
            {
                doc = new PdfDocument();
                doc.SetPageSize(PageSize.LETTER);
                writer = PdfWriter.GetInstance(doc, content);
                writer.CloseStream = false;
                doc.Open();
                doc.NewPage();

                foreach (IElement element in iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(html), null))
                {
                    doc.Add(element);
                }
            }

下面是我得到一个异常说 无法转换类型 iTextSharp.text.html.simpleparser.CellWrapper'的对象类型'iTextSharp.text.Paragraph
iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(新StringReader(HTML),NULL)

below is where i get an exception saying "Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'." iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(html), null)

是不是有什么错误的HTML我试图解析?是否有这样做的更好的办法?
感谢很多提前对你的帮助。这是非常AP preciated。

Is there something wrong with the html I'm trying to parse? Is there a better way of doing this? Thanks much in advance for your help. It's very much appreciated.

推荐答案

`HTMLWorker'的已经去precated 赞成的 XMLWorker 。这里是HTML代码段测试工作的例子像你上面使用:

`HTMLWorker' has been deprecated in favor of XMLWorker. Here is a working example tested with a snippet of HTML like you used above:

StringReader html = new StringReader(@"
<div style='font-size: 18pt; font-weight: bold;'>
Mouser Electronics <br />Authorized Distributor</div><br /> <br />
<div style='font-size: 14pt;'>Click to View Pricing, Inventory, Delivery & Lifecycle Information:
</div>
<br />
<div>
<table>
<tr><td></td><td>
<a href='http://www.mouser.com/access/?pn=78211-009' 
style='color: Blue; font-size: 10pt; text-decoration: underline;'>78211-009</a></td></tr>
</table></div>    
");      
using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(document, STREAM);
  document.Open();
  XMLWorkerHelper.GetInstance().ParseXHtml(
    writer, document, html
  );
}

在使用 XMLWorker 你需要使用结构良好的HTML - 这是一个XML解析器,毕竟。样本 HTML 从上面你的问题没有结束&LT; A&GT; &LT ; BR&GT; 标记。如 HtmlAgilityPack A HTML 解析器会修复这些的问题,并把这样的:

When using XMLWorker you need to use well-formed HTML - it's an XML parser, after all. The sample HTML from your question above doesn't have closing <a> or <br> tags. A HTML parser like HtmlAgilityPack will fix those problems, and turn this:

<div><img src='a.gif'><br><hr></div>

这个:

<div><img src='a.gif' /><br /><hr /></div>

只有code几行:

with only a few lines of code:

var hDocument = new HtmlDocument()
{
    OptionWriteEmptyNodes = true,
    OptionAutoCloseOnEnd = true
};
hDocument.LoadHtml("<div><img src='a.gif'><br><hr></div>");
var closedTags  = hDocument.DocumentNode.WriteTo();

XMLWorker 可作为的NuGet包,或如在SourceForge 单独下载。

查看这里的更高级用法 XMLWorker

See here for more advanced usage of XMLWorker.

这篇关于iTextSharp的上试图解析HTML的PDF转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆