I'm using MSEdge.exe in headless mode to convert some HTML Output from my C# app to PDF. The relevant bit of the code that I'm using is below:
string processExe = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
string processArgs = $"--headless --run-all-compositor-stages-before-draw --virtual-time-budget=6000 --no-pdf-header-footer --print-to-pdf-no-header --print-to-pdf=\"{outputFilePath}\" \"{inputFilePath}\"";
var printPDF = Process.Start(processExe, processArgs);
printPDF.WaitForExit();
This worked fine for the last year or so, until today! It seems that there's been an update to something in Edge, and now I'm getting "Exit Code 21" (and no PDF of course), which I cannot find any documentation for.
Does anyone have any idea?
Thanks
The issue seems to be related to Chromium having merged the headless and non-headless into a single exe, and now headless exits when the gui-version is open. This change came to Microsoft Edge with version 128.0.2739.42.
The problem is exacerbated in Edge, because some copilot-integration seems to prevent Edge from closing its processes when you close the window.
A dirty workaround is to use:
--headless=old
This forces the headless mode to use the previous behavior with the standalone headless exe, which will work even though a Edge gui window is open or has been open.
However, it should be noted that this solution only works short-term, as this old headless mode will soon be removed. From the debug output, when using old headless mode:
Old Headless mode will be removed from the Chrome binary soon. Please use the new Headless mode (https://permanently-removed.invalid/docs/chromium/new-headless) or the chrome-headless-shell which is a standalone implementation of the old Headless mode (https://permanently-removed.invalid/blog/chrome-headless-shell).
^^ For some reason Microsoft thinks that linking to google documentation is evil. The working links should probably have been https://developer.chrome.com/docs/chromium/new-headless and https://developer.chrome.com/blog/chrome-headless-shell respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With