FastReport中国社区FastReport联系电话 联系电话:023-68661681

如何在Linux上使用FastReport Core

来源:   发布时间:2018-02-05   浏览:4512次

今天的文章中,我们来看一个教你如何在Linux上使用FastReport Core的例子。

首先对于Linux,我们将需要额外的库,默认情况下可能并没有安装:

  • Libgdiplus;
  • libx11-dev。

在服务器端Linux操作系统上通常没有安装XServer,而这是处理FastReport Core图形所必需的。所以我们必须安装它。你可以选择Xvfb、VcXsrv或任何其他服务。

我们待会儿再来讲XServer的问题,现在,我们先创建一个ASP.Net Core应用程序:

如何在Linux上使用FastReport Core

选择Web应用程序:

如何在Linux上使用FastReport Core

通过NuGet将FastReport Core添加到项目中。

如何在Linux上使用FastReport Core

我们添加一个本地仓库作为NuGet选项中的包的来源:

如何在Linux上使用FastReport Core

设置对本地存储库或文件夹Service FastReport.2017.4.0-release.nupkg的引用:

如何在Linux上使用FastReport Core

从下拉列表中选择软件包的本地源并安装FastReport软件包。

从“Controllers”文件夹中打开“HomeController.cs”文件。我们添加几个额外的库到使用部分:

using FastReport;
using FastReport.Export.Html;
using System.IO;
using System.Text;

我们来编辑Index方法:

 public IActionResult Index()
 {
 Report report = new Report();
 
 string report_path = ”Reports”;
 System.Data.DataSet dataSet = new System.Data.DataSet();
 dataSet.ReadXml(report_path + "nwind.xml");
 report.Report.RegisterData(dataSet, "NorthWind");
 report.Report.Load(Path.Combine(report_path, "Simple List.frx"));
 report.Prepare();
 HTMLExport export = new HTMLExport();
 export.Layers = true;
 using (MemoryStream ms = new MemoryStream())
 {
 export.EmbedPictures = true;
 export.Export(report, ms);
 ms.Flush();
 ViewData["Report"] = Encoding.UTF8.GetString(ms.ToArray());
 ViewData["ReportName"] = "Simple List.frx";
 }
 return View();

由于WebReport对象在FastReport Core中暂时不可用,因此我们使用常规报表对象。创建一个数据源并将其注册到报表对象中。加载报表模板。使用 Prepare () 方法准备报表。接下来,我们在HTML中创建已完成的报表的导出。我们导出为MemoryStream流(或文件)。然后,我们使用ViewData或ViewBag将报表传递给视图。

现在继续编辑“Views - > Index.chtml”视图。

这非常简单 - 我们以HTML格式显示报表:

@{
 ViewData["Title"] = "Home Page";
}
@if (ViewData.ContainsKey("Report"))
{
 @Html.Raw(ViewData["Report"])
}

如前所述,FRCore需要XServer,而服务器端Linux并不提供。

我们来看一个使用debian服务器配置Linux示例:

1.打开控制台;

2.更新apt-get并安装软件包:

  • sudo apt-get update;
  • sudo apt-get install -y xvfb x11vnc x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps libgdiplus libx11-dev;

3.更改环境变量,DISPLAY =: 99

我们在Program类中添加了两个方法。LinuxStart方法检查是否正在运行,如果是XServer,如果是的话则关闭它并创建一个新的。

StopLinux方法仅停止虚拟服务器。

public class Program
 {
 static Process xvfb;
 const string xvfb_pid = "pid.xvfb.fr";
 public static void Main(string[] args)
 {
 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
 LinuxStart();
 BuildWebHost(args).Run();
 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
 LinuxStop();
 }
 
 private static void LinuxStop()
 {
 xvfb.Kill();
 if (File.Exists(xvfb_pid))
 File.Delete(xvfb_pid);
 }
 
 public static void LinuxStart()
 {
 if (File.Exists(xvfb_pid))
 {
 string pid = File.ReadAllText(xvfb_pid);
 try
 {
 xvfb = Process.GetProcessById(int.Parse(pid));
 xvfb.Kill();
 xvfb = null;
 }
 catch { }
 File.Delete(xvfb_pid);
 }
 string display = Environment.GetEnvironmentVariable("DISPLAY");
 if (String.IsNullOrEmpty(display))
 {
 Environment.SetEnvironmentVariable("DISPLAY", ":99");
 display = ":99";
 }
 ProcessStartInfo info = new ProcessStartInfo();
 info.FileName = "/usr/bin/Xvfb";
 info.Arguments = display + " -ac -screen 0 1024x768x16 +extension RANDR -dpi 96";
 info.CreateNoWindow = true;
 xvfb = new Process();
 xvfb.StartInfo = info;
 xvfb.Start();
 File.WriteAllText(xvfb_pid, xvfb.Id.ToString());
 }
 
 public static IWebHost BuildWebHost(string[] args) =>
 WebHost.CreateDefaultBuilder(args)
 .UseStartup<Startup>()
 .UseUrls("http://[::]:5000")
 .Build();
 } 

应用程序已准备就绪。运行查看效果:

如何在Linux上使用FastReport Core

总结一下,我们可以得出结论,和Windows一样,在Linux上运行FastReport Core非常简单。只不过需要一些操作系统的高级设置才能使用.Net Core。


产品介绍 | 下载试用 | 优惠活动 | 在线客服 | 联系Elyn

 

推荐阅读

FastReport正版授权6.9折优惠
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:FastReport控件中文网 [https://www.fastreportcn.com/]
本文地址:https://www.fastreportcn.com/post/2011.html

联系我们
  • 重庆总部 023-68661681
购买
  • sales@evget.com
合作
  • business@evget.com


在线
客服
微信
QQ 电话
023-68661681
返回
顶部