【发布时间】:2015-03-06 12:49:56
【问题描述】:
我想在我的 ASP.NET MVC 5 应用程序中创建报告。我找到了这段代码(http://report.sourceforge.net/ 中的 Hello world 示例):
Report report = new Report(new PdfFormatter());
FontDef fd = new FontDef(report, "Helvetica");
FontProp fp = new FontPropMM(fd, 25);
Page page = new Page(report);
page.AddCB_MM(80, new RepString(fp, "Hello World!"));
RT.ViewPDF(report, "HelloWorld.pdf");
当我将此代码放在控制台应用程序的主程序中时,它会创建并打开一个 PDF 文件,其中写有“Hello World”。 但是当我像这样将相同的代码放在 ASP.NET 中的控制器中时,我什么也得不到,没有打开 PDF 或没有 PDF 保存在服务器中(我单击 mywebsite/Person/Print):
public ActionResult Print(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person= db.Person.Find(id);
if (person== null)
{
Report report = new Report(new PdfFormatter());
FontDef fd = new FontDef(report, "Helvetica");
FontProp fp = new FontPropMM(fd, 25);
Page page = new Page(report);
page.AddCB_MM(80, new RepString(fp, "Hello World!"));
RT.ViewPDF(report, "HelloWorld.pdf");
}
return View(person);
}
如何修改我的 ASP.NET 应用程序以让用户使用此代码获取 PDF? (如果您知道任何适用于 ASP.NET MVC 5 的无酷报告工具,我也将不胜感激。)谢谢。
【问题讨论】:
-
我们没有关于 Report 类的信息。它来自哪里?这是什么图书馆?
-
你必须彻底改变你正在采取的方法。运行控制台应用程序时,输出将在运行的机器上呈现并照常显示。但是一个网站或服务在服务器上计算并将结果返回给客户端,因此交互模型完全不同。您必须允许用户通过 http/s 将其作为可下载资源提供来下载 pdf,或者通过动态生成它们并返回
File或在服务器上生成它们并为客户端提供链接到资源。 -
@Sphaso 我添加了关于图书馆的链接。
-
您似乎正在使用控制台应用程序,
RT.ViewPDF启动了 Adobe Reader。出于显而易见的原因,这在服务器环境中不起作用。但是那个库似乎已经过时了(最后一个版本是 2004 年和 2006 年)
标签: c# asp.net asp.net-mvc pdf asp.net-mvc-5