Skip to content

Export to Pdf No GUI

Peter Gill edited this page May 3, 2024 · 5 revisions

This code demonstrates the basics of loading a report with no gui and exporting to pdf.

This requires the following nuget packages.

dotnet add package Majorsilence.Reporting.RdlEngine
dotnet add package Majorsilence.Reporting.RdlCri

Code reads rdl file into string, parses, and initializes a report, finally exporting to pdf.

using System;
using fyiReporting.RDL;

namespace NoGuiExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var rpt = GetReport(new Uri(@"C:\Users\peter.WHEYGROUP\Downloads\SimpleTest1.rdl"));
            SaveReport(@"C:\Users\peter.WHEYGROUP\Downloads\simpletest1.pdf", rpt);
        }
        private static void SaveReport(string savePath, Report rpt)
        {
            rpt.RunGetData(null);
            var sg = new fyiReporting.RDL.OneFileStreamGen(savePath, true);
            rpt.RunRender(sg,  OutputPresentationType.PDF);
        }

        private static Report GetReport(Uri sourcefile)
        {
            var reportSource = System.IO.File.ReadAllText(sourcefile.AbsolutePath);
            var rdlp = new RDLParser(reportSource);
            var r = rdlp.Parse();

            if (r.ErrorMaxSeverity > 0)
            {
                foreach (string emsg in r.ErrorItems)
                {
                    Console.WriteLine(emsg);
                }

                int severity = r.ErrorMaxSeverity;
                r.ErrorReset();
                if (severity > 4)
                {
                    r = null; // don't return when severe errors
                }
            }

            return r;
        }
    }
}