WebReportHtml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using FastReport.Export.Html;
  2. using FastReport.Preview;
  3. using FastReport.Table;
  4. using System;
  5. using System.IO;
  6. using System.Net;
  7. using System.Text;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Globalization;
  11. #if !OPENSOURCE
  12. using FastReport.AdvMatrix;
  13. #endif
  14. namespace FastReport.Web
  15. {
  16. public partial class WebReport
  17. {
  18. #region Internal Methods
  19. internal void ExportReport(Stream stream, string exportFormat, IEnumerable<KeyValuePair<string, string>> exportParameters)
  20. {
  21. var exportInfo = ExportsHelper.GetInfoFromExt(exportFormat);
  22. ExportReport(stream, exportInfo, exportParameters);
  23. }
  24. internal void ExportReport(Stream stream, ExportsHelper.ExportInfo exportInfo, IEnumerable<KeyValuePair<string, string>> exportParameters)
  25. {
  26. using (var export = exportInfo.CreateExport())
  27. {
  28. if (exportInfo.HaveSettings && exportParameters.Any())
  29. {
  30. var availableProperties = exportInfo.Properties;
  31. if (availableProperties != null)
  32. foreach (var exportParameter in exportParameters)
  33. {
  34. var existProp = availableProperties
  35. .Where(prop => prop.Name == exportParameter.Key)
  36. .FirstOrDefault();
  37. if (existProp != null)
  38. {
  39. string propValue = exportParameter.Value;
  40. object propValueConverted;
  41. if (existProp.PropertyType.IsEnum)
  42. propValueConverted = Enum.Parse(existProp.PropertyType, propValue);
  43. else
  44. propValueConverted = Convert.ChangeType(propValue, existProp.PropertyType, CultureInfo.InvariantCulture);
  45. System.Diagnostics.Debug.WriteLine($"Export setting: {existProp}: {propValueConverted}");
  46. existProp.SetMethod.Invoke(export, new[] { propValueConverted });
  47. }
  48. }
  49. }
  50. if (exportInfo.Export == Exports.Prepared)
  51. Report.SavePrepared(stream);
  52. else if (export != null)
  53. export.Export(Report, stream);
  54. else
  55. throw new UnsupportedExportException();
  56. stream.Position = 0;
  57. }
  58. }
  59. #endregion
  60. #region Private Methods
  61. private void Refresh(int pageN, ReportPage page)
  62. {
  63. if (Report.NeedRefresh)
  64. Report.InteractiveRefresh();
  65. else
  66. Report.PreparedPages.ModifyPage(pageN, page);
  67. }
  68. private void GoToPageNumber(string @goto)
  69. {
  70. switch (@goto)
  71. {
  72. case "first":
  73. FirstPage();
  74. break;
  75. case "last":
  76. LastPage();
  77. break;
  78. case "prev":
  79. PrevPage();
  80. break;
  81. case "next":
  82. NextPage();
  83. break;
  84. default:
  85. if (int.TryParse(@goto, out int value))
  86. GotoPage(value - 1);
  87. break;
  88. }
  89. }
  90. private int PageNByBookmark(string bookmark)
  91. {
  92. int pageN = -1;
  93. if (Report.PreparedPages != null)
  94. {
  95. for (int i = 0; i < Report.PreparedPages.Count; i++)
  96. {
  97. ReportPage page = Report.PreparedPages.GetPage(i);
  98. if (page != null)
  99. {
  100. ObjectCollection allObjects = page.AllObjects;
  101. for (int j = 0; j < allObjects.Count; j++)
  102. {
  103. ReportComponentBase c = allObjects[j] as ReportComponentBase;
  104. if (c.Bookmark == bookmark)
  105. {
  106. pageN = i;
  107. break;
  108. }
  109. }
  110. page.Dispose();
  111. if (pageN != -1)
  112. break;
  113. }
  114. }
  115. }
  116. return pageN;
  117. }
  118. private void DoClickAdvancedMatrixByParamID(string objectName, int pageN, int index)
  119. {
  120. #if !OPENSOURCE
  121. if (Report.PreparedPages != null)
  122. {
  123. bool found = false;
  124. while (pageN < Report.PreparedPages.Count && !found)
  125. {
  126. ReportPage page = Report.PreparedPages.GetPage(pageN);
  127. if (page != null)
  128. {
  129. ObjectCollection allObjects = page.AllObjects;
  130. foreach (Base obj in allObjects)
  131. {
  132. if (obj is MatrixCollapseButton collapseButton)
  133. {
  134. if (collapseButton.Name == objectName
  135. && collapseButton.Index == index)
  136. {
  137. collapseButton.MatrixCollapseButtonClick();
  138. Refresh(pageN, page);
  139. found = true;
  140. break;
  141. }
  142. }
  143. else if(obj is MatrixSortButton sortButton)
  144. {
  145. if(sortButton.Name == objectName
  146. && sortButton.Index == index)
  147. {
  148. sortButton.MatrixSortButtonClick();
  149. Refresh(pageN, page);
  150. found = true;
  151. break;
  152. }
  153. }
  154. }
  155. page.Dispose();
  156. pageN++;
  157. }
  158. }
  159. }
  160. #endif
  161. }
  162. private void Click(ReportComponentBase c, ReportPage page, int pageN)
  163. {
  164. c.OnClick(null);
  165. Refresh(pageN, page);
  166. }
  167. private void CheckboxClick(CheckBoxObject checkBox, ReportPage page, int pageN)
  168. {
  169. checkBox.Checked = !checkBox.Checked;
  170. Refresh(pageN, page);
  171. }
  172. private void DoDetailedReport(ReportComponentBase obj, string value)
  173. {
  174. if (obj != null)
  175. {
  176. string fileName = obj.Hyperlink.DetailReportName;
  177. if (File.Exists(fileName))
  178. {
  179. Report tabReport = new Report();
  180. tabReport.Load(fileName);
  181. string paramName = obj.Hyperlink.ReportParameter;
  182. string paramValue = string.IsNullOrEmpty(obj.Hyperlink.Value) ? value : obj.Hyperlink.Value;
  183. Data.Parameter param = tabReport.Parameters.FindByName(paramName);
  184. if (param != null && param.ChildObjects.Count > 0)
  185. {
  186. string[] paramValues = paramValue.Split(obj.Hyperlink.ValuesSeparator[0]);
  187. if (paramValues.Length > 0)
  188. {
  189. int i = 0;
  190. foreach (Data.Parameter childParam in param.ChildObjects)
  191. {
  192. childParam.Value = paramValues[i++];
  193. if (i >= paramValues.Length)
  194. break;
  195. }
  196. }
  197. }
  198. else
  199. tabReport.SetParameterValue(paramName, paramValue);
  200. Report.Dictionary.ReRegisterData(tabReport.Dictionary);
  201. tabReport.PreparePhase1();
  202. tabReport.PreparePhase2();
  203. Tabs.Add(new ReportTab()
  204. {
  205. Name = paramValue,
  206. Report = tabReport,
  207. Closeable = true,
  208. NeedParent = false
  209. });
  210. CurrentTabIndex = Tabs.Count - 1;
  211. }
  212. }
  213. }
  214. internal void DoHtmlPage(StringBuilder sb, HTMLExport html, int pageN)
  215. {
  216. if (html.PreparedPages[pageN].PageText == null)
  217. {
  218. html.PageNumbers = (pageN + 1).ToString();
  219. html.Export(Report, (Stream)null);
  220. }
  221. //Prop.CurrentWidth = html.PreparedPages[pageN].Width;
  222. //Prop.CurrentHeight = html.PreparedPages[pageN].Height;
  223. if (html.PreparedPages[pageN].CSSText != null &&
  224. html.PreparedPages[pageN].PageText != null)
  225. {
  226. sb.Append(html.PreparedPages[pageN].CSSText);
  227. sb.Append(html.PreparedPages[pageN].PageText);
  228. if (!html.EmbedPictures)
  229. CacheHtmlPictures(html, pageN);
  230. }
  231. }
  232. void DoAllHtmlPages(StringBuilder sb, HTMLExport html)
  233. {
  234. //Prop.CurrentWidth = 0;
  235. //Prop.CurrentHeight = 0;
  236. for (int pageN = 0; pageN < html.PreparedPages.Count; pageN++)
  237. {
  238. if (html.PreparedPages[pageN].PageText == null)
  239. {
  240. html.PageNumbers = (pageN + 1).ToString();
  241. html.Export(Report, (Stream)null);
  242. //if (html.PreparedPages[pageN].Width > Prop.CurrentWidth)
  243. // Prop.CurrentWidth = html.PreparedPages[pageN].Width;
  244. //if (html.PreparedPages[pageN].Height > Prop.CurrentHeight)
  245. // Prop.CurrentHeight = html.PreparedPages[pageN].Height;
  246. }
  247. if (html.PreparedPages[pageN].CSSText != null &&
  248. html.PreparedPages[pageN].PageText != null)
  249. {
  250. sb.Append(html.PreparedPages[pageN].CSSText);
  251. sb.Append(html.PreparedPages[pageN].PageText);
  252. if (!EmbedPictures)
  253. CacheHtmlPictures(html, Layers ? 0 : pageN);
  254. }
  255. }
  256. }
  257. void CacheHtmlPictures(HTMLExport html, int pageN)
  258. {
  259. //WebReportCache cache = new WebReportCache(this.Context);
  260. for (int i = 0; i < html.PreparedPages[pageN].Pictures.Count; i++)
  261. {
  262. try
  263. {
  264. Stream picStream = html.PreparedPages[pageN].Pictures[i];
  265. byte[] image = new byte[picStream.Length];
  266. picStream.Position = 0;
  267. int n = picStream.Read(image, 0, (int)picStream.Length);
  268. string guid = html.PreparedPages[pageN].Guids[i];
  269. //cache.PutObject(guid, image);
  270. PictureCache[guid] = image;
  271. }
  272. catch
  273. {
  274. //Log.AppendFormat("Error with picture: {0}\n", i.ToString());
  275. }
  276. }
  277. }
  278. internal float GetReportPageWidthInPixels()
  279. {
  280. float _pageWidth = 0;
  281. if (SinglePage)
  282. {
  283. foreach (PageBase page in Report.Pages)
  284. {
  285. // find maxWidth
  286. if (page is ReportPage)
  287. {
  288. var _page = page as ReportPage;
  289. if (_page.WidthInPixels > _pageWidth)
  290. _pageWidth = _page.WidthInPixels;
  291. }
  292. }
  293. }
  294. else
  295. {
  296. _pageWidth = Report.PreparedPages.GetPageSize(CurrentPageIndex).Width;
  297. }
  298. return _pageWidth;
  299. }
  300. internal float GetReportPageHeightInPixels()
  301. {
  302. float _pageHeight = 0;
  303. if (SinglePage)
  304. {
  305. for (int i = 0; i < Report.PreparedPages.Count; i++)
  306. {
  307. _pageHeight += Report.PreparedPages.GetPageSize(i).Height;
  308. }
  309. }
  310. else
  311. {
  312. _pageHeight = Report.PreparedPages.GetPageSize(CurrentPageIndex).Height;
  313. }
  314. return _pageHeight;
  315. }
  316. #endregion
  317. }
  318. }