同意弹窗和广告

从网上获取的页面很少单独出现:同意对话框盖在上面,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.