同意ダイアログと広告

Web から取得したページには、たいてい何かが付いてきます。同意ダイアログが上にかぶさり、Cookie バーが下にあり、段落の間には広告があります。既定では、Weblitz はブラウザーと同じようにすべて印刷します。二つのオプションでそれらを省けます。どちらも設定しない限りオフで、どちらも何もクリックしません。

WeblitzPdf 2.0.0-beta.4 以降。

同意ボックス

Consent = HtmlConsent.Hide にすると、同意ボックスを印刷から除きます。ボタンは押さず、読者の代わりに回答することもありません。ページとその同意管理プラットフォームはそのまま動作し、それらが表示する同意の部分は印刷されません。隠すのは次のものです:

  • ほとんどのサイトが使う同意管理プラットフォーム(OneTrust、Sourcepoint、Cookiebot、Didomi、Usercentrics、CookieYes など)のボックス。プラットフォーム自身のコンテナーで識別します。
  • EasyList Cookie List に挙げられているもの。EasyList の作者が管理する、数千のサイトの Cookie 通知のリストです。
  • ページ独自のボックス。ウィンドウに固定されたものやダイアログで、数行の長さで、Cookie、同意、プライバシーについて述べ、回答するためのボタンを持つもの。
  • そのようなボックスがページにかける覆いと、ページのスクロールにかけるロック。ロックがあると、印刷が初めのページだけになってしまいます。

隠したものは結果の注記(コード consentHidden)に示され、ページ独自のボックスはそれぞれセレクターの形で名前が示されます。どのルールにも当てはまらないボックスは残ります。Hide で指定してください。同意しないとコンテンツを表示しないサイトは、同意なしで表示する内容を印刷します。

広告とトラッカー

Ads = HtmlAds.Block にすると、EasyList と EasyPrivacy のフィルターリストに従い、広告ブロッカーで表示したとおりにページを印刷します:

  • リストがブロックするリクエストは送られません。リクエストが減り、実行するスクリプトも減ります。
  • リストが隠す要素は印刷されません。
  • リクエストが拒否されたフレームや画像は、空のボックスを残しません。

注記には省いたものの数が示されます(コード adsBlocked)。AdFilters で、リストの構文による独自のルールを一行に一つずつ追加できます。||ads.example.com^ はホストへのリクエストをブロックし、example.com##.sponsored はサイト上の要素を隠し、@@ と #@# は例外を作ります。広告をブロックする読者を拒否するサイトは、その読者に表示する内容を印刷します。

リストとその更新

フィルターリストは EasyList の作者たちの成果です(easylist.to)。パッケージには EasyList、EasyPrivacy、EasyList Cookie List のスナップショットが、アプリケーションの横の lists/ にプレーンなファイルとして、変更なしで含まれ、その告知とライセンスは lists/NOTICE.txt にあります。スナップショットはパッケージと同じだけ古く、リストは毎日変わります。

HtmlToPdf.UpdateFilterListsAsync() はその日のリストを取得し、以後それを使います。アプリケーションの起動時に呼び出してください:

var lists = await HtmlToPdf.UpdateFilterListsAsync();   // optional, at start

リストを取得するのはこれだけです。レンダリングが取得することはなく、バックグラウンドで動くものもありません。コピーはフォルダー(指定すれば独自のフォルダー)に保存され、以後の実行で再び読み込まれます。リストはそれぞれの更新間隔が過ぎるまで再取得されず、取得できなかったリストは前のコピーかスナップショットを使い続け、その理由が結果に示されます。

広告とトラッカーなしで

Ads = Block は、フィルターリスト EasyList と EasyPrivacy(EasyList の作者たちによるもので、パッケージにはそのスナップショットが含まれます)に従い、広告ブロッカーが表示するとおりにページを印刷します。リストがブロックするリクエストは送られず、リストが隠す要素は印刷されず、リクエストを拒否されたフレームは空の枠を残しません。AdFilters は同じ構文で独自のルールを追加します。ここではページのスポンサー枠に使っています。adsBlocked ノートは省いたものを数えます。ここでは Consent = Hide も併用しています。

リファレンス: HtmlPdfOptions.Ads、HtmlPdfOptions.AdFilters、HtmlToPdf.UpdateFilterListsAsync

