scryber.core icon indicating copy to clipboard operation
scryber.core copied to clipboard

Is enumeration over collections using the <template /> tag working?

Open ManiSJ opened this issue 1 year ago • 2 comments

I have class A with property Items of type list of class B. class B has Name property.

C# code

 string workingDirectory = System.Environment.CurrentDirectory;
 var path = System.IO.Path.Combine(workingDirectory, "Resources\\Details.html");
 var output = System.IO.Path.GetFullPath(System.IO.Path.Combine("Report", "Pdf", "Details.pdf"));

   using (var doc = Document.ParseDocument(path))
   {
      doc.Params["model"] = new
      {
        allDetails = class A
      };

      using (var stream = new System.IO.FileStream(output, System.IO.FileMode.Create))
    {
       doc.SaveAsPDF(stream);
    }
  }

Resources\Details.html has(showing only tbody below)

    <tbody>
       <template data-bind='{{model.allDetails.Items}}'>
       <tr>
           <<td>{.Name}</td>
       </tr>
      </template>
   </tbody>

I am getting below error in browser console

500 Internal Server Error", error: "Scryber.PDFParserException: The XmlParser could not parse the source file 'D:\Projects\ProjectName\Resources\Details.html'. Could not read the value data-bind from the Xml source\r\n ---> Scryber.PDFParserException: Could not read the value data-bind from the Xml source\r\n ---> System.NullReferenceException: Object reference not set to an instance of an object.\r\n at Scryber.Generation.ParserAttributeDefinition.DoGetValue(XmlReader reader, PDFGeneratorSettings settings)\r\n at Scryber.Generation.ParserPropertyDefinition.GetValue(XmlReader reader, PDFGeneratorSettings settings)\r\n --- End of inner exception stack trace ---\r\n

ManiSJ avatar Apr 08 '24 07:04 ManiSJ

Hi @ManiSJ - binding over the collection should be fine. I did notice that there is an extra '<' in your table row, and only single parentheses inside the table cell...

    <tbody>
       <template data-bind='{{model.allDetails.Items}}'>
       <tr>
           <td>{{.Name}}</td>
       </tr>
      </template>
   </tbody>

This should work better, but if you are still facing issues feel free to comment more - a class structure would work well for me so I can test here.

Richard

richard-scryber avatar Apr 08 '24 14:04 richard-scryber

Hi @richard-scryber, Below is my full code

I am using Scryber.Core (5.0.7) package then my .cs file has using Scryber.Components;

C#

private void CreatePdfReport()
{
    // https://scrybercore.readthedocs.io/en/latest/1_overview/1_gui_controller_full.html
    // https://scrybercore.readthedocs.io/en/latest/1_overview/7_parameters_and_expressions.html

    string workingDirectory = System.Environment.CurrentDirectory;
    var path = System.IO.Path.Combine(workingDirectory, "Resources\\Details.html");
    var output = System.IO.Path.GetFullPath(System.IO.Path.Combine("Report", "Pdf", "Details.pdf"));

    using (var doc = Document.ParseDocument(path))
    {
        doc.Params["model"] = new 
        {
            Items = new[]
                {
                    new { name = "First item" },
                    new { name = "Second item" },
                    new { name = "Third item" },
                }
        };

        //And save it to a file or a stream
        using (var stream = new System.IO.FileStream(output, System.IO.FileMode.Create))
        {
            doc.SaveAsPDF(stream);
        }
    }
}

html (file properties: Set the Build Action to None and the Copy to output to Always.)

 <tbody>
     <template data-bind='{{model.Items}}'>
     </template>
 </tbody>

Having td or removing td inside template same error "Could not read the value data-bind from the Xml source\r\n ---> Scryber.PDFParserException: Could not read the value data-bind from the Xml source\r\n ---> System.NullReferenceException: Object reference not set to an instance of an object."

ManiSJ avatar Apr 09 '24 21:04 ManiSJ

@ManiSJ - Thanks for the info. v5.0.7 is quite old. If you upgrade to the latest package on Nuget (6.0.4-beta) at this time then you should have a much more pleasurable experience.

If you are still having issues after that, then deffo reach out.

richard-scryber avatar Apr 10 '24 13:04 richard-scryber

Hi @richard-scryber its working as expected. It seems I need to install Scryber.Core.OpenType package too.

ManiSJ avatar Apr 10 '24 22:04 ManiSJ

@ManiSJ - glad to hear it's working for you. Just for my information, did you upgrade the package, or keep with v5?

The open type package should be referenced and included as part of installation. So keen to understand. ☺️

richard-scryber avatar Apr 11 '24 13:04 richard-scryber

@richard-scryber I updated scryber.core to latest and installed open type package too.

ManiSJ avatar Apr 11 '24 13:04 ManiSJ

Ok, happy days! Any other problems or suggestions feel free 👍

richard-scryber avatar Apr 11 '24 13:04 richard-scryber