file-folder-helper/ADO2025/PI5/Helper-2025-05-05.cs
Mike Phares 43527b3356 Selenium (Not Fully Tested)
hyper-text-markup-language-to-portable-document-format (Not Fully Tested)
2025-05-19 10:07:27 -07:00

84 lines
3.4 KiB
C#

#if Html2pdf
using iText.Html2pdf;
#endif
using Microsoft.Extensions.Logging;
#if Selenium
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
#endif
namespace File_Folder_Helper.ADO2025.PI5;
internal static partial class Helper20250505 {
// <PackageReference Include="Selenium.WebDriver" Version="4.31.0" />
// <PackageReference Include="iText" Version="9.1.0" />
// <PackageReference Include="iText.bouncy-castle-adapter" Version="9.1.0" />
// <PackageReference Include="iText.commons" Version="9.1.0" />
// <PackageReference Include="iText.hyph" Version="9.1.0" />
// <PackageReference Include="iText.pdfhtml" Version="6.1.0" />
internal static void HyperTextMarkupLanguageToPortableDocumentFormat(ILogger<Worker> logger, List<string> args) {
if (args.Count == 999)
TestA();
if (args.Count == 999)
TestB();
if (args.Count != 999)
TestC(logger);
}
private static void TestA() {
#if Html2pdf
string inputFile = Path.Combine(Environment.CurrentDirectory, ".vscode", "helper", ".html");
string outputFile = Path.Combine(Environment.CurrentDirectory, ".vscode", "helper", "a.pdf");
using (FileStream htmlSource = File.Open(inputFile, FileMode.Open))
using (FileStream pdfDest = File.Open(outputFile, FileMode.Create)) {
ConverterProperties converterProperties = new();
HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);
}
#endif
}
private static void TestB() {
#if Html2pdf
HttpClient httpClient = new();
Task<Stream> stream = httpClient.GetStreamAsync("https://ourrescue.org/");
stream.Wait();
string outputFile = Path.Combine(Environment.CurrentDirectory, ".vscode", "helper", "b.pdf");
using (FileStream pdfDest = File.Open(outputFile, FileMode.Create)) {
ConverterProperties converterProperties = new();
HtmlConverter.ConvertToPdf(stream.Result, pdfDest, converterProperties);
}
#endif
}
private static void TestC(ILogger<Worker> logger) {
#if Selenium
EdgeOptions edgeOptions = new();
edgeOptions.AddArgument("--no-sandbox");
edgeOptions.AddArgument("--disable-gpu");
edgeOptions.AddArgument("--headless=new");
edgeOptions.AddArgument("--start-maximized");
edgeOptions.AddArgument("--profile-directory=Default");
edgeOptions.AddArgument("--browser-version 133.0.3065.82");
EdgeDriver edgeDriver = new(edgeOptions);
string outputFile = Path.Combine(Environment.CurrentDirectory, ".vscode", "helper", ".png");
try {
// edgeDriver.Navigate().GoToUrl("https://ourrescue.org/");
// edgeDriver.Navigate().GoToUrl("https://intranet.infineon.com/");
edgeDriver.Navigate().GoToUrl("https://messa020ec.infineon.com:50205/ProductionReport/DailyReport");
int fullWidth = int.Parse(edgeDriver.ExecuteScript("return document.body.parentNode.scrollWidth").ToString());
int fullHeight = int.Parse(edgeDriver.ExecuteScript("return document.body.parentNode.scrollHeight").ToString());
edgeDriver.Manage().Window.Size = new(fullWidth, fullHeight);
Screenshot screenshot = edgeDriver.GetScreenshot();
screenshot.SaveAsFile(outputFile);
} catch (Exception ex) {
logger.LogError(ex, ex.Message);
}
edgeDriver.Close();
#endif
}
}