Zimt

How to Remove Cookie Banners from wkhtmltopdf Screenshots

· 6 min read · Cookies and Bars

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To remove cookie banners from wkhtmltopdf screenshots, you need to hide or block the banner elements before the page is rendered. This is done by injecting custom CSS or JavaScript that sets the banner's display to none, or by using wkhtmltopdf's built-in options to run JavaScript after the page loads. The result is a clean screenshot without consent pop-ups, which is useful for documentation, reports, or automated testing where visual noise is unwanted.

What You'll Need

  • wkhtmltopdf installed on your system (version 0.12.5 or later recommended for better JavaScript support).
  • Basic knowledge of HTML, CSS, and command-line usage.
  • A text editor to create or modify CSS/JavaScript files.
  • Access to the target website's HTML structure (use browser developer tools to inspect the cookie banner).
  • Optional: a local web server if you need to serve custom files to wkhtmltopdf.

Step-by-Step

  1. Identify the cookie banner's CSS selector. Open the target page in a browser, right-click the banner, and choose "Inspect". Note the element's class, ID, or a unique combination (e.g., .cookie-consent or #gdpr-banner). If the banner is inside an iframe, you'll need to handle that separately (see Troubleshooting).
  2. Create a custom CSS file. In a new file (e.g., hide-banner.css), add a rule that hides the banner: .cookie-consent { display: none !important; }. Use !important to override inline styles. If the banner uses a fixed position, also consider visibility: hidden or opacity: 0 as alternatives.
  3. Create a JavaScript file (optional but recommended). Some banners are injected dynamically after page load. A CSS rule may not catch them if they are added later. In hide-banner.js, add: document.addEventListener('DOMContentLoaded', function() { var banner = document.querySelector('.cookie-consent'); if (banner) banner.style.display = 'none'; });. This ensures the banner is hidden even if it appears after initial render.
  4. Use wkhtmltopdf's --run-script option. Pass your JavaScript file directly: wkhtmltopdf --run-script hide-banner.js input.html output.pdf. Note that --run-script executes after the page loads, so it can handle dynamically inserted banners.
  5. Alternatively, inject CSS via --user-style-sheet. Use: wkhtmltopdf --user-style-sheet hide-banner.css input.html output.pdf. This applies your CSS to the page before rendering, which is often simpler for static banners.
  6. Combine both approaches for robustness. Use --user-style-sheet to hide the banner early and --run-script to catch any late additions. For example: wkhtmltopdf --user-style-sheet hide-banner.css --run-script hide-banner.js input.html output.pdf.
  7. Test with a screenshot. Run wkhtmltopdf with the --javascript-delay option to allow scripts to execute: wkhtmltopdf --javascript-delay 1000 --user-style-sheet hide-banner.css --run-script hide-banner.js input.html output.png. Inspect the output to confirm the banner is gone.
  8. Adjust selectors if needed. If the banner persists, re-inspect the page; the selector may have changed or the banner may be in a shadow DOM or iframe. Update your CSS/JS accordingly and repeat.

Common Mistakes

  • Using a selector that is too generic. Hiding all elements with a class like .modal might hide other important content. Always use the most specific selector possible.
  • Forgetting !important. Many cookie banners use inline styles or high-specificity CSS. Without !important, your rule may be overridden.
  • Not waiting for JavaScript. If the banner is added by JavaScript after load, a CSS-only approach may fail. Use --run-script and a delay.
  • Ignoring iframes. If the banner is inside an iframe, you cannot style it from the parent page due to same-origin policy. You'll need to disable iframes or use a different approach.
  • Overlooking shadow DOM. Some modern banners use shadow DOM, which isolates styles. You may need to inject JavaScript that traverses shadow roots.
  • Assuming all pages use the same selector. If you're batch-processing multiple sites, each may have a different banner structure. Create site-specific CSS/JS files.

Tips

  • Use browser developer tools to test your CSS/JS live. Before running wkhtmltopdf, paste your CSS into the browser's style editor or run your JS in the console to confirm it hides the banner.
  • Prefer display: none over visibility: hidden. The latter leaves empty space where the banner was, which may still affect layout.
  • Set a reasonable --javascript-delay. A delay of 1000–2000 ms is usually enough for most banners to appear and be hidden. Too short and the script runs before the banner exists; too long and rendering is slow.
  • Consider using a headless browser instead. If wkhtmltopdf struggles with complex banners, tools like Puppeteer or Playwright offer more control over page interaction and element hiding.
  • Keep your CSS/JS files version-controlled. As websites update their banners, you may need to adjust selectors. Store them alongside your wkhtmltopdf commands for reproducibility.

Troubleshooting

  • Banner still appears after using --user-style-sheet: Check that the CSS file path is correct and that the selector matches. Use --debug-javascript to see if wkhtmltopdf reports errors. Also try adding !important.
  • JavaScript doesn't run: Ensure --enable-javascript is set (it is by default in most builds). Verify that your script has no syntax errors by running it in a browser console first.
  • Banner is inside an iframe: You cannot style cross-origin iframes. Options: use a browser extension or proxy to strip the iframe, or switch to a tool that allows iframe manipulation (e.g., Puppeteer with page.frames()).
  • Banner uses shadow DOM: Write JavaScript that recursively traverses shadow roots and hides matching elements. Example: function hideInShadow(root) { root.querySelectorAll('*').forEach(el => { if (el.shadowRoot) hideInShadow(el.shadowRoot); }); var banner = root.querySelector('.cookie-consent'); if (banner) banner.style.display = 'none'; } hideInShadow(document);.
  • Layout breaks after hiding: Some banners reserve space via CSS (e.g., padding-top on body). You may need to override those styles too, or use visibility: hidden and height: 0 to collapse the element.
  • wkhtmltopdf crashes or hangs: Reduce --javascript-delay or disable JavaScript entirely if the banner is static. Also check for incompatible SSL certificates; use --no-check-certificate if necessary (only for trusted local testing).

Frequently Asked Questions

Can I remove cookie banners without modifying the page's HTML?

Yes, by using wkhtmltopdf's --user-style-sheet and --run-script options, you can inject CSS and JavaScript that hide the banner without altering the original HTML file.

Does wkhtmltopdf support modern JavaScript for hiding banners?

wkhtmltopdf uses an older WebKit engine, so support for modern JavaScript features (like ES6) is limited. Stick to ES5 syntax and avoid features like arrow functions or const/let if you encounter issues.

What if the cookie banner only appears after scrolling?

Some banners are triggered by scroll events. You can simulate a scroll in your JavaScript before hiding: window.scrollTo(0, document.body.scrollHeight); then hide the banner after a short delay.

Can I hide multiple different banners with one CSS file?

Yes, include multiple selectors in your CSS file, each targeting a different banner class or ID. Use commas to group them: .cookie-consent, #gdpr-banner, .privacy-notice { display: none !important; }.

Is there a way to automatically detect and hide any cookie banner?

No reliable universal selector exists because banners vary widely. Heuristics like hiding elements with text containing "cookie" are possible but fragile. Manual inspection per site is more dependable.

Why does the banner reappear in the PDF but not in the browser?

wkhtmltopdf may not execute all JavaScript or may render before scripts finish. Increase --javascript-delay and ensure your script runs on DOMContentLoaded or later. Also check for console errors using --debug-javascript.