Stack Exchange
Value cannot be null. Parameter name: elementId in PDFsharp
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;
    }


Can we see the stack trace?

A stack trace would really help, there is no parameter named elementId.

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.

Perhaps also https://github.com/ArthurHub/HTML-Renderer/blob/cc3b5c221b192a950d3d53b561ac01049668e239/Source/HtmlRenderer/Core/HtmlContainerInt.cs @TimSchmelter?

@User_2235 does the issue occur if htmlcontenttbl has all of the links (a tags) removed?

@mjwills: you're right. I guess it's this line where the AnchorId is null or empty, this code is reachable from PdfGenerator.GeneratePdf

@mjwills thank you , i removed blank anchor tag and error is gone :)

The title is wrong - this is not a problem of PDFsharp, it is a problem of PdfGenerator.

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.

What is considered a "blank anchor tag"? I have a few containing icons, nbsp and various other elements but the rendering still fails.

<a> is an anchor tag @AlenSiljak.

Thanks, I'm aware of that but, as I've asked earlier, what is a "blank anchor tag" that breaks the renderer? I've got no blank anchor tags and the renderer still throws this exception.

Looking at the code (https://github.com/ArthurHub/HTML-Renderer/blob/cc3b5c221b192a950d3d53b561ac01049668e239/Source/HtmlRenderer/Core/HtmlContainerInt.cs#L619) I am going to guess one without an id (it has been almost two year since I've looked at this stuff). If you add ids to your <a>s does it work? Alternatively, it may be one without a href as per https://github.com/ArthurHub/HTML-Renderer/blob/e0a2d1702aae5a4f267cc60d847c2c64c1a5b0bc/Source/HtmlRenderer/Core/Entities/LinkElementData.cs#L83 .

Do you have any <a> tags with no href or where the href is just # @AlenSiljak?

thanks for the pointers! Yes, there are a few without text (just icons) and a few with # in href, due to use of Bootstrap 3. And, of course, no a element has an id. :) I'll try to experiment a bit and see what works.

@AlenSiljak, do you remember what ended up working?

@Lukas, I think I removed all the annotations but it is hard to even remember the context now. Sorry!
