Sicos1977 / ChromiumHtmlToPdf

Convert HTML to PDF with a Chromium based browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clearing cookies before invoking ConvertToPdf

aleksander-oven opened this issue · comments

Is there a way to clear all existing cookies via DevTools interface before proceeding with conversion? I could not find one.

I'm currently passing a custom profile directory to the browser process via:

new Converter(..., userProfile: "C:\\MyCustomProfilePath", ...)

And I can see an Sqlite file called:

C:\MyCustomProfilePath\Default\Cookies

But deleting the file isn't possible, because it is locked by the process. Is there any other way?

In case it wasn't self-evident: I'm asking this because I'm reusing the same browser process for multiple conversions in sequence and I need to prevent cookies from being inherited from one to the next.

commented

You could try to do something like this. Just inject it when loading the page. You can use the RunJavascript property for That

function deleteAllCookies() {
    const cookies = document.cookie.split(";");

    for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i];
        const eqPos = cookie.indexOf("=");
        const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}
        /// <summary>
        ///     Runs the given javascript after the webpage has been loaded and before it is converted
        ///     to PDF
        /// </summary>
        public string RunJavascript { get; set; }