Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 元素不允许错误itextSharp_C#_Pdf_Itext - Fatal编程技术网

C# 元素不允许错误itextSharp

C# 元素不允许错误itextSharp,c#,pdf,itext,C#,Pdf,Itext,我正在尝试使用itextsharp将html转换为pdf。但始终使用HtmlAgilityPack获取错误“element not allowed”Html格式。 ........................................................................................................................................................................

我正在尝试使用itextsharp将html转换为pdf。但始终使用HtmlAgilityPack获取错误“element not allowed”Html格式。 ..................................................................................................................................................................................................... 这是我的密码

 hDocument.LoadHtml(html);
            var closedTags = hDocument.DocumentNode.WriteTo();
            var _cssSource = Server.MapPath("~/css/style.css");
            var css = System.IO.File.ReadAllText(_cssSource);
            var memoryStram = new MemoryStream();
            var memmoryStram = new MemoryStream();
            var document = new Document(PageSize.A4, 10, 10, 10, 10);
            var writer = PdfWriter.GetInstance(document, memmoryStram);
            document.Open();
            using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(css)))
            {
                using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(closedTags)))
                {
                    // XMLWorker _worker = new XMLWorker(
                    StringReader htmls = new StringReader(html);
                    var p = XMLWorkerHelper.GetInstance();
                    p.ParseXHtml(writer, document, htmlMemoryStream, cssMemoryStream);
                }
            }
            document.Close();
错误:

         at iTextSharp.text.pdf.ColumnText.AddElement(IElement element)
      at iTextSharp.text.pdf.PdfPCell.AddElement(IElement element)
     at iTextSharp.tool.xml.html.table.TableData.End(IWorkerContext ctx, Tag tag, IList`1 currentContent)
   at iTextSharp.tool.xml.html.AbstractTagProcessor.EndElement(IWorkerContext ctx, Tag tag, IList`1 currentContent)
   at iTextSharp.tool.xml.pipeline.html.HtmlPipeline.Close(IWorkerContext context, Tag t, ProcessObject po)
 at iTextSharp.tool.xml.XMLWorker.EndElement(String tag, String ns)
 at iTextSharp.tool.xml.parser.XMLParser.EndElement()
 at iTextSharp.tool.xml.parser.state.ClosingTagState.Process(Char character)
 at iTextSharp.tool.xml.parser.XMLParser.ParseWithReader(TextReader reader)
  at iTextSharp.tool.xml.parser.XMLParser.Parse(TextReader reader)
 at iTextSharp.tool.xml.parser.XMLParser.Parse(Stream inp)
  at iTextSharp.tool.xml.XMLWorkerHelper.ParseXHtml(PdfWriter writer, Document doc, Stream inp, Stream inCssFile, Encoding charset, IFontProvider fontProvider, String resourcesRootPath)
  at iTextSharp.tool.xml.XMLWorkerHelper.ParseXHtml(PdfWriter writer, Document doc, Stream inp, Stream inCssFile, Encoding charset, IFontProvider fontProvider)
  at iTextSharp.tool.xml.XMLWorkerHelper.ParseXHtml(PdfWriter writer, Document doc, Stream inp, Stream inCssFile)
  at IBATechnologies.IBA.SyncApp.Interface.Controllers.UtilitiesController.pdfBytes(String html) in E:\IBASyncApp\IBA.SyncApp.Interface\IBATechnologies.IBA.SyncApp.Interface\Controllers\UtilitiesController.cs:line 54
封闭式标签

     <!-- ngInclude: _rpt.tmpUrl --><div class="col-all-12 PDF-report-holder   ng-  scope" ng-include="_rpt.tmpUrl"><table id="myTable" class="table-responsive ng-scope">
     <tbody><tr class="header">
   <th colspan="5">
   <h2 class="col-all-12"> Branch</h2>
   <p class="col-all-12 ng-binding">WMS Stock Status - Item Summary</p>
  </th>

 <th class="-Pdf-logo">
       <a href="http://technologies.com/" class="col-all-12"><img       src="/image/default/logo.png" /></a>
     </th>
     </tr>
     <tr class="-Pdf-head">
  <td>SKU</td>
   <td>Base Unit</td>
   <td>Base Qty</td>
  <td>Pltpos(s)</td>
   <td>CBM</td>
   <td>Weight TON</td>

    </tr>
     </tbody><!-- ngRepeat: cust in _rpt.data --><tbody ng-repeat="cust in _rpt.data"    class="ng-scope">
