How to pass CromeOptions via MVN command line - selenium-chromedriver

Goal :
To pass options.addArguments("--headless"); in the command line.
What's in mind :
mvn clean test --Chrome.options.AddArguments("--headless) ?
getDriver method:
try {
java.util.Properties p = new Properties();
FileInputStream fi = new FileInputStream(System.getProperty("user.dir")
+ "/src/main/resources/config.properties");
p.load(fi);
String browserName = p.getProperty("default.browser");
switch (browserName) {
case "chrome" :
setChromeDriverPath();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("--start-maximized");
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
//options.addArguments("--kiosk");
//options.addArguments("--headless");
driver = new ChromeDriver(options);
break;
case "firefox" :
System.out.println("BROWSER DRIVER - FIREFOX - NOT IMPLEMENTED YET");
break;
case "safari" :
System.out.println("BROWSER DRIVER - SAFARI - NOT IMPLEMENTED YET");
break;
}
} catch (Exception e) {
System.out.println("Unable to load browser : getDriver returns : " + e.getMessage());
}
return driver;
}

In order to do this, you will have to work with the concept of the property function through the System. In general, you will need to create some variables like browser, version, headless or not, etc. and you will make them readable as properties inside your code sample. Every time you will execute your mvn command, you will include after the command the option you want by writing -Dbrowser="Chrome", for example. After that, your code will need to handle the value "Chrome" accordingly. For more details check this link here.
It's a really good analysis. It will solve your problem.

Related

Selenium: Clear chrome cache

