使用 Itextsharp 将 MVC 4 视图转换为 Pdf

Convert MVC 4 View To Pdf using Itextsharp

我正在尝试将 MVC 4 视图转换为 PDF。我不知道从哪里开始,在搜索 google 之后我找到了 ItextSharp 并一直在使用它。 我的视图相当简单,它有一个地图和一个 Table。我只想在控制器中调用一个动作并让它打印我的网页。 任何建议将不胜感激

您可以使用Rotativa

public ActionResult TestViewWithModel(string id)
{
    var model = new TestViewModel {DocTitle = id, DocContent = "This is a test"};
    return new ViewAsPdf(model);
}

public ActionResult PrintIndex()
{
    return new ActionAsPdf("Index", new { name = "Giorgio" }) { FileName = "Test.pdf" };
}

它在后台使用 wkhtmltopdf

wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command line tools to render HTML into PDF and various image formats using the QT Webkit rendering engine. These run entirely "headless" and do not require a display or display service.