简体   繁体   English

如何将MVC视图下载为PDF?

[英]How to download an MVC view as a PDF?

I'm currently coding in Visual Studio 2015. It's for a school project and I need to download a view as a PDF. 我目前在Visual Studio 2015中进行编码。这是针对学校项目的,我需要将视图下载为PDF。 I tried using a lot of methods posted here but can't seem to get it to work. 我尝试使用此处发布的许多方法,但似乎无法使其正常工作。 It's a simple view with information displayed from tables in a view and I want to download the view as a PDF. 这是一个简单的视图,具有从视图中的表中显示的信息,我想将视图下载为PDF。 Any easy suggestions. 任何简单的建议。

if you mean convert html to pdf, you can refer to below post 如果您的意思是将html转换为pdf,则可以参考以下帖子

Convert HTML to PDF in .NET 在.NET中将HTML转换为PDF

Actually your question is not clear. 其实你的问题还不清楚。 Anyways if you have that pdf file you may achieve that with FileResult method in Controller Refer this 不管怎么说,如果你有一个PDF文件,你可以做到这一点与FileResult方法控制器请参阅本

Or else if you want that particular view as it exactly as in pdf u may add a Save as PDF button in your page and make that button action same as Ctrl+P action as PDF. 否则,如果您希望该特定视图与pdf中的完全相同,则可以在页面中添加“另存为PDF”按钮,并使该按钮的操作与Ctrl + P操作相同。

Or else if you want another way please review your question and provide correct requirements. 否则,如果您想要其他方式,请查看您的问题并提供正确的要求。

How to download an MVC view as a PDF? 如何将MVC视图下载为PDF? using Rotativa 使用Rotativa

  1. Install NuGet Package 安装NuGet软件包
Install-Package Rotativa -Version 1.7.4-rc
  1. C# MVC C#MVC
//UserController
public ActionResult Download(string encryptReportId)
{
    //business login to bind this view
    //bind view using this encryptReportId
    return View();
}

public ActionResult DownloadViewAsPDF(string encryptId)
{
    return new Rotativa.ActionAsPdf("Download", new { encryptReportId = encryptReportId }) { FileName = "TestingPdf.pdf" };
}
  1. Html HTML
//use ajax call for this DownloadViewAsPDF of User Controller
$.ajax({
    url: "/User/DownloadViewAsPDF",
    data: {
        encryptId ': id,//id can be get on download button click event as a parameter
    },
    type: "GET",
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    success: function(result) {
        alert("Report Downloaded as PDF!!");
    },
    error: function(errormessage) {
        alert(errormessage.responseText);
    }
});

I hope this will help. 我希望这将有所帮助。

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

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