【问题标题】:Value cannot be null. Parameter name: elementId in PDFsharp值不能为空。参数名称:PDFsharp 中的 elementId
【发布时间】:2017-09-13 13:50:18
【问题描述】:

我正在使用 C# 和 PDFSharp 处理导出到 PDF 的功能。我收到此错误:

Value cannot be null.
Parameter name: elementId

错误在这一行: PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(), PdfSharp.PageSize.A4, 30);

这是整个方法:

public bool ExportPdf(string htmlcontenttbl)
    {
        Response.ClearContent();
        Response.ClearHeaders();

        Response.Buffer = true;
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment;filename=Myfile.pdf");
        //Response.AddHeader("Content-Disposition", "inline;filename=file.pdf");
        //Response.AppendHeader("Content-Disposition", "attachment; filename=Myfile.pdf");
        Response.Charset = "";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(), PdfSharp.PageSize.A4, 30);
        var config = new PdfGenerateConfig();
        config.PageOrientation = PageOrientation.Landscape;
        config.PageSize = PageSize.A4;
        config.MarginBottom = 30;
        config.MarginTop = 30;
        //PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl, config);

        byte[] bytes = null;
        using (MemoryStream stream = new MemoryStream())
        {
            document.Save(stream, true);
            bytes = stream.ToArray();
        }

        //var path1 = Server.MapPath("~/Images/" + DateTime.Now.TimeOfDay.Ticks + "result.pdf");
        //System.IO.File.WriteAllBytes(path1, bytes);
        //Response.TransmitFile(path1, 0, bytes.Length);
        //Response.OutputStream.Write(bytes, 0, bytes.Length);

        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();

        return true;
    }

【问题讨论】:

  • 我们能看到堆栈跟踪吗?
  • 堆栈跟踪真的很有帮助,没有名为 elementId 的参数。
  • ScrollToElement 是该项目中唯一引发此异常的地方:github.com/ArthurHub/HTML-Renderer/… 但不知道它来自何处以及为什么参数为null 或为空 .
  • 也许还有github.com/ArthurHub/HTML-Renderer/blob/…@TimSchmelter?
  • 标题错了——这不是PDFsharp的问题,是PdfGenerator的问题。

标签: c# .net pdfsharp


【解决方案1】:

如果您有一个空白的锚标记,则可能会发生该异常。

它很可能最终是从here 生成的。

您应该删除空白的锚标记。

【讨论】:

  • 什么是“空白锚标签”?我有一些包含图标、nbsp 和各种其他元素,但渲染仍然失败。
  • <a> 是一个锚标签@AlenSiljak。
  • 谢谢,我知道这一点,但正如我之前问过的,什么是破坏渲染器的“空白锚标记”?我没有空白的锚标签,渲染器仍然抛出这个异常。
  • 查看代码 (github.com/ArthurHub/HTML-Renderer/blob/…) 我猜一个没有id 的代码(自从我看到这些东西已经快两年了)。如果您将ids 添加到您的<a>s 中是否有效?或者,根据github.com/ArthurHub/HTML-Renderer/blob/…,它可能是没有href 的。
  • 你有没有<a>标签没有href或者href只是#@AlenSiljak?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 2014-05-17
  • 2016-05-14
  • 2015-11-05
相关资源
最近更新 更多