首页 > 解决方案 > 如何为 FastReport.Web 中的元素设置字体以便应用于 pdf 导出?

问题描述

我正在为我的 dotnet 核心应用程序使用快速报告网络。我有一个包含一些元素的报告。 这些元素使用客户端 pc 上不存在的特殊字体

如果我在设计器中设置字体,则仅当该字体安装在客户端 PC 上时才会起作用。

如何为这些元素设置字体,以便预览和 pdf 导出中的文本正确?

此外,我有打印问题,例如 pdf 和打印中的分隔字符和无序的数字和字母。(我的报告语言是波斯语,它是 rtl)

我的代码是:

在控制器中:

var webReport = new WebReport();
webReport.Report.RegisterData(some_data, "Data");
var file = System.IO.Path.Combine(_env.WebRootPath, "Reports\\" + model.File);
webReport.Report.Load(file);
return View(webReport);

鉴于:

<div id="printBody" style="width:100%">
    @await Model.Render()
</div>

提前感谢您的帮助。

标签: .net-corefontsfastreport

解决方案


目前看来,fastreport web 不支持 rtl 语言的打印和 pdf 导出的全部功能。所以我做了以下场景,我得到了可接受的结果。

1 - 将报告导出到图像

2 - 使用iTextSharp将准备好的图像转换为 pdf

3 - 返回准备好的 pdf

这样就不需要在客户端电脑上安装字体了。

我使用了以下功能:

public FileStreamResult ReportToPdf(WebReport rpt, IWebHostEnvironment _env)
    {

        rpt.Report.Prepare();
        using (ImageExport image = new ImageExport())
        {
            //Convert to image
            image.ImageFormat = ImageExportFormat.Jpeg;
            image.JpegQuality = 100; // quality
            image.Resolution = 250; // resolution 
            image.SeparateFiles = true;
            var fname = "";
            var no = new Random().Next(10, 90000000);
            fname = $"f_{DateTime.Now.Ticks}_{no}";
            rpt.Report.Export(image, _env.WebRootPath + "\\Exports\\" + fname + ".jpg");


            // Convert to pdf
            MemoryStream workStream = new MemoryStream();
            Document document = new Document();
            PdfWriter.GetInstance(document, workStream).CloseStream = false;
            document.SetMargins(0, 0, 0, 0);

            document.SetPageSize(PageSize.A4);
            document.Open();
            float documentWidth = document.PageSize.Width;
            float documentHeight = document.PageSize.Height;

            foreach (var path in image.GeneratedFiles)
            {
                var imagex = Image.GetInstance(System.IO.File.ReadAllBytes(path));
                imagex.ScaleToFit(documentWidth, documentHeight);
                document.Add(imagex);

                try
                {
                    System.IO.File.Delete(path);
                }
                catch { }
            }
            document.Close();
            byte[] byteInfo = workStream.ToArray();
            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;

            return new FileStreamResult(workStream, "application/pdf");
        }
    }

推荐阅读