<tr>
<td colspan="6"><strong><p class="ng-binding">Test Customer</p></strong></td>

 </tr>
  <!-- ngRepeat: row in cust.itemGroup --><tr ng-repeat="row in    cust.itemGroup"     ng-repeat-finished="rpt_ng_repeat" class="ng-scope">
     <td><p class="ng-binding">100 Test Item</p></td>
      <td><p class="ng-binding">PLT</p></td>
      <td><p class="ng-binding">68</p></td>
        <td><p class="ng-binding">68</p></td>
       <td><p class="ng-binding">68</p></td>
       <td><p class="ng-binding">0.068</p></td>
       </tr><!-- end ngRepeat: row in cust.itemGroup -->
       <tr class="-Pdf-head">
       <td colspan="3"><p>Total</p></td>
     <td><p class="ng-binding">68</p></td>
   <td><p class="ng-binding">68</p></td>
   <td><p class="ng-binding">0.068</p></td>
   </tr>
       </tbody><!-- end ngRepeat: cust in _rpt.data -->
      </table>
    </div>
回应

public class PdfContent : ActionResult
{
    public MemoryStream MemoryStream { get; set; }
    public string FileName { get; set; }
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        var response = context.HttpContext.Response;
        response.ContentType = "pdf/application";
        response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".pdf");
        response.OutputStream.Write(MemoryStream.GetBuffer(), 0, MemoryStream.GetBuffer().Length);
    }
}
CSS

原因 显然,iText
XMLWorker
不接受表头元素(
h1
h2
h3
h4
h5
,或
h6
)中的标题元素(
th
):如果您在此处删除
h2
元素

<th colspan="5">
  <h2 class="col-all-12"> Branch</h2>
  <p class="col-all-12 ng-binding">WMS Stock Status - Item Summary</p>
</th>

被拒绝,与HTML的异常情况相同

说明书 根据HTML5规范,表标题中的标题元素实际上是不允许的:

4.9.10
th
元素 [……]

内容模型:

流动内容,但没有页眉、页脚、分段内容或标题内容子体

()

其中标题内容定义为

3.2.4.1.4标题内容 标题内容定义节的标题(无论是使用节内容元素显式标记,还是由标题内容本身暗示)

=>h1 h2 h3 h4 h5 h6

()

底线 因此,您的HTML输入不是有效的HTML,因此,iText(或任何HTML处理器)可能无法处理它


测试代码 作为参考,源于您的代码,这是我用于测试的代码:

