Dynamically Adding Pages to Report Causes Internal Server Error

2 posts, 0 answers
  1. Victor
    Victor avatar
    11 posts
    Member since:
    Oct 2008

    Posted 12 Feb Link to this post

    I have a site that allows users to upload images to the report. The users can upload as many images as they want and the images get appended to the report.

    The report viewer initially contains a report and the users images are appended at the button of the report. The way I append the images: I have created a one page report that has an image in the middle of the report. I iterate through all the image paths and add a new report for each image.

     

    Here is an example of when the report processes the error. When a user adds 1 image and renders the report the report view looks great but once they add more images and try to render the report the report view crashes.

    Below is the Error and Stack Trace:

    URL: /api/Reports/clients/160626-95db/instances/160630-aeac/documents/161216-3e80161216-c106/info

    "message":"",
    "exceptionMessage":"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index",
    "exceptionType":"Telerik.Reporting.Services.Engine.DocumentRenderException",
    "stackTrace":" at Telerik.Reporting.Services.Engine.Document.GetDocumentInfo()\r\n at Telerik.Reporting.Services.Engine.ReportEngine.GetDocumentInfo(String clientID, String instanceID, String documentID)\r\n at Telerik.Reporting.Services.WebApi.ReportsControllerBase.GetDocumentInfo(String clientID, String instanceID, String documentID)\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()"

     

    Here is the report:

    namespace LT.Services.Reporting.EFV.Reports.Reports.ScannedImage
    {
        partial class rptScannedImage
        {
            #region Component Designer generated code
            /// <summary>
            /// Required method for telerik Reporting designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                Telerik.Reporting.Group group1 = new Telerik.Reporting.Group();
                Telerik.Reporting.ReportParameter reportParameter1 = new Telerik.Reporting.ReportParameter();
                Telerik.Reporting.ReportParameter reportParameter2 = new Telerik.Reporting.ReportParameter();
                Telerik.Reporting.ReportParameter reportParameter3 = new Telerik.Reporting.ReportParameter();
                Telerik.Reporting.ReportParameter reportParameter4 = new Telerik.Reporting.ReportParameter();
                Telerik.Reporting.ReportParameter reportParameter5 = new Telerik.Reporting.ReportParameter();
                Telerik.Reporting.ReportParameter reportParameter6 = new Telerik.Reporting.ReportParameter();
                Telerik.Reporting.Drawing.StyleRule styleRule1 = new Telerik.Reporting.Drawing.StyleRule();
                Telerik.Reporting.Drawing.StyleRule styleRule2 = new Telerik.Reporting.Drawing.StyleRule();
                Telerik.Reporting.Drawing.StyleRule styleRule3 = new Telerik.Reporting.Drawing.StyleRule();
                Telerik.Reporting.Drawing.StyleRule styleRule4 = new Telerik.Reporting.Drawing.StyleRule();
                this.groupFooterSection = new Telerik.Reporting.GroupFooterSection();
                this.groupHeaderSection = new Telerik.Reporting.GroupHeaderSection();
                this.detail = new Telerik.Reporting.DetailSection();
                this.pictureBox1 = new Telerik.Reporting.PictureBox();
                ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
                // 
                // groupFooterSection
                // 
                this.groupFooterSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.0520833320915699D);
                this.groupFooterSection.Name = "groupFooterSection";
                this.groupFooterSection.Style.Visible = false;
                // 
                // groupHeaderSection
                // 
                this.groupHeaderSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.0520833320915699D);
                this.groupHeaderSection.Name = "groupHeaderSection";
                this.groupHeaderSection.Style.Visible = false;
                // 
                // detail
                // 
                this.detail.Height = Telerik.Reporting.Drawing.Unit.Inch(1.0000008344650269D);
                this.detail.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {
                this.pictureBox1});
                this.detail.Name = "detail";
                this.detail.PageBreak = Telerik.Reporting.PageBreak.After;
                // 
                // pictureBox1
                // 
                this.pictureBox1.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(3.9418537198798731E-05D), Telerik.Reporting.Drawing.Unit.Inch(0D));
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(1.0000003576278687D), Telerik.Reporting.Drawing.Unit.Inch(1D));
                this.pictureBox1.Sizing = Telerik.Reporting.Drawing.ImageSizeMode.AutoSize;
                this.pictureBox1.Style.BorderStyle.Default = Telerik.Reporting.Drawing.BorderType.None;
                // 
                // rptScannedImage
                // 
                group1.GroupFooter = this.groupFooterSection;
                group1.GroupHeader = this.groupHeaderSection;
                group1.Name = "group";
                this.Groups.AddRange(new Telerik.Reporting.Group[] {
                group1});
                this.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {
                this.groupHeaderSection,
                this.groupFooterSection,
                this.detail});
                this.Name = "rptScannedImage";
                this.PageSettings.Margins = new Telerik.Reporting.Drawing.MarginsU(Telerik.Reporting.Drawing.Unit.Inch(0D), Telerik.Reporting.Drawing.Unit.Inch(0D), Telerik.Reporting.Drawing.Unit.Inch(0D), Telerik.Reporting.Drawing.Unit.Inch(0D));
                this.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Custom;
                this.PageSettings.PaperSize = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(8.5D), Telerik.Reporting.Drawing.Unit.Inch(11D));
                reportParameter1.Name = "PageNumber";
                reportParameter1.Type = Telerik.Reporting.ReportParameterType.Integer;
                reportParameter1.Value = "1";
                reportParameter2.Name = "ImageToAdd";
                reportParameter2.Value = "D:\\KDrive\\LT001\\LT001001\\001000003.tif";
                reportParameter3.Name = "ImageExists";
                reportParameter3.Type = Telerik.Reporting.ReportParameterType.Boolean;
                reportParameter3.Value = "= True";
                reportParameter4.Name = "RegistrationNumber";
                reportParameter4.Value = "000000000";
                reportParameter5.Name = "IsEmbeddedResource";
                reportParameter5.Value = "= False";
                reportParameter6.Name = "FileResourceName";
                reportParameter6.Value = "LT.Services.Reporting.EFV.Reports.Contents.Images.eSubmission_CoverPage.jpg";
                this.ReportParameters.Add(reportParameter1);
                this.ReportParameters.Add(reportParameter2);
                this.ReportParameters.Add(reportParameter3);
                this.ReportParameters.Add(reportParameter4);
                this.ReportParameters.Add(reportParameter5);
                this.ReportParameters.Add(reportParameter6);
                this.Style.BackgroundColor = System.Drawing.Color.White;
                styleRule1.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
                new Telerik.Reporting.Drawing.StyleSelector("Title")});
                styleRule1.Style.Color = System.Drawing.Color.Black;
                styleRule1.Style.Font.Bold = true;
                styleRule1.Style.Font.Italic = false;
                styleRule1.Style.Font.Name = "Tahoma";
                styleRule1.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(18D);
                styleRule1.Style.Font.Strikeout = false;
                styleRule1.Style.Font.Underline = false;
                styleRule2.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
                new Telerik.Reporting.Drawing.StyleSelector("Caption")});
                styleRule2.Style.Color = System.Drawing.Color.Black;
                styleRule2.Style.Font.Name = "Tahoma";
                styleRule2.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(10D);
                styleRule2.Style.VerticalAlign = Telerik.Reporting.Drawing.VerticalAlign.Middle;
                styleRule3.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
                new Telerik.Reporting.Drawing.StyleSelector("Data")});
                styleRule3.Style.Font.Name = "Tahoma";
                styleRule3.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(9D);
                styleRule3.Style.VerticalAlign = Telerik.Reporting.Drawing.VerticalAlign.Middle;
                styleRule4.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
                new Telerik.Reporting.Drawing.StyleSelector("PageInfo")});
                styleRule4.Style.Font.Name = "Tahoma";
                styleRule4.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(8D);
                styleRule4.Style.VerticalAlign = Telerik.Reporting.Drawing.VerticalAlign.Middle;
                this.StyleSheet.AddRange(new Telerik.Reporting.Drawing.StyleRule[] {
                styleRule1,
                styleRule2,
                styleRule3,
                styleRule4});
                this.Width = Telerik.Reporting.Drawing.Unit.Inch(1.0000393390655518D);
                this.ItemDataBinding += new System.EventHandler(this.rptScannedImage_ItemDataBinding);
                ((System.ComponentModel.ISupportInitialize)(this)).EndInit();

            }
            #endregion

            private Telerik.Reporting.DetailSection detail;
            private Telerik.Reporting.GroupHeaderSection groupHeaderSection;
            private Telerik.Reporting.GroupFooterSection groupFooterSection;
            private Telerik.Reporting.PictureBox pictureBox1;

        }
    }

    Here is the location I add the image to the report

    public partial class rptScannedImage : Telerik.Reporting.Report
        {
            private static readonly CacheHelper _cacheHelper = CacheHelper.Instance;
            private Object thisLock = new Object();


            public rptScannedImage()
            {
                //
                // Required for telerik Reporting designer support
                //
                InitializeComponent();
                //
                // TODO: Add any constructor code after InitializeComponent call
                //
            }
            private void rptScannedImage_ItemDataBinding(object sender, EventArgs e)
            {
                lock (thisLock)
                {
                    bool imageExists = (bool)this.ReportParameters["ImageExists"].Value;
                    int pageNumber = (int)this.ReportParameters["PageNumber"].Value;
                    string imageFile = (string)this.ReportParameters["ImageToAdd"].Value;
                    bool isEmbeddedResource = (bool)this.ReportParameters["IsEmbeddedResource"].Value;
                    string fileResourceName = (string)this.ReportParameters["FileResourceName"].Value;

                    if (!imageExists)
                    {
                        var txtNotAvailable = new Telerik.Reporting.TextBox();
                        txtNotAvailable.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(0.5D), Telerik.Reporting.Drawing.Unit.Inch(2.5D));
                        txtNotAvailable.Name = "txtNotAvailable";
                        txtNotAvailable.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(7.5D), Telerik.Reporting.Drawing.Unit.Inch(5.5));
                        txtNotAvailable.Style.Font.Name = "Times New Roman";
                        txtNotAvailable.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(72D);
                        txtNotAvailable.Style.TextAlign = Telerik.Reporting.Drawing.HorizontalAlign.Center;
                        txtNotAvailable.Value = "Dummy Registered Document Image for " + (string)this.ReportParameters["RegistrationNumber"].Value;
                        this.detail.Items.Add(txtNotAvailable);
                    }
                    else
                    {
                        Image image = null;
                        // if it's not an embedded resourece 
                        if (!isEmbeddedResource)
                        {
                            // we need to clone the image for thread safety
                            image = ReportDataMethod.GetImage(imageFile);
                        }
                        else
                        {
                            image = Reporting.EFV.Reports.ReportDataMethod.GetImageFromResource(fileResourceName);
                        }

                        double width = image.Width / image.HorizontalResolution;
                        double height = image.Height / image.VerticalResolution;
                        SizeU size = new SizeU(Unit.Inch(width), Unit.Inch(height));
                        Telerik.Reporting.Processing.Report rpt = (Telerik.Reporting.Processing.Report)sender;
                        rpt.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Custom;
                        rpt.PageSettings.PaperSize = size;

                        pictureBox1.MimeType = "image/png";
                        pictureBox1.Value = image;
                    }
                }
            }

     

    Here is the way I append the images in the resolver

        // Add File Attachment to report
                                // Attach image files as pages
    // Gets the paths
                                var pngImagePaths = ReportDataMethod.GetFilePaths(eFormID);
                                if (pngImagePaths != null)
                                {
                                    var pageCountID = 1;
                                    foreach (var imagePath in pngImagePaths)
                                    {
                                        // Add cover page (Eventually we will add this to all E-Forms
                                        // Add scanned file
                                        // note that setting filePath or File Resource Name to null will
                                        // cause the preview to crash on reload
                                        PDFToImageParameters fileParams = new PDFToImageParameters()
                                        {
                                            FilePath = imagePath,
                                            FileResourceName = "",
                                            ImageExists = true,
                                            IsEmbeddedResource = false,
                                            PageCount = pageCountID
                                        };
                                        var fileReport = UpdateScannedImageReportWithParams(eFormID, pageCountID == 1 ? "File Attachment" : String.Empty, pageCountID++, fileParams);
                                        book.Reports.Add(fileReport);
                                    }
                                }

     

    Any help would be appreciated.

     

    Thanks!

     

  2. Stef
    Admin
    Stef avatar
    3036 posts

    Posted 15 Feb Link to this post

    Hello Victor,

    The Reporting REST service's resolver is called multiple times on registering a client, instance and getting a rendered document. Then on each operation with the report as Refresh, preview with new set of parameters, print and export.

    In between these operations, the report definition must remain the same, changes in the document (ReportBook in the case) may result in the reported error. Note that rendered documents are cached and the service first looks for a report cached on the server before returning a newly rendered document.

    I can suggest you the following approach:
    1. Save the user selection for images on the server. For example get a list of paths to images on the server and save it in database with the current user's ID.

    2. Create a report with a single PictureBox item in its Detail section;
    3. Add a data source component consuming data from the location where you can save the user selection from step 1, and set it as report's DataSource. As a result there will be produced a Detail section per data record.
      Each data source component allows you to map the data-retrieval method's arguments to report parameters e.g. Using Parameters with the SqlDataSource component, if needed to filter data on retrieval;
    4. Set the PictureBox.Value via expression to the stored path to the image. If you need to modify additionally the image, the recommended approach is to use a custom function instead of using the report's events;

    5. If you want each image on a new page, set the Detail section's PageBreak property.


    I hope this information is helpful.

    Regards,
    Stef
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
Back to Top