问题
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
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:
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!
回答1:
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>
}
回答2:
Try this css:
@media print
{
#print-me
{
display:none;
}
}
ref
来源:https://stackoverflow.com/questions/18512693/removing-a-print-button-before-rendering-pdf-from-view-mvc3