You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PdfGenerator.cs 7.0KB

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