In my application I need a way to clear only the cache of the chrome browser before log out (except cookies - I do not want to delete cookies).
Can any one suggest me a way to click on the CLEAR DATA button in chrome.
I have written the below code but the code is not working.
Configuration :
Chrome Version: Version 65.0.3325.181 (Official Build) (64-bit)
Selenium Version: 3.11.0
//Clear the cache for the ChromeDriver instance.
driver.get("chrome://settings/clearBrowserData");
Thread.sleep(10000);
driver.findElement(By.xpath("//*[#id='clearBrowsingDataConfirm']")).click();
You are using here
driver.findElement(By.xpath("//*[#id='clearBrowsingDataConfirm']")).click();
Unfortunately, this won’t work because the Chrome settings page uses Polymer and WebComponents, need to use query selector using the /deep/ combinator, so selector in this case is * /deep/ #clearBrowsingDataConfirm.
Here is workaround to your problem...you can achieve the same using either one of the following...
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
public class ClearChromeCache {
WebDriver driver;
/*This will clear cache*/
#Test
public void clearCache() throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C://WebDrivers/chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("disable-infobars");
chromeOptions.addArguments("start-maximized");
driver = new ChromeDriver(chromeOptions);
driver.get("chrome://settings/clearBrowserData");
Thread.sleep(5000);
driver.switchTo().activeElement();
driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm")).click();
Thread.sleep(5000);
}
/*This will launch browser with cache disabled*/
#Test
public void launchWithoutCache() throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C://WebDrivers/chromedriver.exe");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability("applicationCacheEnabled", false);
driver = new ChromeDriver(cap);
}
}
Chrome supports DevTools Protocol commands like Network.clearBrowserCache (documentation).
Selenium does not have an interface for this proprietary protocol by default.
You can add support by expanding Selenium's commands:
driver.command_executor._commands['SEND_COMMAND'] = (
'POST', '/session/$sessionId/chromium/send_command'
)
This is how you use it:
driver.execute('SEND_COMMAND', dict(cmd='Network.clearBrowserCache', params={}))
Note: this example is for Selenium for Python, but it's also possible in Selenium for other platforms in a similar way by expanding the commands.
YEAR 2020 Solution (using Selenium 4 alpha):
Using the devtools
private void clearDriverCache(ChromeDriver driver) {
driver.getDevTools().createSessionIfThereIsNotOne();
driver.getDevTools().send(Network.clearBrowserCookies());
// you could also use
// driver.getDevTools().send(Network.clearBrowserCache());
}
Don´t forget the send keys!!!!
For Selenium Basic, below code is functional.
Function clean_cache()
Set driver = New ChromeDriver
Dim keys As New Selenium.keys
driver.Get "chrome://settings/clearBrowserData"
Sleep 5000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Enter)
Sleep 2000
driver.Quit
End Function
There is another way to click on Clear data button by traversing through shadow tree. If you are trying to locate clear data button by simply searching web element by locator strategy, it won't work due to Chrome browser version upgrade. You need to traverse through shadow tree. You can try below code to click on "Clear data" in advance tab:
package selenium.demo.test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test {
static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","F:\\selenium\\chromedriver.exe");
// Instantiate a ChromeDriver class.
driver=new ChromeDriver();
driver.get("chrome://settings/clearBrowserData");
WebElement root1 = driver.findElement(By.cssSelector("settings-ui"));
// get 1st shadowroot element
WebElement shadowRoot1 = expandRootElement(root1);
// get 2nd parent
WebElement root2 = shadowRoot1.findElement(By.cssSelector("settings-main"));
// get 2nd shadowroot element
WebElement shadowRoot2 = expandRootElement(root2);
// get 3rd parent
WebElement root3 = shadowRoot2.findElement(By.cssSelector("settings-basic-page"));
// get 3rd shadowroot element
WebElement shadowRoot3 = expandRootElement(root3);
// get 4th parent
WebElement root4 = shadowRoot3.findElement(By.cssSelector("settings-section > settings-privacy-page"));
// get 4th shadowroot element
WebElement shadowRoot4 = expandRootElement(root4);
// get 5th parent
WebElement root5 = shadowRoot4.findElement(By.cssSelector("settings-clear-browsing-data-dialog"));
// get 5th shadowroot element
WebElement shadowRoot5 = expandRootElement(root5);
// get 6th parent
WebElement root6 = shadowRoot5.findElement(By.cssSelector("#clearBrowsingDataDialog"));
WebElement root7 = root6.findElement(By.cssSelector("cr-tabs[role='tablist']"));
root7.click();
WebElement clearDataButton = root6.findElement(By.cssSelector("#clearBrowsingDataConfirm"));
clearDataButton.click(); // click that hard to reach button!
driver.quit();
}
private static WebElement expandRootElement(WebElement element) {
WebElement ele = (WebElement) ((JavascriptExecutor) driver)
.executeScript("return arguments[0].shadowRoot", element);
return ele;
}
}
Below snippet navigates to the the chrome settings for clearing the browser data and sends a keypress to the opened dialog. Then it waits for the tab to close. You could easily do these steps manual.
IWebDriver Driver = new ChromeDriver()
Driver.Navigate().GoToUrl("chrome://settings/clearBrowserData");
Driver.SwitchTo().ActiveElement();
Driver.FindElement(By.XPath("//settings-ui")).SendKeys(Keys.Enter);
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
wait.Until(wd => wd.Url.StartsWith("chrome://settings"));
this way work for me :
in step one =>
pip install keyboard
step2 : use it in your code =>
from time import sleep
self.driver.get('chrome://settings/clearBrowserData')
sleep(10)
keyboard.send("Enter")
Method #1 worked for me in clearing JWT using python selenium and chromedriver 87.
# method 1
driver.execute_script('window.localStorage.clear()')
# method 2
driver.execute_script('window.sessionStorage.clear()')
The correct solution is deprecated, I've solved the problem by following this guide:
https://www.browserstack.com/guide/how-to-handle-cookies-in-selenium#:~:text=Navigate%20to%20the%20chrome%20settings,to%20open%20Chrome%20Developer%20Tools.
Specifically, using the command:
driver.manage().deleteAllCookies();
self.driver.get('chrome://settings/clearBrowserData')
time.sleep(0.5) # this is necessary
actions = ActionChains(self.driver)
actions.send_keys(Keys.TAB * 7 + Keys.ENTER)
actions.perform()
The controling protocol already have this task:
https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-clearBrowserCache
And selenium v4+ have this implemented in its API:
driver.getDevTools().send(Network.clearBrowserCache());
For older versions of selenium it's still possible to call this method natively using underlying protocol:
private void clearCache(ChromeDriverService service, WebDriver driver) throws IOException {
Map<String, Object> commandParams = new HashMap<>();
commandParams.put("cmd", "Network.clearBrowserCache");
commandParams.put("params", emptyMap());
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();
String command = objectMapper.writeValueAsString(commandParams);
String u = service.getUrl().toString() + "/session/" + driver.getSessionId() + "/chromium/send_command";
HttpPost request = new HttpPost(u);
request.addHeader("content-type", "application/json");
request.setEntity(new StringEntity(command));
httpClient.execute(request);
}
Note: for chromium you should use "/chromium/send_command" endpoint, for chrome: "/goog/cdp/execute".
But as of my experience, these both work the same way in both chrome and chromium.
Just add below code
driver.manage().deleteAllCookies();
Getting NoSuchElement exception :
driver = new ChromeDriver(service, chromeOption());
driver.manage().deleteAllCookies();
driver.get("chrome://settings/clearBrowserData");
staticWait(5);
driver.switchTo().activeElement();
driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm")).click();
Chrome Version : 97.0.4692.71

