Search code examples
c#jqueryasp.net-mvc-3wkhtmltopdf

Removing a "Print" button Before Rendering PDF from View - MVC3


I have a view that I pass to Rotativa (wkhtmltopdf) to get a PDF version of the view for users to print, once they click the "print button":

Here is the rendered view that will be converted to PDF if the user clicks the "print" button

enter image description here

Here is the button code from the view

<div id="print-pdf" align="center">
    <a id="print-me"  class = "print-btn"
   href='@Url.Action("PrintMyView", "Report", 
             new { id = Model.MonthlyReportID, 
                   clubKeyNo = Model.ClubKeyNumber, 
                   month = Model.ReportMonth, 
                   year = Model.ReportYear }, null)'>Print Report</a>
</div>

Common sense tells me that I need to remove the button before the PDF is generated from the view, so users don't get a PDF with a button on it:

enter image description here

I tried hiding the button (.hide()) and also removing it (.remove()) but the PDF is still rendering with the button:

<script type="text/javascript">

    $(document).ready(function () {

        $('#print-me').show(); 

        $('#print-pdf').click(function () {
            $('#print-me').hide();
        });
    });


</script>

Anything else that I could try here?

Thanks!


Solution

  • You can do a version for PDF so you can handled what and what no should be printed

    what are running in javascript does not work because then executed when the doom is ready, but the Rotativa will not execute any javascript

    so, you render just what you need

    @if (Model.mustPrint){ 
        <div id="print-pdf" align="center">
            <a id="print-me"  class = "print-btn"    href='@Url.Action("PrintMyView", "Report",
                         new
                         {
                             id = Model.MonthlyReportID,
                             clubKeyNo = Model.ClubKeyNumber,
                             month = Model.ReportMonth,
                             year = Model.ReportYear
                         }, null)'>Print Report</a> 
        </div>
    }