As a report developer, you are required to represent the information of the customers and products with the following key requirements:
Report must show customers’ picture and name.
It must associate the customers with their products.
Report must be generated in the HTML Document.
Adding Syntax to be evaluated by GroupDocs.Assembly Engine
An HTML report cannot hold a picture file, so the template embeds each customer’s photo in an img element as Base64-encoded data. The template is an HTML file whose tags are written with the angle brackets escaped, << and >>, so that the HTML stays valid:
The .NET template computes the Base64 text in the template, with a C# helper class registered as a known type (FileUtil.GetBytesAsBase64(Photo)). In Python, the code computes it while loading the data and gives each customer a PhotoBase64 member, so the template only outputs it.
Each customer’s Photo is the file name of its picture. The code reads each picture and stores its Base64 text in the customer’s dict.
importbase64importjsonfromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblerwithopen("customers.json",encoding="utf-8")asf:customers=json.load(f)# An HTML report embeds each photo as Base64 textforcustomerincustomers:withopen(customer["Photo"],"rb")asphoto:customer["PhotoBase64"]=base64.b64encode(photo.read()).decode("ascii")assembler=DocumentAssembler()assembler.assemble_document("common-master-detail-photos.html","common-master-detail-photos-report.html",DataSourceInfo(customers,"customers"))
The template can also do the encoding, as the .NET one does. Pass each photo as bytes, which reaches the template as a byte array, and make the engine aware of the .NET System.Convert class, whose ToBase64String method encodes it. Only the expression in the img element differs from the template above:
Without the conversion, a byte array is written as its type name, System.Byte[].
importjsonfromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblerwithopen("customers.json",encoding="utf-8")asf:customers=json.load(f)# Each photo goes to the template as bytesforcustomerincustomers:withopen(customer["Photo"],"rb")asphoto:customer["PhotoBytes"]=photo.read()assembler=DocumentAssembler()# The template calls Convert.ToBase64Stringassembler.known_types.add("System.Convert")assembler.assemble_document("common-master-detail-photo-bytes.html","common-master-detail-photo-bytes-report.html",DataSourceInfo(customers,"customers"))