public void CreatePdfAjithOriginal()
{
    String html = "<!-- ngInclude: _rpt.tmpUrl -->"
        + "<div class=\"col-all-12 PDF-report-holder ng-scope\" ng-include=\"_rpt.tmpUrl\">"
          + "<table id=\"myTable\" class=\"table-responsive ng-scope\">"
            + "<tbody>"
              + "<tr class=\"header\">"
                + "<th colspan=\"5\">"
//                  + "<h2 class=\"col-all-12\"> Branch</h2>"
                  + "<p class=\"col-all-12 ng-binding\">WMS Stock Status - Item Summary</p>"
                + "</th>"
                + "<th class=\"-Pdf-logo\">"
                  + "<a href=\"http://technologies.com/\" class=\"col-all-12\"><img src=\"/image/default/logo.png\" /></a>"
                + "</th>"
              + "</tr>"
              + "<tr class=\"-Pdf-head\">"
                + "<td>SKU</td>"
                + "<td>Base Unit</td>"
                + "<td>Pltpos(s)</td>"
                + "<td>CBM</td>"
                + "<td>Weight TON</td>"
              + "</tr>"
            + "</tbody>"
            + "<!-- ngRepeat: cust in _rpt.data -->"
            + "<tbody ng-repeat=\"cust in _rpt.data\" class=\"ng-scope\">"
              + "<tr>"
                + "<td colspan=\"6\"><strong><p class=\"ng-binding\">Test Customer</p></strong></td>"
              + "</tr>"
              + "<!-- ngRepeat: row in cust.itemGroup -->"
              + "<tr ng-repeat=\"row in cust.itemGroup\" ng-repeat-finished=\"rpt_ng_repeat\" class=\"ng-scope\">"
                + "<td><p class=\"ng-binding\">100 Test Item</p></td>"
                + "<td><p class=\"ng-binding\">PLT</p></td>"
                + "<td><p class=\"ng-binding\">68</p></td>"
                + "<td><p class=\"ng-binding\">68</p></td>"
                + "<td><p class=\"ng-binding\">68</p></td>"
                + "<td><p class=\"ng-binding\">0.068</p></td>"
              + "</tr>"
              + "<!-- end ngRepeat: row in cust.itemGroup -->"
              + "<tr class=\"-Pdf-head\">"
                + "<td colspan=\"3\"><p>Total</p></td>"
                + "<td><p class=\"ng-binding\">68</p></td>"
                + "<td><p class=\"ng-binding\">68</p></td>"
                + "<td><p class=\"ng-binding\">0.068</p></td>"
              + "</tr>"
            + "</tbody>"
            + "<!-- end ngRepeat: cust in _rpt.data -->"
          + "</table>"
        + "</div>";
    // A minimal example provoking the exception:
//    String html = "<table><tr><th><h1>Test</h1></th></tr></table>";
    String css = ".PDF-report{position:fixed; top:0; right:-100%; background:#fff; width:75%; height:100%; z-index:100; padding:10px; border-left:1px solid #ccc}"
        + " .PDF-report-active{ right: 0}"
        + " .PDF-report table{background:#fff; min-width:100%}"
        + " .PDF-report table td{ min-width:100px;}"
        + " .IBA-Pdf-logo img{ float:right}"
        + " .IBA-Pdf-head{ border-top:5px solid #000; font-weight:bold }"
        + " .PDF-report-holder{ height: 100 %; overflow: scroll}"
        + " .col-all-12{width:100%;}"
        + " .col-all-12, .all-col{height:auto; padding:0; margin:0; float:left}";

    Directory.CreateDirectory(@"C:\Temp\test-results\xmlworker\");
    var document = new Document(PageSize.A4, 10, 10, 10, 10);
    var writer = PdfWriter.GetInstance(document, new FileStream(@"C:\Temp\test-results\xmlworker\AjithOriginal.pdf", FileMode.Create));
    document.Open();
    using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(css)))
    {
        using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
        {
            var p = XMLWorkerHelper.GetInstance();
            p.ParseXHtml(writer, document, htmlMemoryStream, cssMemoryStream);
        }
    }
    document.Close();
}
public void CreatePdfAjithOriginal()
{
字符串html=“”
+ ""
+ ""
+ ""
+ ""
+ ""
//+“分支”
+“

WMS库存状态-物料汇总

” + "" + "" + "" + "" + "" + "" +“SKU” +“基本单位” +"公共交通工具" +“CBM” +“重量吨” + "" + "" + "" + "" + "" +测试客户 + "" + "" + "" +“

100个测试项目

” +“PLT

” +“

68

” +“

68

” +“

68

” +“

0.068

” + "" + "" + "" +“总计

” +“

68

” +“

68

” +“

0.068

” + "" + "" + "" + "" + ""; //引发异常的一个最小示例: //字符串html=“Test”; 字符串css=“.PDF报告{位置:固定;顶部:0;右侧:-100%;背景:fff;宽度:75%;高度:100%;z索引:100;填充:10px;边框左侧:1px实心#ccc}” +“.PDF报告处于活动状态{右侧:0}” +“.PDF报告表{背景:#fff;最小宽度:100%}” +“.PDF报告表td{最小宽度:100px;}” +“.IBA Pdf徽标img{float:right}” +“.IBA Pdf头{边框顶部:5px实心#000;字体重量:粗体}” +“.PDF报表持有者{高度:100%;溢出:滚动}” +“.col-all-12{宽度:100%;}” +“.col-all-12,.all col{height:自动;padding:0;margin:0;float:left}”; CreateDirectory(@“C:\Temp\test results\xmlworker\”); var文件=新文件(PageSize.A4、10、10、10、10); var writer=PdfWriter.GetInstance(文档,新文件流(@“C:\Temp\test results\xmlworker\ajithoriging.pdf”,FileMode.Create)); document.Open(); 使用(var cssMemoryStream=newmemorystream(System.Text.Encoding.UTF8.GetBytes(css))) { 使用(var htmlemorystream=newmemorystream(System.Text.Encoding.UTF8.GetBytes(html))) { var p=XMLWorkerHelper.GetInstance(); p、 ParseXHtml(编写器、文档、htmlMemoryStream、cssMemoryStream); } } document.Close(); }
html和css中的额外空格都已从源代码中删除,因为我假设它们是复制到堆栈溢出时引入的工件。如果这个假设是错误的,那么在继续之前,应该对原始HTML和CSS进行清理

此外,还删除了未使用的代码,例如
var memoryStram
StringReader htmls
。原始代码也应该被清理

无论如何,上述结果是

另一方面,你说


生成的pdf文件为空

考虑到您最近添加的代码,我假设您的意思是,它在某些浏览器中显示为空白。因此,它要么在发送到
HttpContext.Response
的过程中丢失了,要么您的浏览器不喜欢它的发送方式

要验证PDF是否正确地访问了
HttpContext.Response
,您应该始终检查相关的
MemoryStream
实例是否携带PDF,例如通过将相应的
MemoryStream
ToArray()值存储到文件系统中

一个可能的问题:您没有显示您的方法
pdfBytes
如何实际返回
MemoryStream
;也许您使用空的
memoryStram
而不是生成PDF的
memoryStram

一旦您确定PDF确实到达
HttpContext.Response
且已损坏,请使用浏览器再次测试。如果是这样的话
<th colspan="5">
  <h2 class="col-all-12"> Branch</h2>
  <p class="col-all-12 ng-binding">WMS Stock Status - Item Summary</p>
</th>
<table><tr><td><h1>Test</h1></td></tr></table>
<table><tr><th><p>Test</p></th></tr></table>
<table><tr><th><h1>Test</h1></th></tr></table>
public void CreatePdfAjithOriginal()
{
    String html = "<!-- ngInclude: _rpt.tmpUrl -->"
        + "<div class=\"col-all-12 PDF-report-holder ng-scope\" ng-include=\"_rpt.tmpUrl\">"
          + "<table id=\"myTable\" class=\"table-responsive ng-scope\">"
            + "<tbody>"
              + "<tr class=\"header\">"
                + "<th colspan=\"5\">"
//                  + "<h2 class=\"col-all-12\"> Branch</h2>"
                  + "<p class=\"col-all-12 ng-binding\">WMS Stock Status - Item Summary</p>"
                + "</th>"
                + "<th class=\"-Pdf-logo\">"
                  + "<a href=\"http://technologies.com/\" class=\"col-all-12\"><img src=\"/image/default/logo.png\" /></a>"
                + "</th>"
              + "</tr>"
              + "<tr class=\"-Pdf-head\">"
                + "<td>SKU</td>"
                + "<td>Base Unit</td>"
                + "<td>Pltpos(s)</td>"
                + "<td>CBM</td>"
                + "<td>Weight TON</td>"
              + "</tr>"
            + "</tbody>"
            + "<!-- ngRepeat: cust in _rpt.data -->"
            + "<tbody ng-repeat=\"cust in _rpt.data\" class=\"ng-scope\">"
              + "<tr>"
                + "<td colspan=\"6\"><strong><p class=\"ng-binding\">Test Customer</p></strong></td>"
              + "</tr>"
              + "<!-- ngRepeat: row in cust.itemGroup -->"
              + "<tr ng-repeat=\"row in cust.itemGroup\" ng-repeat-finished=\"rpt_ng_repeat\" class=\"ng-scope\">"
                + "<td><p class=\"ng-binding\">100 Test Item</p></td>"
                + "<td><p class=\"ng-binding\">PLT</p></td>"
                + "<td><p class=\"ng-binding\">68</p></td>"
                + "<td><p class=\"ng-binding\">68</p></td>"
                + "<td><p class=\"ng-binding\">68</p></td>"
                + "<td><p class=\"ng-binding\">0.068</p></td>"
              + "</tr>"
              + "<!-- end ngRepeat: row in cust.itemGroup -->"
              + "<tr class=\"-Pdf-head\">"
                + "<td colspan=\"3\"><p>Total</p></td>"
                + "<td><p class=\"ng-binding\">68</p></td>"
                + "<td><p class=\"ng-binding\">68</p></td>"
                + "<td><p class=\"ng-binding\">0.068</p></td>"
              + "</tr>"
            + "</tbody>"
            + "<!-- end ngRepeat: cust in _rpt.data -->"
          + "</table>"
        + "</div>";
    // A minimal example provoking the exception:
//    String html = "<table><tr><th><h1>Test</h1></th></tr></table>";
    String css = ".PDF-report{position:fixed; top:0; right:-100%; background:#fff; width:75%; height:100%; z-index:100; padding:10px; border-left:1px solid #ccc}"
        + " .PDF-report-active{ right: 0}"
        + " .PDF-report table{background:#fff; min-width:100%}"
        + " .PDF-report table td{ min-width:100px;}"
        + " .IBA-Pdf-logo img{ float:right}"
        + " .IBA-Pdf-head{ border-top:5px solid #000; font-weight:bold }"
        + " .PDF-report-holder{ height: 100 %; overflow: scroll}"
        + " .col-all-12{width:100%;}"
        + " .col-all-12, .all-col{height:auto; padding:0; margin:0; float:left}";

    Directory.CreateDirectory(@"C:\Temp\test-results\xmlworker\");
    var document = new Document(PageSize.A4, 10, 10, 10, 10);
    var writer = PdfWriter.GetInstance(document, new FileStream(@"C:\Temp\test-results\xmlworker\AjithOriginal.pdf", FileMode.Create));
    document.Open();
    using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(css)))
    {
        using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
        {
            var p = XMLWorkerHelper.GetInstance();
            p.ParseXHtml(writer, document, htmlMemoryStream, cssMemoryStream);
        }
    }
    document.Close();
}