印刷されたページ:news-page.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>The Daily Example</title>
<style>
  body { font: 15px/1.6 Georgia, serif; margin: 0; color: #111827; }
  header { padding: 14px 32px; border-bottom: 3px double #111827; font: bold 26px Georgia, serif; }
  main { display: flex; gap: 32px; padding: 20px 32px; }
  article { flex: 1; }
  h1 { font-size: 26px; line-height: 1.25; margin: 0 0 8px; }
  .byline { color: #6b7280; font: 12px Arial, sans-serif; margin-bottom: 12px; }
  aside { width: 200px; flex: none; }
  .slot { display: block; background: #e5e7eb; color: #6b7280; font: 11px Arial, sans-serif; text-align: center; }
  .sponsored { margin-top: 16px; padding: 10px; border: 1px solid #d1d5db; font: 12px Arial, sans-serif; }
  .sponsored b { display: block; color: #6b7280; text-transform: uppercase; font-size: 10px; }
  #veil { position: fixed; inset: 0; background: rgba(17, 24, 39, .55); }
  #consent { position: fixed; top: 150px; left: 50%; width: 420px; margin-left: -230px; padding: 20px; background: #fff;
    border-radius: 8px; font: 13px/1.5 Arial, sans-serif; box-shadow: 0 8px 30px rgba(0, 0, 0, .3); }
  #consent h2 { margin: 0 0 8px; font-size: 17px; }
  #consent button { margin: 12px 8px 0 0; padding: 8px 14px; border: 0; border-radius: 4px; background: #111827; color: #fff; }
  #cookie-bar { position: fixed; left: 0; right: 0; bottom: 0; padding: 12px 32px; background: #111827; color: #fff;
    font: 13px Arial, sans-serif; }
  #cookie-bar a { color: #fbbf24; }
</style></head>
<body>
<header>The Daily Example</header>
<ins class="adsbygoogle slot" data-ad-slot="1234567890" style="height: 90px; line-height: 90px; margin: 12px 32px;">Advertisement</ins>
<main>
  <article>
    <h1>City council approves the new tram line after a decade of debate</h1>
    <div class="byline">By a staff reporter · 17 November 2026</div>
    <p>The council voted on Tuesday evening to build the line that will link the harbour to the northern districts.
    Work is to start in the spring and to last four years, with the first trams running on the southern half two years
    earlier.</p>
    <p>Supporters called the vote overdue. The line was first proposed in 2016 and has been redrawn three times since,
    each time to spare a street its trees or a square its market.</p>
    <iframe class="slot" src="https://securepubads.g.doubleclick.net/gampad/adx?slot=inline" width="300" height="250" style="border: 0; margin: 8px 0;"></iframe>
    <p>Opponents say the cost, now put at 480 million, will keep rising. The mayor answered that the city could not
    afford another decade of buses stuck in the same traffic as the cars they were meant to replace.</p>
    <p>The first works will close the harbour road at night from March.</p>
  </article>
  <aside>
    <div class="sponsored"><b>Sponsored</b>Ten gadgets every commuter needs this winter</div>
    <div class="sponsored"><b>Sponsored</b>The savings account the banks keep quiet about</div>
  </aside>
</main>
<div id="veil" hidden></div>
<div id="consent" role="dialog" aria-modal="true" hidden>
  <h2>We value your privacy</h2>
  We and our 214 partners use cookies and similar technologies to store and read information on your device, to show
  you personalised advertising and to measure its audience.
  <div><button>Accept all</button><button>Reject all</button><button>Manage preferences</button></div>
</div>
<div id="cookie-bar" hidden>We use cookies to give you the best experience. <a href="#">Find out more</a> · <a href="#" role="button">OK, I agree</a></div>
<script src="https://www.google-analytics.com/analytics.js" async></script>
<script>
  // (as a consent platform does: its boxes appear once the page has loaded)
  window.addEventListener('load', function () {
    for (const id of ['veil', 'consent', 'cookie-bar']) document.getElementById(id).hidden = false;
  });
</script>
</body></html>
既定: ページそのまま
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
100,219 バイト(97.9 KB)· PDF
ノート
  • Info resourceRefused: Script https://www.google-analytics.com/analytics.js: no resource loader was given (only data: URLs load).
  • Info resourceRefused: Frame https://securepubads.g.doubleclick.net/gampad/adx?slot=inline: no resource loader was given (only data: URLs load).
  • Info scriptsRan: The page's scripts ran (17 ms of virtual time, 2 tasks; 0 canvases drawn).
Ads = Block、独自のルール付き
var result = HtmlToPdf.Render(html, new HtmlPdfOptions
{
    Ads = HtmlAds.Block,
    AdFilters = ["##.sponsored"],
    Consent = HtmlConsent.Hide,
});
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
52,292 バイト(51.1 KB)· PDF
ノート
  • Info consentHidden: Consent boxes are not printed (Consent = Hide): the page's own div#consent, the page's own div#cookie-bar.
  • Info scriptsRan: The page's scripts ran (33 ms of virtual time, 6 tasks; 0 canvases drawn).
  • Info adsBlocked: Advertisements and trackers are left out (Ads = Block): 2 requests not made, 168 of the lists' hiding rules given to the page.