Search code examples
c#htmlpdf-generation

Convert panel HTML to PDF using C# ASP.NET


I have a basic form with mostly textboxes and a few checkboxes and radio buttons. Pretty much when a user fills it out and hits submit, I need it to convert and display a PDF with the same structure and offer a print option. I have searched online and I have tried most of the options but nothing works.

I don't expect this to be too difficult, but I am fairly new to C# and can't figure out how to make an HTML panel into a PDF and print it. Any help would be appreciated, thanks in advance!

Here is the HTML for one of the labels and textboxes and the submit button:

        <div>
            <asp:Label ID="lblDate" runat="server" Text="Date:"></asp:Label>
            <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
        </div>

    <asp:Button ID="SubmitButton" runat="server" Text="Button" OnClick="btnSubmit"/>

Once clicked, the .cs page will go through this:

    litDate.Text = "Date: " + txtDate.Text + "<br />";

and update the panel to display the value:

<asp:Panel ID="PDFPanel" runat="server" Visible="false">
            <asp:Literal ID="litDate" runat="server"></asp:Literal>
</asp:Panel>

I am not sure if the panel is needed, but this is how I was able to be sure I was getting the value back. Is there a way to go straight from submit to PDF and print?


Solution

  • Use something like http://code.google.com/p/wkhtmltopdf/

    This roughly outlines how to do it: https://stackoverflow.com/a/18767473/181771