PrintService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using FastReport.Export.Html;
  2. using FastReport.Web.Infrastructure;
  3. #if !OPENSOURCE
  4. using FastReport.Export.Pdf;
  5. #endif
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Text;
  10. namespace FastReport.Web.Services
  11. {
  12. internal sealed class PrintService : IPrintService
  13. {
  14. public byte[] PrintReport(WebReport webReport, string printMode)
  15. {
  16. switch (printMode)
  17. {
  18. case "html":
  19. return PrintHtml(webReport);
  20. #if !OPENSOURCE
  21. case "pdf":
  22. return PrintPdf(webReport);
  23. #endif
  24. default:
  25. return null;
  26. }
  27. }
  28. private byte[] PrintHtml(WebReport webReport)
  29. {
  30. webReport.PictureCache.Clear();
  31. using (var htmlExport = new HTMLExport())
  32. {
  33. htmlExport.OpenAfterExport = false;
  34. htmlExport.Navigator = false;
  35. htmlExport.Layers = webReport.Layers;
  36. htmlExport.SinglePage = true;
  37. htmlExport.Pictures = webReport.Pictures;
  38. htmlExport.Print = true;
  39. htmlExport.Preview = true;
  40. htmlExport.SubFolder = false;
  41. htmlExport.EmbedPictures = webReport.EmbedPictures;
  42. htmlExport.EnableMargins = webReport.EnableMargins;
  43. //htmlExport.WebImagePrefix = WebUtils.ToUrl(FastReportGlobal.FastReportOptions.RouteBasePath, controller.RouteBasePath, ID, "picture") + "/";
  44. htmlExport.WebImagePrefix = WebUtils.ToUrl(FastReportGlobal.FastReportOptions.RoutePathBaseRoot, FastReportGlobal.FastReportOptions.RouteBasePath, $"preview.getPicture?reportId={webReport.ID}&pictureId=");
  45. htmlExport.ExportMode = HTMLExport.ExportType.WebPrint;
  46. byte[] file = null;
  47. using (MemoryStream ms = new MemoryStream())
  48. {
  49. htmlExport.Export(webReport.Report, ms);
  50. file = ms.ToArray();
  51. }
  52. if (htmlExport.PrintPageData != null)
  53. {
  54. //WebReportCache cache = new WebReportCache(this.Context);
  55. // add all pictures in cache
  56. for (int i = 0; i < htmlExport.PrintPageData.Pictures.Count; i++)
  57. {
  58. Stream stream = htmlExport.PrintPageData.Pictures[i];
  59. byte[] image = new byte[stream.Length];
  60. stream.Position = 0;
  61. int n = stream.Read(image, 0, (int)stream.Length);
  62. string picGuid = htmlExport.PrintPageData.Guids[i];
  63. //cache.PutObject(picGuid, image);
  64. webReport.PictureCache[picGuid] = image;
  65. }
  66. // cleanup
  67. for (int i = 0; i < htmlExport.PrintPageData.Pictures.Count; i++)
  68. {
  69. Stream stream = htmlExport.PrintPageData.Pictures[i];
  70. stream.Dispose();
  71. stream = null;
  72. }
  73. htmlExport.PrintPageData.Pictures.Clear();
  74. htmlExport.PrintPageData.Guids.Clear();
  75. }
  76. return file;
  77. }
  78. }
  79. #if !OPENSOURCE
  80. private byte[] PrintPdf(WebReport webReport)
  81. {
  82. using (var pdfExport = new PDFExport())
  83. {
  84. pdfExport.OpenAfterExport = false;
  85. //pdfExport.EmbeddingFonts = PdfEmbeddingFonts;
  86. //pdfExport.TextInCurves = PdfTextInCurves;
  87. //pdfExport.Background = PdfBackground;
  88. //pdfExport.PrintOptimized = PdfPrintOptimized;
  89. //pdfExport.Title = PdfTitle;
  90. //pdfExport.Author = PdfAuthor;
  91. //pdfExport.Subject = PdfSubject;
  92. //pdfExport.Keywords = PdfKeywords;
  93. //pdfExport.Creator = PdfCreator;
  94. //pdfExport.Producer = PdfProducer;
  95. //pdfExport.Outline = PdfOutline;
  96. //pdfExport.DisplayDocTitle = PdfDisplayDocTitle;
  97. //pdfExport.HideToolbar = PdfHideToolbar;
  98. //pdfExport.HideMenubar = PdfHideMenubar;
  99. //pdfExport.HideWindowUI = PdfHideWindowUI;
  100. //pdfExport.FitWindow = PdfFitWindow;
  101. //pdfExport.CenterWindow = PdfCenterWindow;
  102. //pdfExport.PrintScaling = PdfPrintScaling;
  103. //pdfExport.UserPassword = PdfUserPassword;
  104. //pdfExport.OwnerPassword = PdfOwnerPassword;
  105. //pdfExport.AllowPrint = PdfAllowPrint;
  106. //pdfExport.AllowCopy = PdfAllowCopy;
  107. //pdfExport.AllowModify = PdfAllowModify;
  108. //pdfExport.AllowAnnotate = PdfAllowAnnotate;
  109. //pdfExport.PdfCompliance = PdfA ? PDFExport.PdfStandard.PdfA_2a : PDFExport.PdfStandard.None;
  110. pdfExport.ShowPrintDialog = true;
  111. pdfExport.ExportMode = PDFExport.ExportType.WebPrint;
  112. using (MemoryStream ms = new MemoryStream())
  113. {
  114. pdfExport.Export(webReport.Report, ms);
  115. return ms.ToArray();
  116. }
  117. }
  118. }
  119. #endif
  120. }
  121. }