-2

I am working on Export to PDF funtionality using C# and PDFSharp. I am getting this error:

Value cannot be null.
Parameter name: elementId

The error is on this line : PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(), PdfSharp.PageSize.A4, 30);

Here's the whole method:

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;
    }
User_2235
  • 39
  • 2
  • 11
  • 3
    Can we see the stack trace? – 15ee8f99-57ff-4f92-890c-b56153 Sep 13 '17 at 13:51
  • 2
    A stack trace would really help, there is no parameter named elementId. – Ben Hoffman Sep 13 '17 at 13:55
  • `ScrollToElement` is the only place in that project that throws this exception: https://github.com/ArthurHub/HTML-Renderer/search?utf8=%E2%9C%93&q=elementId&type= But no idea from where it came and why the parameter was `null` **or empty**. – Tim Schmelter Sep 13 '17 at 14:13
  • Perhaps also https://github.com/ArthurHub/HTML-Renderer/blob/cc3b5c221b192a950d3d53b561ac01049668e239/Source/HtmlRenderer/Core/HtmlContainerInt.cs @TimSchmelter? – mjwills Sep 13 '17 at 14:16
  • @User_2235 does the issue occur if `htmlcontenttbl` has all of the links (`a` tags) removed? – mjwills Sep 13 '17 at 14:18
  • @mjwills: you're right. I guess it's [this line](https://github.com/ArthurHub/HTML-Renderer/search?utf8=%E2%9C%93&q=var+anchorRect+%3D+container.GetElementRectangle%28link.AnchorId%29%3B&type=) where the `AnchorId` is null or empty, this code is reachable from `PdfGenerator.GeneratePdf` – Tim Schmelter Sep 13 '17 at 14:18
  • @mjwills thank you , i removed blank anchor tag and error is gone :) – User_2235 Sep 14 '17 at 04:25
  • 1
    The title is wrong - this is not a problem of PDFsharp, it is a problem of PdfGenerator. – I liked the old Stack Overflow Sep 14 '17 at 09:45

1 Answers1

2

That exception can occur if you have a blank anchor tag.

It is likely ultimately being generated from here.

You should remove the blank anchor tag.

mjwills
  • 23,389
  • 6
  • 40
  • 63