GenerateExtensions.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using QuestPDF.Drawing;
  7. using QuestPDF.Helpers;
  8. using QuestPDF.Infrastructure;
  9. using QuestPDF.Skia;
  10. namespace QuestPDF.Fluent
  11. {
  12. public static class GenerateExtensions
  13. {
  14. static GenerateExtensions()
  15. {
  16. ClearGenerateAndShowFiles();
  17. }
  18. internal static void GenerateAndDiscard(this IDocument document)
  19. {
  20. DocumentGenerator.GenerateAndDiscard(document);
  21. }
  22. #region Genearate And Show Configuration
  23. private static readonly Random Random = new();
  24. private const string GenerateAndShowNamePrefix = "QuestPDF Preview";
  25. private static void ClearGenerateAndShowFiles()
  26. {
  27. var legacyPreviewFiles = Directory
  28. .GetFiles(TemporaryStorage.GetPath(), $"{GenerateAndShowNamePrefix} *")
  29. .Where(x => DateTime.UtcNow - new FileInfo(x).LastAccessTimeUtc > TimeSpan.FromHours(1));
  30. foreach (var legacyPreviewFile in legacyPreviewFiles)
  31. {
  32. try
  33. {
  34. if (File.Exists(legacyPreviewFile))
  35. File.Delete(legacyPreviewFile);
  36. }
  37. catch
  38. {
  39. // ignored
  40. }
  41. }
  42. }
  43. #endregion
  44. #region PDF
  45. /// <summary>
  46. /// Generates the document in PDF format and returns it as a byte array.
  47. /// </summary>
  48. public static byte[] GeneratePdf(this IDocument document)
  49. {
  50. using var memoryStream = new MemoryStream();
  51. document.GeneratePdf(memoryStream);
  52. return memoryStream.ToArray();
  53. }
  54. /// <summary>
  55. /// Generates the document in PDF format and saves it to the specified file path.
  56. /// </summary>
  57. public static void GeneratePdf(this IDocument document, string filePath)
  58. {
  59. if (File.Exists(filePath))
  60. File.Delete(filePath);
  61. using var fileStream = File.Create(filePath);
  62. document.GeneratePdf(fileStream);
  63. }
  64. /// <summary>
  65. /// Generates the document in PDF format and outputs it to a provided stream.
  66. /// </summary>
  67. public static void GeneratePdf(this IDocument document, Stream stream)
  68. {
  69. using var skiaStream = new SkWriteStream(stream);
  70. DocumentGenerator.GeneratePdf(skiaStream, document);
  71. skiaStream.Flush();
  72. }
  73. /// <summary>
  74. /// Generates the document in PDF format, saves it in temporary file, and then opens it with the default application.
  75. /// </summary>
  76. public static void GeneratePdfAndShow(this IDocument document)
  77. {
  78. var filePath = Path.Combine(TemporaryStorage.GetPath(), $"{GenerateAndShowNamePrefix} {Random.Next()}.pdf");
  79. document.GeneratePdf(filePath);
  80. Helpers.Helpers.OpenFileUsingDefaultProgram(filePath);
  81. }
  82. #endregion
  83. #region XPS
  84. /// <summary>
  85. /// Generates the document in XPS format and returns it as a byte array.
  86. /// </summary>
  87. /// <remarks>
  88. /// Supported only on the Windows platform.
  89. /// </remarks>
  90. public static byte[] GenerateXps(this IDocument document)
  91. {
  92. using var memoryStream = new MemoryStream();
  93. document.GenerateXps(memoryStream);
  94. return memoryStream.ToArray();
  95. }
  96. /// <summary>
  97. /// Generates the document in XPS format and saves it to the specified file path.
  98. /// </summary>
  99. /// <remarks>
  100. /// Supported only on the Windows platform.
  101. /// </remarks>
  102. public static void GenerateXps(this IDocument document, string filePath)
  103. {
  104. if (File.Exists(filePath))
  105. File.Delete(filePath);
  106. using var fileStream = File.Create(filePath);
  107. document.GenerateXps(fileStream);
  108. }
  109. /// <summary>
  110. /// Generates the document in XPS format and outputs it to a provided stream.
  111. /// </summary>
  112. /// <remarks>
  113. /// Supported only on the Windows platform.
  114. /// </remarks>
  115. public static void GenerateXps(this IDocument document, Stream stream)
  116. {
  117. if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  118. throw new PlatformNotSupportedException("XPS generation is only supported on the Windows platform.");
  119. using var skiaStream = new SkWriteStream(stream);
  120. DocumentGenerator.GenerateXps(skiaStream, document);
  121. skiaStream.Flush();
  122. }
  123. /// <summary>
  124. /// Generates the document in XPS format, saves it in temporary file, and then opens it with the default application.
  125. /// </summary>
  126. /// <remarks>
  127. /// Supported only on the Windows platform.
  128. /// </remarks>
  129. public static void GenerateXpsAndShow(this IDocument document)
  130. {
  131. var filePath = Path.Combine(TemporaryStorage.GetPath(), $"{GenerateAndShowNamePrefix} {Random.Next()}.xps");
  132. document.GenerateXps(filePath);
  133. Helpers.Helpers.OpenFileUsingDefaultProgram(filePath);
  134. }
  135. #endregion
  136. #region Images
  137. /// <summary>
  138. /// Generates the document as a series of images and returns them as a collection of byte arrays.
  139. /// </summary>
  140. /// <param name="settings">Optional settings to customize the generation process, such as image resolution, compression ratio, and more.</param>
  141. public static IEnumerable<byte[]> GenerateImages(this IDocument document, ImageGenerationSettings? settings = null)
  142. {
  143. settings ??= ImageGenerationSettings.Default;
  144. return DocumentGenerator.GenerateImages(document, settings);
  145. }
  146. /// <param name="imageIndex">Specifies the index of the generated image from the document, starting at 0.</param>
  147. /// <returns>The file path where the image should be saved.</returns>
  148. public delegate string GenerateDocumentImagePath(int imageIndex);
  149. /// <summary>
  150. /// Generates the document as a sequence of images, saving them to paths determined by the <paramref name="imagePathSource"/> delegate.
  151. /// </summary>
  152. /// <param name="imagePathSource">A delegate that gets image index as an input, and returns file path where it should be saved.</param>
  153. /// <param name="settings">Optional settings for fine-tuning the generated images, such as resolution, compression ratio, etc.</param>
  154. public static void GenerateImages(this IDocument document, GenerateDocumentImagePath imagePathSource, ImageGenerationSettings? settings = null)
  155. {
  156. settings ??= ImageGenerationSettings.Default;
  157. var index = 0;
  158. foreach (var imageData in document.GenerateImages(settings))
  159. {
  160. var path = imagePathSource(index);
  161. if (File.Exists(path))
  162. File.Delete(path);
  163. File.WriteAllBytes(path, imageData);
  164. index++;
  165. }
  166. }
  167. #endregion
  168. #region SVG
  169. /// <summary>
  170. /// Generates the document as a series of SVG images and returns them as a collection of strings.
  171. /// </summary>
  172. public static ICollection<string> GenerateSvg(this IDocument document)
  173. {
  174. return DocumentGenerator.GenerateSvg(document);
  175. }
  176. #endregion
  177. }
  178. }