Permalink
Browse files

Try using a helper method defined in TemplateBase for Data Images

- this is probably preferred way to use helpers (inline still useful for
  some customization scenarios)
- should consider adding a RazorReport base template with some useful
  helpers defined (images would be useful for sure)
- use image in example report (needs IE9 to display properly in winfoms
  app because of limiations with data: uris in older versions)
  • Loading branch information...
1 parent bb44ba6 commit 6ea28e6f26426a0d2337c147ffc93772d73ee817 @AlexCuse committed Jul 5, 2012
@@ -5,5 +5,6 @@ public class Example {
public string Name { get; set; }
public string Email { get; set; }
public Dictionary<object, object> Values { get; set; }
+ public byte[] Image { get; set; }
}
}
@@ -1,4 +1,4 @@
-@Raw(DocType)
+@DocType
<html>
<head>
@@STYLES
@@ -21,5 +21,7 @@
</tr>
}
</table>
+ <p>Here is a cat:</p>
+ @DataImage(Model.Image)
</body>
</html>
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
+using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
@@ -30,19 +31,28 @@ public partial class Form1 : Form {
string RunCompiled () {
- var model = new Example { Name = "Alex", Email = "test@example.com", Values = new Dictionary<object, object> { { "Compiled", "Yes" }, { "Worked", "Yes" } } };
+ var model = new Example { Name = "Alex", Email = "test@example.com", Values = new Dictionary<object, object> { { "Compiled", "Yes" }, { "Worked", "Yes" } }, Image = GetTestImage () };
return precompilingBuilder.BuildReport (model);
}
+ byte[] GetTestImage () {
+ string filename = "testImage.png";
+ var size = (int)new FileInfo (filename).Length;
+ using (var stream = File.Open (filename, FileMode.Open))
+ using (var reader = new BinaryReader (stream)) {
+ return reader.ReadBytes (size);
+ }
+ }
+
string Run () {
- var model = new Example { Name = "Alex", Email = "test@example.com", Values = new Dictionary<object, object> { { "Compiled", "No" }, { "Worked", "Yes" } } };
+ var model = new Example { Name = "Alex", Email = "test@example.com", Values = new Dictionary<object, object> { { "Compiled", "No" }, { "Worked", "Yes" } }, Image = GetTestImage () };
return builder.BuildReport (model);
}
byte[] RunPdf () {
- var model = new Example { Name = "Alex", Email = "test@example.com", Values = new Dictionary<object, object> { { "Compiled", "No" }, { "Worked", "Yes" } } };
+ var model = new Example { Name = "Alex", Email = "test@example.com", Values = new Dictionary<object, object> { { "Compiled", "No" }, { "Worked", "Yes" } }, Image = GetTestImage() };
return builder.BuildPdf (model);
}
@@ -103,6 +103,11 @@
<ItemGroup>
<EmbeddedResource Include="Style.css" />
</ItemGroup>
+ <ItemGroup>
+ <Content Include="testImage.png">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
+ </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
@@ -1,10 +1,17 @@
using System;
using RazorEngine.Templating;
+using RazorEngine.Text;
namespace RazorReport.Example {
public class RazorTemplateBase<T> : TemplateBase<T> {
- public String DocType {
- get { return @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">"; }
+ public IEncodedString DocType {
+ get { return Raw(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">"); }
+ }
+
+ public IEncodedString DataImage(byte[] image, string altText = "image") {
+ return
+ Raw(string.Format(
+ @"<img src=""data:image/png;base64,{0}"" alt=""{1}"" />", Convert.ToBase64String (image, Base64FormattingOptions.None), altText));
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6ea28e6

Please sign in to comment.