Geolocator.RequestAccessAsync throws "method called at unexpected time" exception

I have a chunk of code I use to get device location. This is done in a monogame windows uwp project. The following code worked before (I was on VS2015 Community). I recent did a fresh OS and install VS2017 Community. I finally got my project to build and run. The only thing not working is the Geolocator.RequestAccessAsync is throwing a "method called at unexpected time" exception. Any ideas?
public async Task<Vector2> GetDeviceGps()
{
var accessStatus = await Geolocator.RequestAccessAsync();
switch (accessStatus)
{
case GeolocationAccessStatus.Allowed:
// If DesiredAccuracy or DesiredAccuracyInMeters are not set (or value is 0), DesiredAccuracy.Default is used.
Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = 10 };
// Subscribe to the StatusChanged event to get updates of location status changes.
//geolocator.StatusChanged += OnStatusChanged;
// Carry out the operation.
Geoposition pos = await geolocator.GetGeopositionAsync();
return new Vector2((float)pos.Coordinate.Point.Position.Longitude, (float)pos.Coordinate.Point.Position.Latitude);
case GeolocationAccessStatus.Denied:
//Do something!
break;
case GeolocationAccessStatus.Unspecified:
//Murp
break;
}
return Vector2.Zero;
}
This is invoked and handled like so (called from update method of game):
mapper.GetDeviceGps().ContinueWith(pos =>
{
Vector2 bob = pos.Result;
});
When you run the RequestAccessAsync for the first time in your app, the user will be asked to grant your app the location permission. Therefore the method needs to be executed in the UI thread.

