简体   繁体   English

在从视图渲染 PDF 之前删除“打印”按钮 - MVC3

[英]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":我有一个视图,我传递给Rotativa (wkhtmltopdf) 以获得视图的 PDF 版本供用户打印,一旦他们单击“打印按钮”:

Here is the rendered view that will be converted to PDF if the user clicks the "print" button这是如果用户单击“打印”按钮将转换为 PDF 的渲染视图

在此处输入图片说明

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:常识告诉我,我需要在从视图中生成 PDF 之前删除该按钮,因此用户不会获得带有按钮的 PDF:

在此处输入图片说明

I tried hiding the button (.hide()) and also removing it (.remove()) but the PDF is still rendering with the button:我尝试隐藏按钮 (.hide()) 并删除它 (.remove()) 但 PDF 仍在使用按钮呈现:

<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!谢谢!

Try this css:试试这个css:

@media print 
{
    #print-me
    {
        display:none;
    }
}

ref 参考

You can do a version for PDF so you can handled what and what no should be printed您可以为 PDF 制作一个版本,以便您可以处理应该打印的内容和不应该打印的内容

what are running in javascript does not work because then executed when the doom is ready, but the Rotativa will not execute any javascript在 javascript 中运行的内容不起作用,因为在末日准备就绪时执行,但 Rotativa 不会执行任何 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>
}

Rotativa does not render as print media, but as a browser, you must force rendering as print. Rotativa 不呈现为打印媒体,但作为浏览器,您必须强制呈现为打印。 Send the " --print-media-type " argument发送“ --print-media-type ”参数

Doing this you can use css to hide if the media for print这样做你可以使用 css 来隐藏打印的媒体

@media print {
.noprint {
display: none! important
}}

Ex:前任:

    return new ViewAsPdf("Details", obj)
                    {
                        CustomSwitches = "--print-media-type"
}

Complete example:完整示例:

return new ViewAsPdf("Details", obj)
                {
                    CustomSwitches = "--print-media-type",
                    FileName = your_name_report.pdf",
                    PageOrientation = Orientation.Portrait,
                    PageSize = Size.A4,
                    PageMargins = new Margins(0, 0, 0, 0),
                    PageWidth = 210,
                    PageHeight = 297
                };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM