Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PdfGenerator.cs 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System.Data.Common;
  2. using BlackRock.Reporting.API.Core;
  3. using BlackRock.Reporting.API.Core.Models;
  4. using iTextSharp.text;
  5. using iTextSharp.text.pdf;
  6. using PuppeteerSharp;
  7. using iTextSharp;
  8. using iTextSharp.text.xml;
  9. namespace BlackRock.Reporting.API.Persistence
  10. {
  11. public class PdfGenerator : IPdfGenerator
  12. {
  13. private readonly IHostEnvironment host;
  14. public PdfGenerator(IHostEnvironment host)
  15. {
  16. this.host = host;
  17. }
  18. public async Task Generate(string url, string path, PdfOptions options)
  19. {
  20. await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
  21. var opts = new LaunchOptions
  22. {
  23. Headless = true
  24. };
  25. using (var browser = await Puppeteer.LaunchAsync(opts))
  26. using (var page = await browser.NewPageAsync())
  27. {
  28. await page.GoToAsync(url, WaitUntilNavigation.DOMContentLoaded);
  29. // await modifyPage(page,param);
  30. var allResultsSelector = ".chartjs-render-monitor";
  31. await page.WaitForSelectorAsync(allResultsSelector);
  32. await EvaluateScript(page, "dist/main.js");
  33. await page.PdfAsync(path, options
  34. // new PdfOptions{
  35. // Landscape = true,
  36. // PrintBackground = true,
  37. // Format = PuppeteerSharp.Media.PaperFormat.Letter
  38. // }
  39. );
  40. }
  41. // POST header image
  42. // ModifyPdf(path);
  43. }
  44. public async Task Isolate(string url, string path, PdfOptions options)
  45. {
  46. await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
  47. var opts = new LaunchOptions
  48. {
  49. Headless = true
  50. };
  51. using (var browser = await Puppeteer.LaunchAsync(opts))
  52. using (var page = await browser.NewPageAsync())
  53. {
  54. await page.GoToAsync(url, WaitUntilNavigation.DOMContentLoaded);
  55. var allResultsSelector = ".chartjs-render-monitor";
  56. await page.WaitForSelectorAsync(allResultsSelector);
  57. await page.EvaluateFunctionAsync("() => document.getElementById('sel').selectedIndex = "+((options.Landscape)?1:0));
  58. await page.ClickAsync("button.dugme");
  59. await EvaluateScript(page, "dist/main.js");
  60. await EvaluateScript(page, "dist/main2.js");
  61. await page.PdfAsync(path, options
  62. // new PdfOptions{
  63. // PrintBackground = true,
  64. // Format = PuppeteerSharp.Media.PaperFormat.Letter
  65. // }
  66. );
  67. }
  68. }
  69. private async Task EvaluateScript(Page page, string fileName)
  70. {
  71. string path2 = Path.Combine(host.ContentRootPath, fileName);
  72. var result = await System.IO.File.ReadAllTextAsync(path2);
  73. await page.EvaluateExpressionAsync(result);
  74. }
  75. private void AddAnImage()
  76. {
  77. using (var inputPdfStream = new FileStream(Path.Combine(host.ContentRootPath, "newFile.pdf"), FileMode.Open))
  78. using (var inputImageStream = new FileStream(Path.Combine(host.ContentRootPath, "pic.jpg"), FileMode.Open))
  79. using (var outputPdfStream = new FileStream(Path.Combine(host.ContentRootPath, "newFilesesses.pdf"), FileMode.Create))
  80. {
  81. PdfReader reader = new PdfReader(inputPdfStream);
  82. PdfStamper stamper = new PdfStamper(reader, outputPdfStream);
  83. PdfContentByte pdfContentByte = stamper.GetOverContent(1);
  84. var image = iTextSharp.text.Image.GetInstance(inputImageStream);
  85. image.ScaleAbsolute(40, 40);
  86. image.SetAbsolutePosition(0, 0);
  87. pdfContentByte.AddImage(image);
  88. stamper.Close();
  89. }
  90. }
  91. private void ModifyPdf(string oldFile)
  92. {
  93. string formFile = Path.Combine(host.ContentRootPath, "newFile.pdf");
  94. string newFile = Path.Combine(host.ContentRootPath, "newFiles.pdf");
  95. // string newFile = Path.Combine(host.ContentRootPath,"newFile.pdf");
  96. PdfReader reader = new PdfReader(formFile);
  97. using (PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create)))
  98. {
  99. AcroFields fields = stamper.AcroFields;
  100. // set form fields
  101. fields.SetField("name", "John Doe");
  102. fields.SetField("address", "xxxxx, yyyy");
  103. fields.SetField("postal_code", "12345");
  104. fields.SetField("email", "johndoe@xxx.com");
  105. // flatten form fields and close document
  106. stamper.FormFlattening = true;
  107. stamper.Close();
  108. }
  109. }
  110. private void Nesto()
  111. {
  112. //string oldFile = Path.Combine(host.ContentRootPath, "0bd7e2f4-b01e-4c17-8f1e-99562d2bab4a.pdf");
  113. string oldFile = "oldFile.pdf";
  114. string newFile = Path.Combine(host.ContentRootPath, "newFile.pdf");
  115. // open the reader
  116. PdfReader reader = new PdfReader(oldFile);
  117. Rectangle size = reader.GetPageSizeWithRotation(1);
  118. Document document = new Document(size);
  119. AcroFields form = reader.AcroFields;
  120. try
  121. {
  122. for (int pagew = 1; pagew <= reader.NumberOfPages; pagew++)
  123. {
  124. }
  125. }
  126. catch { }
  127. // open the writer
  128. FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
  129. PdfWriter writer = PdfWriter.GetInstance(document, fs);
  130. document.Open();
  131. // the pdf content
  132. PdfContentByte cb = writer.DirectContent;
  133. // select the font properties
  134. BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
  135. cb.SetColorFill(BaseColor.DARK_GRAY);
  136. cb.SetFontAndSize(bf, 8);
  137. // write the text in the pdf content
  138. cb.BeginText();
  139. string text = "Some random blablablabla...";
  140. // put the alignment and coordinates here
  141. cb.ShowTextAligned(1, text, 520, 640, 0);
  142. cb.EndText();
  143. cb.BeginText();
  144. text = "Other random blabla...";
  145. // put the alignment and coordinates here
  146. cb.ShowTextAligned(2, text, 100, 200, 0);
  147. cb.EndText();
  148. // create the new page and add it to the pdf
  149. PdfImportedPage page = writer.GetImportedPage(reader, 1);
  150. var pages = reader.GetPageN(1);
  151. var content = writer.DirectContent;
  152. var template = content.CreateTemplate(100, 100);
  153. float width = 100, height = 100;
  154. // PdfDictionary
  155. // pages.CreateTemplate(writer,100,100);
  156. // cb.AddTemplate(page, 0, 0);
  157. // cb.AddTemplate()
  158. // close the streams and voilá the file should be changed :)
  159. document.Close();
  160. fs.Close();
  161. writer.Close();
  162. reader.Close();
  163. }
  164. }
  165. }