How do you automatically open the Chrome Devtools tab within Selenium (C#)?

I see that there's a relatively new option to open Chrome with Devtools open from the command line, which I have gotten to work from my Windows 8.1 command line using a call like this:
c:\Program Files (x86)\Google\Chrome\Application>"chrome.exe" --auto-open-devtools-for-tabs
When I try to add this option on the same box when creating my ChromeDriver in Selenium (in C#), however, the option seems to be ignored.
var options = new ChromeOptions();
options.AddArgument("auto-open-devtools-for-tabs");
string executingAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location;
string driverPath = Path.Combine(Path.GetDirectoryName(executingAssembly), "ChromeWebDriver");
_driver = new ChromeDriver(driverPath, options);
I've tried a few variations on theme to make sure options are working at all, including...
var options = new ChromeOptions();
options.AddArguments(new[] { "start-maximized", "auto-open-devtools-for-tabs"});
... and...
var options = new ChromeOptions();
options.AddArgument("start-maximized");
options.AddArgument("auto-open-devtools-for-tabs");
... and...
var options = new ChromeOptions();
options.AddArgument("start-maximized");
options.AddExcludedArgument("auto-open-devtools-for-tabs");
... as well as setting those with -- in front of each option string. All I get from any of those are maximized windows.
I get the feeling the auto-open-devtools-for-tabs argument's not supported by Selenium's Chrome Web Driver, but I'm not sure why that wouldn't support the same set of options as the "full" app.
Anyone have this option working with Selenium in C#, or know why it shouldn't be working in this case?
This is not unlike this question, but here I'm asking specifically about the auto-open-devtools-for-tabs option with C#. That asker claims not to have had any luck with options, and was asking how to open devtools from "within" Selenium, looking for a method explicitly before this option existed.
I've tried this with in VS 2017, Selenium v3.12.1#, Firefox v60.0.2, Chrome v66, Nunit v3.10.1, Gecko Driver v20.1, and Chrome driver v2.4 (all using C#).
I tried to search for Firefox but did not have any success. I did find a solution for Chrome v66.
Please provide profile like this: options.AddArguments("--auto-open-devtools-for-tabs");
This is a complete chrome driver implementation:
ChromeOptions options = new ChromeOptions();
options.AddArgument("--start-maximized");
options.AddArguments("disable-infobars");
options.AddArguments("--disable-notifications");
options.AddArguments("--auto-open-devtools-for-tabs");
driver = new ChromeDriver(DrivePath, options, TimeSpan.FromSeconds(100));
See also this post: "List of Chromium Command Line Switches"
Below commands are NOT working, this is issue with Geckodriver so Gecko team has to provide some solution or fix for that:
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.F12);
Actions action = new Actions(driver); action.SendKeys(Keys.F12); action.Perform();
Actions action = new Actions(driver); action .KeyDown(Keys.Control)
.SendKeys(Keys.F12).KeyUp(Keys.Control).Perform();
Actions action = new Actions(driver); action.SendKeys(Keys.F12); action.Click();
Following the thread on SO-12212504 and leading from the selected answer.
One of the solution to this would be pressing F-12 [Key F12 Documentation] key using :
// without an element
new Actions(driver).SendKeys(Keys.F12).Perform();
// send keys to body
new Actions(driver).SendKeys(driver.FindElement(By.XPath("//body")), Keys.F12).Perform();
On the other side could you try and use AddUserProfilePreference from amongst the ChromeOptions Methods :
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("auto-open-devtools-for-tabs", "true");
Note : I am not very sure about the parameter name, but I hope you can find something corresponding here.
Edit : Some more attempts using keyboard shortcuts for the same -
Windows : [F12 or Ctrl + Shift + I]
String openDevTools = Keys.chord(Keys.CONTROL, Keys.SHIFT, "I");
driver.FindElement(By.XPath("//body")).SendKeys(openDevTools).Perform();
Mac : [Cmd + Opt + I]
String openDevTools = Keys.chord(Keys.COMMAND, Keys.ALT, "I");
driver.FindElement(By.XPath("//body")).SendKeys(openDevTools).Perform();
Ruby: must have installed latest selenium-webdriver (3.7.0) gem
options1 = Selenium::WebDriver::Chrome::Options.new
options1.add_argument('--auto-open-devtools-for-tabs')
driver = Selenium::WebDriver.for :chrome, options: options1
driver.get("https://stackoverflow.com")
I think the issue is with your options being declared as a var and not ChromeOptions, this code open google.com with dev tools open
public static void Scraps()
{
//Declare options variable and set dev tools argument
ChromeOptions co = new ChromeOptions();
co.AddArguments("--auto-open-devtools-for-tabs");
//Initiate driver instance and go to google.com
IWebDriver driver = new ChromeDriver(co);
driver.Url = "https://www.google.com/";
}

Reading a project text file windows phone 8

I'm having trouble accessing a text file that is packaged with my Windows Phone 8 app.
Here is the code:
var ResrouceStream = Application.GetResourceStream(new Uri("Data-Test.docx", UriKind.Relative));
if (ResrouceStream != null)
{
Stream myFileStream = ResrouceStream.Stream;
if (myFileStream.CanRead)
{
// logiic here
retrun "Hi";
}
}
else
{
return "hello";
}
Seems simple but the app always returns "hello". i have placed the file in root and also in assets, changed it to content - copy and do not copy, resource copy and do not copy but always it returns "hello".
Spent several hours on this and all solutions I can find show the solution or very similar above!
What am I doing wrong?
EDIT: Returns "hello" when I deploy to phone or emulator.
also tried "/Data-Test...", #"\Data-Text..., #/"Data-Test...!
UPDATE 1:
string aReturn = "";
var asm = Assembly.GetExecutingAssembly();
//Use this to verify the namespacing of the "Embedded Resource".
//asm.GetManifestResourceNames()
// .ToList()
// .ForEach(name => Debug.WriteLine(name));
var ResourceStream = asm.GetManifestResourceStream("ContosoSocial.Assets.QuizQuestions.QuizQuestions-Test1.docx");
if (ResourceStream != null) // <--CHECKED AND DOES NOT EQUAL NULL
{
Stream myFileStream = ResourceStream;
if (myFileStream.CanRead) // <-- CHEACKED AND CAN READ
{
StreamReader myStreamReader = new StreamReader(myFileStream);
LOGIC & EXCEPTION HERE...?
string myLine = myStreamReader.ReadLine();
}
else
{
aReturn = "myFileStream.CanRead = true";
}
}
else
{
aReturn = "stream equals null";
}
Debug.WriteLine(aReturn);
}
The assignment of myFileStream to a StreamReader object is throwing the exception null pointer. I thought I would wrap myFileStream to a StreamReader so I can read a line at a time..? This is my first c# project and I'm unfamiliar with it's syntax and classes.
UPDATE 2:
OK I added...
Debug.WriteLine(aReturn);
...following...
string myLine = myStreamReader.ReadLine();
...and noticed it was retrieving only the 2 characters 'PK' !
So saved the .docx file as .txt and reinserted adn changed build copy to embedded - do not copy...Happy days it now pulls off the first line in the file.
Thanks to OmegaMan for your help with this one :-)
Change file type in the project to Embedded Resource
Extract the resource by working the namespace to its location. Here is an example code where I pull in an XSD:
Code:
var asm = Assembly.GetExecutingAssembly();
// Use this to verify the namespacing of the "Embedded Resource".
// asm.GetManifestResourceNames()
// .ToList()
// .ForEach(name => Debug.WriteLine(name));
var f1 = asm.GetManifestResourceStream("UnitTests.Resources.NexusResponse.xsd");
Note this is not tested on WP8, but GetExecutingAssembly is stated to work within .Net. If you get the namespace wrong, uncomment out the code and display or debug to determine the resources and their namespace.

