-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCreatePdfReport.csx
More file actions
141 lines (134 loc) · 4.9 KB
/
CreatePdfReport.csx
File metadata and controls
141 lines (134 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#load "ViewModels.csx"
#r "System.Drawing"
using iTextSharp;
using iTextSharp.text;
using PdfRpt.Core.Contracts;
using PdfRpt.Core.Helper;
using PdfRpt.FluentInterface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using OfficeOpenXml;
public static byte[] CreatePdfReport(OrderViewModel order, string fileName, TraceWriter log)
{
var pdfReportData = new PdfReport().DocumentPreferences(doc =>
{
log.Info("PDF started...");
log.Info($"OrderId = {order.OrderId}");
doc.RunDirection(PdfRunDirection.LeftToRight);
doc.Orientation(PageOrientation.Portrait);
doc.PageSize(PdfPageSize.A4);
doc.DocumentMetadata(new DocumentMetadata { Author = "Contoso Finance", Application = "ContosoFinance.Apps", Subject = "Contoso Finance Application", Title = "Application" });
doc.Compression(new CompressionSettings
{
EnableCompression = true,
EnableFullCompression = true
});
})
.DefaultFonts(fonts =>
{
fonts.Path(System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\arial.ttf"),
System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\verdana.ttf"));
fonts.Size(9);
fonts.Color(System.Drawing.Color.Black);
})
.PagesFooter(footer =>
{
footer.DefaultFooter(DateTime.Now.ToString("MM/dd/yyyy"));
})
.PagesHeader(header =>
{
header.CacheHeader(cache: true); // It's a default setting to improve the performance.
header.DefaultHeader(defaultHeader =>
{
defaultHeader.RunDirection(PdfRunDirection.LeftToRight);
// defaultHeader.ImagePath(GetImage("logo.png"));
defaultHeader.Message("Contoso Finance - Application");
log.Info("in header...");
});
})
.MainTableTemplate(template =>
{
template.BasicTemplate(BasicTemplate.ClassicTemplate);
})
.MainTablePreferences(table =>
{
table.ColumnsWidthsType(TableColumnWidthType.Relative);
})
.MainTableDataSource(dataSource =>
{
dataSource.StronglyTypedList(order.OrderDetails);
})
.MainTableSummarySettings(summarySettings =>
{
summarySettings.OverallSummarySettings("Summary");
summarySettings.PreviousPageSummarySettings("Previous Page Summary");
summarySettings.PageSummarySettings("Page Summary");
})
.MainTableColumns(columns =>
{
columns.AddColumn(column =>
{
column.PropertyName<OrderDetailViewModel>(o => o.ProductName);
column.CellsHorizontalAlignment(HorizontalAlignment.Left);
column.IsVisible(true);
column.Order(0);
column.Width(4);
column.HeaderCell("Product");
});
columns.AddColumn(column =>
{
column.PropertyName<OrderDetailViewModel>(o => o.Rate);
column.CellsHorizontalAlignment(HorizontalAlignment.Right);
column.IsVisible(true);
column.Order(2);
column.Width(2);
column.HeaderCell("Rate");
column.ColumnItemsTemplate(template =>
{
template.TextBlock();
template.DisplayFormatFormula(obj => obj == null || string.IsNullOrEmpty(obj.ToString())
? string.Empty : obj.ToString());
});
});
})
.MainTableEvents(events =>
{
events.DataSourceIsEmpty(message: "There are no products to display.");
events.MainTableAdded(args =>
{
var summaryTable = new PdfGrid(args.Table.RelativeWidths); // Create a clone of the MainTable's structure
summaryTable.WidthPercentage = args.Table.WidthPercentage;
summaryTable.SpacingBefore = args.Table.SpacingBefore;
/*
summaryTable.AddSimpleRow(
null, null,
(data, cellProperties) =>
{
data.Value = "Total";
cellProperties.PdfFont = args.PdfFont;
cellProperties.HorizontalAlignment = HorizontalAlignment.Right;
},
(data, cellProperties) =>
{
data.Value = string.Format("{0:c}", total);
cellProperties.PdfFont = args.PdfFont;
cellProperties.BorderColor = BaseColor.LIGHT_GRAY;
cellProperties.ShowBorder = true;
});
*/
args.PdfDoc.Add(summaryTable);
});
})
.Export(export =>
{
export.ToExcel();
})
.Generate(data => data.AsPdfStream(new MemoryStream()));
log.Info("PDF streamed...");
return ((MemoryStream)pdfReportData.PdfStreamOutput).ToArray();
}