| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System.Data.Common;
- using BlackRock.Reporting.API.Core;
- using BlackRock.Reporting.API.Core.Models;
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using PuppeteerSharp;
- using iTextSharp;
- using iTextSharp.text.xml;
-
- namespace BlackRock.Reporting.API.Persistence
- {
-
- public class PdfGenerator : IPdfGenerator
- {
- private readonly IHostEnvironment host;
- public PdfGenerator(IHostEnvironment host)
- {
- this.host = host;
- }
- public async Task Generate(string url, string path, PdfOptions options)
- {
- await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
- var opts = new LaunchOptions
- {
- Headless = true
- };
- using (var browser = await Puppeteer.LaunchAsync(opts))
- using (var page = await browser.NewPageAsync())
- {
- await page.GoToAsync(url, WaitUntilNavigation.DOMContentLoaded);
-
-
- var allResultsSelector = ".chartjs-render-monitor";
- await page.WaitForSelectorAsync(allResultsSelector);
- await EvaluateScript(page, "dist/main.js");
- await page.PdfAsync(path, options
- // new PdfOptions{
- // Landscape = true,
- // PrintBackground = true,
- // Format = PuppeteerSharp.Media.PaperFormat.Letter
- // }
- );
-
- }
-
- // POST header image
-
- // ModifyPdf(path);
- }
-
- public async Task Isolate(string url, string path, PdfOptions options)
- {
- await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
- var opts = new LaunchOptions
- {
- Headless = true
- };
- using (var browser = await Puppeteer.LaunchAsync(opts))
- using (var page = await browser.NewPageAsync())
- {
- await page.GoToAsync(url, WaitUntilNavigation.DOMContentLoaded);
-
-
- var allResultsSelector = ".chartjs-render-monitor";
- await page.WaitForSelectorAsync(allResultsSelector);
- await EvaluateScript(page, "dist/main.js");
- await EvaluateScript(page, "dist/main2.js");
- await page.PdfAsync(path, options
- // new PdfOptions{
- // PrintBackground = true,
- // Format = PuppeteerSharp.Media.PaperFormat.Letter
- // }
- );
-
- }
- }
-
- private async Task EvaluateScript(Page page, string fileName)
- {
- string path2 = Path.Combine(host.ContentRootPath, fileName);
- var result = await System.IO.File.ReadAllTextAsync(path2);
- await page.EvaluateExpressionAsync(result);
- }
- private void AddAnImage()
- {
- using (var inputPdfStream = new FileStream(Path.Combine(host.ContentRootPath, "newFile.pdf"), FileMode.Open))
- using (var inputImageStream = new FileStream(Path.Combine(host.ContentRootPath, "pic.jpg"), FileMode.Open))
- using (var outputPdfStream = new FileStream(Path.Combine(host.ContentRootPath, "newFilesesses.pdf"), FileMode.Create))
- {
- PdfReader reader = new PdfReader(inputPdfStream);
- PdfStamper stamper = new PdfStamper(reader, outputPdfStream);
- PdfContentByte pdfContentByte = stamper.GetOverContent(1);
- var image = iTextSharp.text.Image.GetInstance(inputImageStream);
- image.ScaleAbsolute(40, 40);
- image.SetAbsolutePosition(0, 0);
- pdfContentByte.AddImage(image);
- stamper.Close();
- }
- }
- private void ModifyPdf(string oldFile)
- {
- string formFile = Path.Combine(host.ContentRootPath, "newFile.pdf");
- string newFile = Path.Combine(host.ContentRootPath, "newFiles.pdf");
- // string newFile = Path.Combine(host.ContentRootPath,"newFile.pdf");
- PdfReader reader = new PdfReader(formFile);
- using (PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create)))
- {
- AcroFields fields = stamper.AcroFields;
-
- // set form fields
- fields.SetField("name", "John Doe");
- fields.SetField("address", "xxxxx, yyyy");
- fields.SetField("postal_code", "12345");
- fields.SetField("email", "johndoe@xxx.com");
-
- // flatten form fields and close document
- stamper.FormFlattening = true;
- stamper.Close();
- }
- }
-
- private void Nesto()
- {
- //string oldFile = Path.Combine(host.ContentRootPath, "0bd7e2f4-b01e-4c17-8f1e-99562d2bab4a.pdf");
- string oldFile = "oldFile.pdf";
- string newFile = Path.Combine(host.ContentRootPath, "newFile.pdf");
-
- // open the reader
- PdfReader reader = new PdfReader(oldFile);
- Rectangle size = reader.GetPageSizeWithRotation(1);
- Document document = new Document(size);
-
- AcroFields form = reader.AcroFields;
- try
- {
- for (int pagew = 1; pagew <= reader.NumberOfPages; pagew++)
- {
-
- }
- }
- catch { }
-
- // open the writer
- FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
- PdfWriter writer = PdfWriter.GetInstance(document, fs);
- document.Open();
-
- // the pdf content
- PdfContentByte cb = writer.DirectContent;
-
- // select the font properties
- BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
- cb.SetColorFill(BaseColor.DARK_GRAY);
- cb.SetFontAndSize(bf, 8);
-
- // write the text in the pdf content
- cb.BeginText();
- string text = "Some random blablablabla...";
- // put the alignment and coordinates here
- cb.ShowTextAligned(1, text, 520, 640, 0);
- cb.EndText();
- cb.BeginText();
- text = "Other random blabla...";
- // put the alignment and coordinates here
- cb.ShowTextAligned(2, text, 100, 200, 0);
- cb.EndText();
-
- // create the new page and add it to the pdf
- PdfImportedPage page = writer.GetImportedPage(reader, 1);
- var pages = reader.GetPageN(1);
- var content = writer.DirectContent;
- var template = content.CreateTemplate(100, 100);
- float width = 100, height = 100;
- // PdfDictionary
- // pages.CreateTemplate(writer,100,100);
- // cb.AddTemplate(page, 0, 0);
- // cb.AddTemplate()
-
- // close the streams and voilá the file should be changed :)
- document.Close();
- fs.Close();
- writer.Close();
- reader.Close();
- }
- }
- }
|