Calling wkhtmltopdf to generate PDF from HTML

I'm attempting to create a PDF file from an HTML file. After looking around a little I've found: wkhtmltopdf to be perfect. I need to call this .exe from the ASP.NET server. I've attempted:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = HttpContext.Current.Server.MapPath("wkhtmltopdf.exe");
p.StartInfo.Arguments = "TestPDF.htm TestPDF.pdf";
p.Start();
p.WaitForExit();
With no success of any files being created on the server. Can anyone give me a pointer in the right direction? I put the wkhtmltopdf.exe file at the top level directory of the site. Is there anywhere else it should be held?
Edit: If anyone has better solutions to dynamically create pdf files from html, please let me know.
Update:
My answer below, creates the pdf file on the disk. I then streamed that file to the users browser as a download. Consider using something like Hath's answer below to get wkhtml2pdf to output to a stream instead and then send that directly to the user - that will bypass lots of issues with file permissions etc.
My original answer:
Make sure you've specified an output path for the PDF that is writeable by the ASP.NET process of IIS running on your server (usually NETWORK_SERVICE I think).
Mine looks like this (and it works):
/// <summary>
/// Convert Html page at a given URL to a PDF file using open-source tool wkhtml2pdf
/// </summary>
/// <param name="Url"></param>
/// <param name="outputFilename"></param>
/// <returns></returns>
public static bool HtmlToPdf(string Url, string outputFilename)
{
// assemble destination PDF file name
string filename = ConfigurationManager.AppSettings["ExportFilePath"] + "\\" + outputFilename + ".pdf";
// get proj no for header
Project project = new Project(int.Parse(outputFilename));
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = ConfigurationManager.AppSettings["HtmlToPdfExePath"];
string switches = "--print-media-type ";
switches += "--margin-top 4mm --margin-bottom 4mm --margin-right 0mm --margin-left 0mm ";
switches += "--page-size A4 ";
switches += "--no-background ";
switches += "--redirect-delay 100";
p.StartInfo.Arguments = switches + " " + Url + " " + filename;
p.StartInfo.UseShellExecute = false; // needs to be false in order to redirect output
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true; // redirect all 3, as it should be all 3 or none
p.StartInfo.WorkingDirectory = StripFilenameFromFullPath(p.StartInfo.FileName);
p.Start();
// read the output here...
string output = p.StandardOutput.ReadToEnd();
// ...then wait n milliseconds for exit (as after exit, it can't read the output)
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
// if 0 or 2, it worked (not sure about other values, I want a better way to confirm this)
return (returnCode == 0 || returnCode == 2);
}
I had the same problem when i tried using msmq with a windows service but it was very slow for some reason. (the process part).
This is what finally worked:
private void DoDownload()
{
var url = Request.Url.GetLeftPart(UriPartial.Authority) + "/CPCDownload.aspx?IsPDF=False?UserID=" + this.CurrentUser.UserID.ToString();
var file = WKHtmlToPdf(url);
if (file != null)
{
Response.ContentType = "Application/pdf";
Response.BinaryWrite(file);
Response.End();
}
}
public byte[] WKHtmlToPdf(string url)
{
var fileName = " - ";
var wkhtmlDir = "C:\\Program Files\\wkhtmltopdf\\";
var wkhtml = "C:\\Program Files\\wkhtmltopdf\\wkhtmltopdf.exe";
var p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = wkhtml;
p.StartInfo.WorkingDirectory = wkhtmlDir;
string switches = "";
switches += "--print-media-type ";
switches += "--margin-top 10mm --margin-bottom 10mm --margin-right 10mm --margin-left 10mm ";
switches += "--page-size Letter ";
p.StartInfo.Arguments = switches + " " + url + " " + fileName;
p.Start();
//read output
byte[] buffer = new byte[32768];
byte[] file;
using(var ms = new MemoryStream())
{
while(true)
{
int read = p.StandardOutput.BaseStream.Read(buffer, 0,buffer.Length);
if(read <=0)
{
break;
}
ms.Write(buffer, 0, read);
}
file = ms.ToArray();
}
// wait or exit
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
return returnCode == 0 ? file : null;
}
Thanks Graham Ambrose and everyone else.
OK, so this is an old question, but an excellent one. And since I did not find a good answer, I made my own :) Also, I've posted this super simple project to GitHub.
Here is some sample code:
var pdfData = HtmlToXConverter.ConvertToPdf("<h1>SOO COOL!</h1>");
Here are some key points:
No P/Invoke
No creating of a new process
No file-system (all in RAM)
Native .NET DLL with intellisense, etc.
Ability to generate a PDF or PNG (HtmlToXConverter.ConvertToPng)
Check out the C# wrapper library (using P/Invoke) for the wkhtmltopdf library: https://github.com/pruiz/WkHtmlToXSharp
There are many reason why this is generally a bad idea. How are you going to control the executables that get spawned off but end up living on in memory if there is a crash? What about denial-of-service attacks, or if something malicious gets into TestPDF.htm?
My understanding is that the ASP.NET user account will not have the rights to logon locally. It also needs to have the correct file permissions to access the executable and to write to the file system. You need to edit the local security policy and let the ASP.NET user account (maybe ASPNET) logon locally (it may be in the deny list by default). Then you need to edit the permissions on the NTFS filesystem for the other files. If you are in a shared hosting environment it may be impossible to apply the configuration you need.
The best way to use an external executable like this is to queue jobs from the ASP.NET code and have some sort of service monitor the queue. If you do this you will protect yourself from all sorts of bad things happening. The maintenance issues with changing the user account are not worth the effort in my opinion, and whilst setting up a service or scheduled job is a pain, its just a better design. The ASP.NET page should poll a result queue for the output and you can present the user with a wait page. This is acceptable in most cases.
You can tell wkhtmltopdf to send it's output to sout by specifying "-" as the output file.
You can then read the output from the process into the response stream and avoid the permissions issues with writing to the file system.
My take on this with 2018 stuff.
I am using async. I am streaming to and from wkhtmltopdf. I created a new StreamWriter because wkhtmltopdf is expecting utf-8 by default but it is set to something else when the process starts.
I didn't include a lot of arguments since those varies from user to user. You can add what you need using additionalArgs.
I removed p.WaitForExit(...) since I wasn't handling if it fails and it would hang anyway on await tStandardOutput. If timeout is needed, then you would have to call Wait(...) on the different tasks with a cancellationtoken or timeout and handle accordingly.
public async Task<byte[]> GeneratePdf(string html, string additionalArgs)
{
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = #"C:\Program Files\wkhtmltopdf\wkhtmltopdf.exe",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = "-q -n " + additionalArgs + " - -";
};
using (var p = Process.Start(psi))
using (var pdfSream = new MemoryStream())
using (var utf8Writer = new StreamWriter(p.StandardInput.BaseStream,
Encoding.UTF8))
{
await utf8Writer.WriteAsync(html);
utf8Writer.Close();
var tStdOut = p.StandardOutput.BaseStream.CopyToAsync(pdfSream);
var tStdError = p.StandardError.ReadToEndAsync();
await tStandardOutput;
string errors = await tStandardError;
if (!string.IsNullOrEmpty(errors)) { /* deal/log with errors */ }
return pdfSream.ToArray();
}
}
Things I haven't included in there but could be useful if you have images, css or other stuff that wkhtmltopdf will have to load when rendering the html page:
you can pass the authentication cookie using --cookie
in the header of the html page, you can set the base tag with href pointing to the server and wkhtmltopdf will use that if need be
Thanks for the question / answer / all the comments above. I came upon this when I was writing my own C# wrapper for WKHTMLtoPDF and it answered a couple of the problems I had. I ended up writing about this in a blog post - which also contains my wrapper (you'll no doubt see the "inspiration" from the entries above seeping into my code...)
Making PDFs from HTML in C# using WKHTMLtoPDF
Thanks again guys!
The ASP .Net process probably doesn't have write access to the directory.
Try telling it to write to %TEMP%, and see if it works.
Also, make your ASP .Net page echo the process's stdout and stderr, and check for error messages.
Generally return code =0 is coming if the pdf file is created properly and correctly.If it's not created then the value is in -ve range.
using System;
using System.Diagnostics;
using System.Web;
public partial class pdftest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void fn_test()
{
try
{
string url = HttpContext.Current.Request.Url.AbsoluteUri;
Response.Write(url);
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName =
#"C:\PROGRA~1\WKHTML~1\wkhtmltopdf.exe";//"wkhtmltopdf.exe";
startInfo.Arguments = url + #" C:\test"
+ Guid.NewGuid().ToString() + ".pdf";
Process.Start(startInfo);
}
catch (Exception ex)
{
string xx = ex.Message.ToString();
Response.Write("<br>" + xx);
}
}
protected void btn_test_Click(object sender, EventArgs e)
{
fn_test();
}
}