すべてのオプションの出力例

出力を変える各オプションについて、小さなページ、そのオプションを設定する C#、Weblitz が印刷したページを示します。違いがわかる場合は既定値と並べています。2026-11-23 に macOS で作成。ページは PDF から描画しており、PDF はダウンロードできます。エンジンが残した注記は、まだ対応していない点も含め、返ってきたまま (英語で) 表示しています。

用紙サイズと向き

ページ領域は用紙によって変わります。破線の枠が領域の端です。

リファレンス: HtmlPdfOptions.Format、HtmlPdfOptions.Landscape、HtmlPdfOptions.Width、HtmlPdfOptions.Height

印刷されたページ:article.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Field report: the Rhône sensors</title>
<style>
  body { font: 11pt/1.5 Arial, sans-serif; color: #1f2937; margin: 0; }
  .frame { border: 2px dashed #ff5a1f; padding: 6mm; }
  h1 { font-size: 22pt; margin: 0 0 3mm; color: #0b1020; }
  h2 { font-size: 14pt; margin: 6mm 0 2mm; color: #0b1020; }
  .band { background: linear-gradient(90deg, #0b1020, #3b0764); color: #fff; padding: 4mm 6mm; border-radius: 3mm; }
  .note { background: #fff7ed; border-left: 4px solid #ff5a1f; padding: 3mm 4mm; }
  table { border-collapse: collapse; width: 100%; margin-top: 3mm; }
  th, td { border: 1px solid #d1d5db; padding: 1.5mm 2.5mm; text-align: left; }
  th { background: #e0e7ff; }
  .page-break { break-before: page; }
</style></head>
<body>
<div class="frame">
  <div class="band"><h1 style="color:#fff">Field report: the Rhône sensors</h1>Page one of five: the dashed line is the page area's edge.</div>
  <h2>1. Where the sensors are</h2>
  <p>Forty-two stations along the river send a reading every ten minutes. The report below follows them from the lake to the sea, one section a page, so that the options that choose pages, sizes and margins show what they do.</p>
  <p class="note">A coloured band and a tinted note: printed with <code>PrintBackground</code> on, left white without it.</p>
  <table><tr><th>Station</th><th>Kilometre</th><th>Level (m)</th></tr>
    <tr><td>Genève</td><td>0</td><td>372.1</td></tr><tr><td>Seyssel</td><td>48</td><td>254.6</td></tr><tr><td>Lyon</td><td>232</td><td>162.3</td></tr></table>
</div>
<div class="frame page-break"><div class="band">Page two of five</div><h2>2. The upper river</h2><p>The upper river is fast and cold: its stations read the level every ten minutes and the temperature every hour.</p></div>
<div class="frame page-break"><div class="band">Page three of five</div><h2>3. Lyon</h2><p>At Lyon the Saône joins: the station reads both rivers.</p></div>
<div class="frame page-break"><div class="band">Page four of five</div><h2>4. The lower river</h2><p>Dams turn the lower river into a staircase of reaches.</p></div>
<div class="frame page-break"><div class="band">Page five of five</div><h2>5. The delta</h2><p>In the Camargue the river splits in two before the sea.</p></div>
</body></html>
既定: A4、縦向き
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目
ページ
5
ファイル
95,383 バイト(93.1 KB)· PDF
Letter、横向き
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Format = "Letter", Landscape = true });
このコードで印刷した PDF の 1 ページ目
ページ
5
ファイル
99,598 バイト(97.3 KB)· PDF
幅と高さ: 120 x 160 mm
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Width = "120mm", Height = "160mm" });
このコードで印刷した PDF の 1 ページ目
ページ
6
ファイル
87,157 バイト(85.1 KB)· PDF

余白

オプションにもページの @page ルールにも余白がなければ、Chrome の既定の余白が適用されます。

リファレンス: HtmlPdfOptions.Margin、HtmlMargins

印刷されたページ:article.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Field report: the Rhône sensors</title>
<style>
  body { font: 11pt/1.5 Arial, sans-serif; color: #1f2937; margin: 0; }
  .frame { border: 2px dashed #ff5a1f; padding: 6mm; }
  h1 { font-size: 22pt; margin: 0 0 3mm; color: #0b1020; }
  h2 { font-size: 14pt; margin: 6mm 0 2mm; color: #0b1020; }
  .band { background: linear-gradient(90deg, #0b1020, #3b0764); color: #fff; padding: 4mm 6mm; border-radius: 3mm; }
  .note { background: #fff7ed; border-left: 4px solid #ff5a1f; padding: 3mm 4mm; }
  table { border-collapse: collapse; width: 100%; margin-top: 3mm; }
  th, td { border: 1px solid #d1d5db; padding: 1.5mm 2.5mm; text-align: left; }
  th { background: #e0e7ff; }
  .page-break { break-before: page; }
</style></head>
<body>
<div class="frame">
  <div class="band"><h1 style="color:#fff">Field report: the Rhône sensors</h1>Page one of five: the dashed line is the page area's edge.</div>
  <h2>1. Where the sensors are</h2>
  <p>Forty-two stations along the river send a reading every ten minutes. The report below follows them from the lake to the sea, one section a page, so that the options that choose pages, sizes and margins show what they do.</p>
  <p class="note">A coloured band and a tinted note: printed with <code>PrintBackground</code> on, left white without it.</p>
  <table><tr><th>Station</th><th>Kilometre</th><th>Level (m)</th></tr>
    <tr><td>Genève</td><td>0</td><td>372.1</td></tr><tr><td>Seyssel</td><td>48</td><td>254.6</td></tr><tr><td>Lyon</td><td>232</td><td>162.3</td></tr></table>
</div>
<div class="frame page-break"><div class="band">Page two of five</div><h2>2. The upper river</h2><p>The upper river is fast and cold: its stations read the level every ten minutes and the temperature every hour.</p></div>
<div class="frame page-break"><div class="band">Page three of five</div><h2>3. Lyon</h2><p>At Lyon the Saône joins: the station reads both rivers.</p></div>
<div class="frame page-break"><div class="band">Page four of five</div><h2>4. The lower river</h2><p>Dams turn the lower river into a staircase of reaches.</p></div>
<div class="frame page-break"><div class="band">Page five of five</div><h2>5. The delta</h2><p>In the Camargue the river splits in two before the sea.</p></div>
</body></html>
既定
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目
ページ
5
ファイル
95,383 バイト(93.1 KB)· PDF
上下左右とも 25 mm
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Margin = HtmlMargins.All("25mm") });
このコードで印刷した PDF の 1 ページ目
ページ
5
ファイル
91,048 バイト(88.9 KB)· PDF
上、右、下、左
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Margin = new HtmlMargins("10mm", "45mm", "10mm", "10mm") });
このコードで印刷した PDF の 1 ページ目
ページ
5
ファイル
91,538 バイト(89.4 KB)· PDF

拡大縮小

用紙が大きい、または小さいものとしてページをレイアウトし、その用紙に印刷します: 0.1 から 2。

リファレンス: HtmlPdfOptions.Scale

印刷されたページ:article.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Field report: the Rhône sensors</title>
<style>
  body { font: 11pt/1.5 Arial, sans-serif; color: #1f2937; margin: 0; }
  .frame { border: 2px dashed #ff5a1f; padding: 6mm; }
  h1 { font-size: 22pt; margin: 0 0 3mm; color: #0b1020; }
  h2 { font-size: 14pt; margin: 6mm 0 2mm; color: #0b1020; }
  .band { background: linear-gradient(90deg, #0b1020, #3b0764); color: #fff; padding: 4mm 6mm; border-radius: 3mm; }
  .note { background: #fff7ed; border-left: 4px solid #ff5a1f; padding: 3mm 4mm; }
  table { border-collapse: collapse; width: 100%; margin-top: 3mm; }
  th, td { border: 1px solid #d1d5db; padding: 1.5mm 2.5mm; text-align: left; }
  th { background: #e0e7ff; }
  .page-break { break-before: page; }
</style></head>
<body>
<div class="frame">
  <div class="band"><h1 style="color:#fff">Field report: the Rhône sensors</h1>Page one of five: the dashed line is the page area's edge.</div>
  <h2>1. Where the sensors are</h2>
  <p>Forty-two stations along the river send a reading every ten minutes. The report below follows them from the lake to the sea, one section a page, so that the options that choose pages, sizes and margins show what they do.</p>
  <p class="note">A coloured band and a tinted note: printed with <code>PrintBackground</code> on, left white without it.</p>
  <table><tr><th>Station</th><th>Kilometre</th><th>Level (m)</th></tr>
    <tr><td>Genève</td><td>0</td><td>372.1</td></tr><tr><td>Seyssel</td><td>48</td><td>254.6</td></tr><tr><td>Lyon</td><td>232</td><td>162.3</td></tr></table>
</div>
<div class="frame page-break"><div class="band">Page two of five</div><h2>2. The upper river</h2><p>The upper river is fast and cold: its stations read the level every ten minutes and the temperature every hour.</p></div>
<div class="frame page-break"><div class="band">Page three of five</div><h2>3. Lyon</h2><p>At Lyon the Saône joins: the station reads both rivers.</p></div>
<div class="frame page-break"><div class="band">Page four of five</div><h2>4. The lower river</h2><p>Dams turn the lower river into a staircase of reaches.</p></div>
<div class="frame page-break"><div class="band">Page five of five</div><h2>5. The delta</h2><p>In the Camargue the river splits in two before the sea.</p></div>
</body></html>
1 (既定)
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目
ページ
5
ファイル
95,383 バイト(93.1 KB)· PDF
0.6
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Scale = 0.6 });
このコードで印刷した PDF の 1 ページ目
ページ
5
ファイル
102,606 バイト(100.2 KB)· PDF
1.4
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Scale = 1.4 });
このコードで印刷した PDF の 1 ページ目
ページ
5
ファイル
92,984 バイト(90.8 KB)· PDF

ページの選択

記事は全五ページで、PDF には指定したページが入ります。

リファレンス: HtmlPdfOptions.PageRanges

印刷されたページ:article.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Field report: the Rhône sensors</title>
<style>
  body { font: 11pt/1.5 Arial, sans-serif; color: #1f2937; margin: 0; }
  .frame { border: 2px dashed #ff5a1f; padding: 6mm; }
  h1 { font-size: 22pt; margin: 0 0 3mm; color: #0b1020; }
  h2 { font-size: 14pt; margin: 6mm 0 2mm; color: #0b1020; }
  .band { background: linear-gradient(90deg, #0b1020, #3b0764); color: #fff; padding: 4mm 6mm; border-radius: 3mm; }
  .note { background: #fff7ed; border-left: 4px solid #ff5a1f; padding: 3mm 4mm; }
  table { border-collapse: collapse; width: 100%; margin-top: 3mm; }
  th, td { border: 1px solid #d1d5db; padding: 1.5mm 2.5mm; text-align: left; }
  th { background: #e0e7ff; }
  .page-break { break-before: page; }
</style></head>
<body>
<div class="frame">
  <div class="band"><h1 style="color:#fff">Field report: the Rhône sensors</h1>Page one of five: the dashed line is the page area's edge.</div>
  <h2>1. Where the sensors are</h2>
  <p>Forty-two stations along the river send a reading every ten minutes. The report below follows them from the lake to the sea, one section a page, so that the options that choose pages, sizes and margins show what they do.</p>
  <p class="note">A coloured band and a tinted note: printed with <code>PrintBackground</code> on, left white without it.</p>
  <table><tr><th>Station</th><th>Kilometre</th><th>Level (m)</th></tr>
    <tr><td>Genève</td><td>0</td><td>372.1</td></tr><tr><td>Seyssel</td><td>48</td><td>254.6</td></tr><tr><td>Lyon</td><td>232</td><td>162.3</td></tr></table>
</div>
<div class="frame page-break"><div class="band">Page two of five</div><h2>2. The upper river</h2><p>The upper river is fast and cold: its stations read the level every ten minutes and the temperature every hour.</p></div>
<div class="frame page-break"><div class="band">Page three of five</div><h2>3. Lyon</h2><p>At Lyon the Saône joins: the station reads both rivers.</p></div>
<div class="frame page-break"><div class="band">Page four of five</div><h2>4. The lower river</h2><p>Dams turn the lower river into a staircase of reaches.</p></div>
<div class="frame page-break"><div class="band">Page five of five</div><h2>5. The delta</h2><p>In the Camargue the river splits in two before the sea.</p></div>
</body></html>
"2-3, 5"
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { PageRanges = "2-3, 5" });
このコードで印刷した PDF の 1 ページ目 このコードで印刷した PDF の 2 ページ目 このコードで印刷した PDF の 3 ページ目
ページ
3
ファイル
59,266 バイト(57.9 KB)· PDF

ページ自身の @page サイズ

用紙サイズを指定するページには、既定でそのサイズが使われます。PreferCssPageSize が false のときは、オプションの Format が優先されます。

リファレンス: HtmlPdfOptions.PreferCssPageSize

印刷されたページ:css-page-size.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>A card of its own size</title>
<style>
  @page { size: A5 landscape; margin: 10mm; }
  body { font: 12pt/1.4 Arial, sans-serif; margin: 0; }
  .card { border: 2px solid #0b1020; border-radius: 4mm; padding: 6mm; background: #f0fdfa; }
  h1 { margin: 0 0 2mm; font-size: 18pt; }
</style></head>
<body><div class="card"><h1>Admission: Hall 4</h1>This page asks for <code>@page { size: A5 landscape }</code>.</div></body></html>
true (既定): ページの指定どおり A5 横向き
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
39,665 バイト(38.7 KB)· PDF
false: オプションの A4
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { PreferCssPageSize = false, Format = "A4" });
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
39,680 バイト(38.8 KB)· PDF

スクリプトと仮想時計

スクリプトは印刷前に一度実行されます。タイマーは WaitFor の間、仮想時間で進む (既定は 5 秒) ので、ページの 2 秒のタイマーは発火し、8 秒のタイマーは待ち時間を長くしたときだけ発火します。beforeprint は PDF を作る直前に実行されます。

リファレンス: HtmlPdfOptions.Scripts、HtmlScripts

印刷されたページ:scripts.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Scripts</title>
<style>body { font: 13pt/1.5 Arial, sans-serif; margin: 0; } li { margin: 2mm 0; } .js { color: #047857; font-weight: bold; }</style></head>
<body>
<h1>What the page's scripts did</h1>
<ul id="log"><li>Written in the HTML.</li></ul>
<script>
  const log = (text) => { const li = document.createElement('li'); li.className = 'js'; li.textContent = text; document.getElementById('log').append(li); };
  log('Added by the script as the page loaded.');
  setTimeout(() => log('Added by a timer after 2 seconds (virtual time).'), 2000);
  setTimeout(() => log('Added by a timer after 8 seconds (virtual time).'), 8000);
  addEventListener('beforeprint', () => log('Added on beforeprint, just before printing.'));
</script>
</body></html>
既定: スクリプト有効、仮想時間 5 秒
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
36,218 バイト(35.4 KB)· PDF
ノート
  • Info scriptsRan: The page's scripts ran (2017 ms of virtual time, 5 tasks; 0 canvases drawn).
WaitFor 10 s
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Scripts = HtmlScripts.Default with { WaitFor = TimeSpan.FromSeconds(10) } });
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
37,010 バイト(36.1 KB)· PDF
ノート
  • Info scriptsRan: The page's scripts ran (8017 ms of virtual time, 8 tasks; 0 canvases drawn).
スクリプト無効
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Scripts = HtmlScripts.Off });
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
30,676 バイト(30.0 KB)· PDF

ネットワーク

ネットワークは既定でオフです。ページの Web アドレスは取得されず、代わりに注記に記録されます。ページが読み込んでよいホストを許可してください (assets は、生成ツールがこのマシン上で起動するサーバーです)。

リファレンス: HtmlPdfOptions.Network、HtmlNetwork、HtmlPdfOptions.BaseUrl

印刷されたページ:network.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Network</title>
<link rel="stylesheet" href="brand.css">
<style>body { font: 12pt/1.5 Arial, sans-serif; margin: 0; } img { width: 40mm; display: block; margin: 4mm 0; }</style></head>
<body>
<h1 class="brand">A page whose picture and CSS are on a server</h1>
<img src="logo.svg" alt="The logo, fetched from the server">
<p>The heading's colour comes from <code>brand.css</code> and the picture from <code>logo.svg</code>, both fetched over HTTP.</p>
</body></html>
オフ (既定)
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { BaseUrl = assets });
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
44,119 バイト(43.1 KB)· PDF
ノート
  • Info resourceRefused: StyleSheet http://127.0.0.1:18431/brand.css: no resource loader was given (only data: URLs load).
  • Info resourceRefused: Image http://127.0.0.1:18431/logo.svg: no resource loader was given (only data: URLs load).
  • Info notImplemented: A picture that did not load shows its alternative text; Chrome's broken-image icon beside it is laid out but not drawn.
アセットのホストを許可
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { BaseUrl = assets, Network = HtmlNetwork.Allow(assets.Host) });
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
45,952 バイト(44.9 KB)· PDF

ログインが必要なページ

サーバーがサインインしたユーザーにだけ表示するページは、ブラウザーが送るものを付けて印刷されます: bearer トークン、セッション Cookie、または HTTP 認証のパスワードです。いずれも指定したホストにだけ送られ、既定では文書自身のホストです (assets は生成ツールがこのマシン上で起動するサーバーで、そのアカウントページにはこれらのいずれかが必要です)。

リファレンス: HtmlNetwork.Headers、HtmlNetwork.Cookies、HtmlNetwork.AcceptCookies、HtmlNetwork.Credentials、HtmlCookie

印刷されたページ:assets/account/statement.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Account statement</title>
<link rel="stylesheet" href="../brand.css">
<style>
  body { font: 11pt/1.5 Arial, sans-serif; margin: 0; }
  table { border-collapse: collapse; width: 100%; margin-top: 6mm; }
  th, td { border-bottom: 1px solid #d0d4dc; padding: 2mm 3mm; text-align: left; }
  td.n { text-align: right; font-variant-numeric: tabular-nums; }
  .who { color: #555; }
</style></head>
<body>
<h1 class="brand">Account statement, October 2026</h1>
<p class="who">Signed in as <strong id="user">a customer</strong>: this page is served only with a valid session.</p>
<table>
  <tr><th>Date</th><th>Description</th><th class="n">Amount</th></tr>
  <tr><td>2026-10-03</td><td>Subscription, October</td><td class="n">-49.00</td></tr>
  <tr><td>2026-10-14</td><td>Refund, order 1182</td><td class="n">12.50</td></tr>
  <tr><td>2026-10-28</td><td>Top-up</td><td class="n">200.00</td></tr>
</table>
<script>
  // what the page's own scripts see of the session (an HttpOnly cookie never shows here)
  const theme = document.cookie.split('; ').find(c => c.startsWith('theme='));
  if (theme) document.getElementById('user').textContent += ' (theme ' + theme.slice(6) + ')';
</script>
</body></html>
bearer トークン
var result = HtmlToPdf.RenderUrl(new Uri(assets, "account/statement.html"), new HtmlPdfOptions
{
    Network = HtmlNetwork.Allow(assets.Host).WithBearerToken(accessToken),
});
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
43,239 バイト(42.2 KB)· PDF
ノート
  • Info scriptsRan: The page's scripts ran (17 ms of virtual time, 2 tasks; 0 canvases drawn).
セッション Cookie
var result = HtmlToPdf.RenderUrl(new Uri(assets, "account/statement.html"), new HtmlPdfOptions
{
    Network = HtmlNetwork.Allow(assets.Host)
        .WithCookie(new HtmlCookie("session", sessionId) { HttpOnly = true })
        .WithCookie(new HtmlCookie("theme", "dark")),
});
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
44,446 バイト(43.4 KB)· PDF
ノート
  • Info scriptsRan: The page's scripts ran (17 ms of virtual time, 2 tasks; 0 canvases drawn).

PDF の圧縮

同じページを、圧縮の強さを変えて出力したものです。下のファイルサイズは、これらのページを作成したときに測定しました。

リファレンス: HtmlPdfOptions.Compression、HtmlPdfCompression

印刷されたページ:article.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Field report: the Rhône sensors</title>
<style>
  body { font: 11pt/1.5 Arial, sans-serif; color: #1f2937; margin: 0; }
  .frame { border: 2px dashed #ff5a1f; padding: 6mm; }
  h1 { font-size: 22pt; margin: 0 0 3mm; color: #0b1020; }
  h2 { font-size: 14pt; margin: 6mm 0 2mm; color: #0b1020; }
  .band { background: linear-gradient(90deg, #0b1020, #3b0764); color: #fff; padding: 4mm 6mm; border-radius: 3mm; }
  .note { background: #fff7ed; border-left: 4px solid #ff5a1f; padding: 3mm 4mm; }
  table { border-collapse: collapse; width: 100%; margin-top: 3mm; }
  th, td { border: 1px solid #d1d5db; padding: 1.5mm 2.5mm; text-align: left; }
  th { background: #e0e7ff; }
  .page-break { break-before: page; }
</style></head>
<body>
<div class="frame">
  <div class="band"><h1 style="color:#fff">Field report: the Rhône sensors</h1>Page one of five: the dashed line is the page area's edge.</div>
  <h2>1. Where the sensors are</h2>
  <p>Forty-two stations along the river send a reading every ten minutes. The report below follows them from the lake to the sea, one section a page, so that the options that choose pages, sizes and margins show what they do.</p>
  <p class="note">A coloured band and a tinted note: printed with <code>PrintBackground</code> on, left white without it.</p>
  <table><tr><th>Station</th><th>Kilometre</th><th>Level (m)</th></tr>
    <tr><td>Genève</td><td>0</td><td>372.1</td></tr><tr><td>Seyssel</td><td>48</td><td>254.6</td></tr><tr><td>Lyon</td><td>232</td><td>162.3</td></tr></table>
</div>
<div class="frame page-break"><div class="band">Page two of five</div><h2>2. The upper river</h2><p>The upper river is fast and cold: its stations read the level every ten minutes and the temperature every hour.</p></div>
<div class="frame page-break"><div class="band">Page three of five</div><h2>3. Lyon</h2><p>At Lyon the Saône joins: the station reads both rivers.</p></div>
<div class="frame page-break"><div class="band">Page four of five</div><h2>4. The lower river</h2><p>Dams turn the lower river into a staircase of reaches.</p></div>
<div class="frame page-break"><div class="band">Page five of five</div><h2>5. The delta</h2><p>In the Camargue the river splits in two before the sea.</p></div>
</body></html>
None
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Compression = HtmlPdfCompression.None });
ページ
5
ファイル
345,843 バイト(337.7 KB)· PDF
Fastest (既定)
var result = HtmlToPdf.Render(html);
ページ
5
ファイル
95,383 バイト(93.1 KB)· PDF
既定
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Compression = HtmlPdfCompression.Default });
ページ
5
ファイル
74,765 バイト(73.0 KB)· PDF
Smallest
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Compression = HtmlPdfCompression.Smallest });
ページ
5
ファイル
72,832 バイト(71.1 KB)· PDF

パスワードと権限

PDF は暗号化されています (AES-256)。リーダーはユーザーパスワードを求め、所有者パスワードがなければコピーできないことがあります (ここではそうです)。

リファレンス: HtmlPdfOptions.Security、HtmlPdfSecurity

印刷されたページ:article.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Field report: the Rhône sensors</title>
<style>
  body { font: 11pt/1.5 Arial, sans-serif; color: #1f2937; margin: 0; }
  .frame { border: 2px dashed #ff5a1f; padding: 6mm; }
  h1 { font-size: 22pt; margin: 0 0 3mm; color: #0b1020; }
  h2 { font-size: 14pt; margin: 6mm 0 2mm; color: #0b1020; }
  .band { background: linear-gradient(90deg, #0b1020, #3b0764); color: #fff; padding: 4mm 6mm; border-radius: 3mm; }
  .note { background: #fff7ed; border-left: 4px solid #ff5a1f; padding: 3mm 4mm; }
  table { border-collapse: collapse; width: 100%; margin-top: 3mm; }
  th, td { border: 1px solid #d1d5db; padding: 1.5mm 2.5mm; text-align: left; }
  th { background: #e0e7ff; }
  .page-break { break-before: page; }
</style></head>
<body>
<div class="frame">
  <div class="band"><h1 style="color:#fff">Field report: the Rhône sensors</h1>Page one of five: the dashed line is the page area's edge.</div>
  <h2>1. Where the sensors are</h2>
  <p>Forty-two stations along the river send a reading every ten minutes. The report below follows them from the lake to the sea, one section a page, so that the options that choose pages, sizes and margins show what they do.</p>
  <p class="note">A coloured band and a tinted note: printed with <code>PrintBackground</code> on, left white without it.</p>
  <table><tr><th>Station</th><th>Kilometre</th><th>Level (m)</th></tr>
    <tr><td>Genève</td><td>0</td><td>372.1</td></tr><tr><td>Seyssel</td><td>48</td><td>254.6</td></tr><tr><td>Lyon</td><td>232</td><td>162.3</td></tr></table>
</div>
<div class="frame page-break"><div class="band">Page two of five</div><h2>2. The upper river</h2><p>The upper river is fast and cold: its stations read the level every ten minutes and the temperature every hour.</p></div>
<div class="frame page-break"><div class="band">Page three of five</div><h2>3. Lyon</h2><p>At Lyon the Saône joins: the station reads both rivers.</p></div>
<div class="frame page-break"><div class="band">Page four of five</div><h2>4. The lower river</h2><p>Dams turn the lower river into a staircase of reaches.</p></div>
<div class="frame page-break"><div class="band">Page five of five</div><h2>5. The delta</h2><p>In the Camargue the river splits in two before the sea.</p></div>
</body></html>
ユーザーパスワード、コピー不可
var result = HtmlToPdf.Render(html, new HtmlPdfOptions
{
    Security = new HtmlPdfSecurity { UserPassword = "open-me", OwnerPassword = "owner-secret", PermitCopying = false },
});
ページ
5
ファイル
98,636 バイト (96.3 KB)、暗号化 · PDF

タイトル、作成者、PDF/A

PDF が自らについて記す情報。アーカイブには PDF/A-2b。

リファレンス: HtmlPdfOptions.Title、HtmlPdfOptions.Author、HtmlPdfOptions.PdfA

印刷されたページ:article.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Field report: the Rhône sensors</title>
<style>
  body { font: 11pt/1.5 Arial, sans-serif; color: #1f2937; margin: 0; }
  .frame { border: 2px dashed #ff5a1f; padding: 6mm; }
  h1 { font-size: 22pt; margin: 0 0 3mm; color: #0b1020; }
  h2 { font-size: 14pt; margin: 6mm 0 2mm; color: #0b1020; }
  .band { background: linear-gradient(90deg, #0b1020, #3b0764); color: #fff; padding: 4mm 6mm; border-radius: 3mm; }
  .note { background: #fff7ed; border-left: 4px solid #ff5a1f; padding: 3mm 4mm; }
  table { border-collapse: collapse; width: 100%; margin-top: 3mm; }
  th, td { border: 1px solid #d1d5db; padding: 1.5mm 2.5mm; text-align: left; }
  th { background: #e0e7ff; }
  .page-break { break-before: page; }
</style></head>
<body>
<div class="frame">
  <div class="band"><h1 style="color:#fff">Field report: the Rhône sensors</h1>Page one of five: the dashed line is the page area's edge.</div>
  <h2>1. Where the sensors are</h2>
  <p>Forty-two stations along the river send a reading every ten minutes. The report below follows them from the lake to the sea, one section a page, so that the options that choose pages, sizes and margins show what they do.</p>
  <p class="note">A coloured band and a tinted note: printed with <code>PrintBackground</code> on, left white without it.</p>
  <table><tr><th>Station</th><th>Kilometre</th><th>Level (m)</th></tr>
    <tr><td>Genève</td><td>0</td><td>372.1</td></tr><tr><td>Seyssel</td><td>48</td><td>254.6</td></tr><tr><td>Lyon</td><td>232</td><td>162.3</td></tr></table>
</div>
<div class="frame page-break"><div class="band">Page two of five</div><h2>2. The upper river</h2><p>The upper river is fast and cold: its stations read the level every ten minutes and the temperature every hour.</p></div>
<div class="frame page-break"><div class="band">Page three of five</div><h2>3. Lyon</h2><p>At Lyon the Saône joins: the station reads both rivers.</p></div>
<div class="frame page-break"><div class="band">Page four of five</div><h2>4. The lower river</h2><p>Dams turn the lower river into a staircase of reaches.</p></div>
<div class="frame page-break"><div class="band">Page five of five</div><h2>5. The delta</h2><p>In the Camargue the river splits in two before the sea.</p></div>
</body></html>
タイトル、作成者、PDF/A
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Title = "Rhône field report", Author = "Hydrology team", PdfA = true });
ページ
5
ファイル
99,689 バイト (97.4 KB)、PDF/A · PDF

制限

制限をかけたページです。ここでは最大二ページ。レンダリングはそこで止まり、できた分を印刷して、その旨を伝えます。Completed は false になり、注記がどの制限かを示します。

リファレンス: HtmlPdfOptions.Limits、HtmlPdfLimits、HtmlPdfResult.Completed

印刷されたページ:article.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Field report: the Rhône sensors</title>
<style>
  body { font: 11pt/1.5 Arial, sans-serif; color: #1f2937; margin: 0; }
  .frame { border: 2px dashed #ff5a1f; padding: 6mm; }
  h1 { font-size: 22pt; margin: 0 0 3mm; color: #0b1020; }
  h2 { font-size: 14pt; margin: 6mm 0 2mm; color: #0b1020; }
  .band { background: linear-gradient(90deg, #0b1020, #3b0764); color: #fff; padding: 4mm 6mm; border-radius: 3mm; }
  .note { background: #fff7ed; border-left: 4px solid #ff5a1f; padding: 3mm 4mm; }
  table { border-collapse: collapse; width: 100%; margin-top: 3mm; }
  th, td { border: 1px solid #d1d5db; padding: 1.5mm 2.5mm; text-align: left; }
  th { background: #e0e7ff; }
  .page-break { break-before: page; }
</style></head>
<body>
<div class="frame">
  <div class="band"><h1 style="color:#fff">Field report: the Rhône sensors</h1>Page one of five: the dashed line is the page area's edge.</div>
  <h2>1. Where the sensors are</h2>
  <p>Forty-two stations along the river send a reading every ten minutes. The report below follows them from the lake to the sea, one section a page, so that the options that choose pages, sizes and margins show what they do.</p>
  <p class="note">A coloured band and a tinted note: printed with <code>PrintBackground</code> on, left white without it.</p>
  <table><tr><th>Station</th><th>Kilometre</th><th>Level (m)</th></tr>
    <tr><td>Genève</td><td>0</td><td>372.1</td></tr><tr><td>Seyssel</td><td>48</td><td>254.6</td></tr><tr><td>Lyon</td><td>232</td><td>162.3</td></tr></table>
</div>
<div class="frame page-break"><div class="band">Page two of five</div><h2>2. The upper river</h2><p>The upper river is fast and cold: its stations read the level every ten minutes and the temperature every hour.</p></div>
<div class="frame page-break"><div class="band">Page three of five</div><h2>3. Lyon</h2><p>At Lyon the Saône joins: the station reads both rivers.</p></div>
<div class="frame page-break"><div class="band">Page four of five</div><h2>4. The lower river</h2><p>Dams turn the lower river into a staircase of reaches.</p></div>
<div class="frame page-break"><div class="band">Page five of five</div><h2>5. The delta</h2><p>In the Camargue the river splits in two before the sea.</p></div>
</body></html>
MaxPages = 2
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Limits = HtmlPdfLimits.Default with { MaxPages = 2 } });
Console.WriteLine($"{result.PageCount} pages, completed: {result.Completed}");
このコードで印刷した PDF の 1 ページ目 このコードで印刷した PDF の 2 ページ目
ページ
2、途中で停止 (Completed false)
ファイル
74,993 バイト(73.2 KB)· PDF
ノート
  • Warning limitReached: the document was cut after 2 pages.

ノート: できなかったこと

ページ自体の問題で例外が送出されることはありません。画像がない、ホストが許可されていない、マシンにないフォント、例外を送出するスクリプト。いずれも注記になります。

リファレンス: HtmlPdfResult.Notes、HtmlPdfNote、HtmlPdfNoteSeverity

印刷されたページ:notes.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Notes</title>
<style>body { font: 12pt/1.5 Arial, sans-serif; margin: 0; } .odd { font-family: "Some Font Nobody Has", serif; }</style></head>
<body>
<h1>A page with problems</h1>
<p><img src="missing-picture.png" alt="A picture that is not there" width="120" height="60"></p>
<p class="odd">Text in a font family the machine does not have.</p>
<p><img src="https://cdn.example.com/banner.png" alt="A picture on another host" width="120" height="60"></p>
<script>
  document.body.append('The script ran up to here; ');
  undefinedFunction();
</script>
</body></html>
問題のあるページ
var result = HtmlToPdf.Render(html);
foreach (var note in result.Notes)
    Console.WriteLine($"{note.Severity} {note.Code}: {note.Message}");
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
56,265 バイト(54.9 KB)· PDF
ノート
  • Warning scriptError: ReferenceError: undefinedFunction is not defined | at about:blank#script-25:11:3 | at HostDelegate.<anonymous> (<anonymous>) | at Object.hostCall (ssrs2-dom.js:15562:11)
  • Warning resourceFailed: Image "missing-picture.png" is not a URL that resolves against no base URL.
  • Info resourceRefused: Image https://cdn.example.com/banner.png: no resource loader was given (only data: URLs load).
  • Info scriptsRan: The page's scripts ran (17 ms of virtual time, 4 tasks; 0 canvases drawn).
  • Info notImplemented: A picture that did not load shows its alternative text; Chrome's broken-image icon beside it is laid out but not drawn.
  • Info fontFamilyMissing: The font family "Some Font Nobody Has" is not on this machine: its text goes to the next family.

ページから見えるブラウザー

既定では、ページからはマシン自身の OS 上で動くデスクトップ版 Chrome 156 に見えます。これらのページは macOS で作成しました。

リファレンス: HtmlPdfOptions.UserAgent、HtmlPdfOptions.UserAgentMetadata、HtmlUserAgentMetadata、HtmlUserAgentBrand

印刷されたページ:user-agent.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Who am I?</title>
<style>body { font: 11pt/1.5 Arial, sans-serif; margin: 0; } dt { font-weight: bold; margin-top: 3mm; } dd { margin: 0; font-family: "Courier New", monospace; font-size: 10pt; }</style></head>
<body>
<h1>What the page sees</h1>
<dl id="out"></dl>
<script>
  const out = document.getElementById('out');
  const show = (name, value) => { const dt = document.createElement('dt'); dt.textContent = name; const dd = document.createElement('dd'); dd.textContent = value; out.append(dt, dd); };
  show('navigator.userAgent', navigator.userAgent);
  show('navigator.platform', navigator.platform);
  const data = navigator.userAgentData;
  show('navigator.userAgentData.brands', data ? data.brands.map(b => b.brand + ' ' + b.version).join(', ') : '(none)');
  show('navigator.userAgentData.platform', data ? data.platform : '(none)');
</script>
</body></html>
既定
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
44,873 バイト(43.8 KB)· PDF
ノート
  • Info scriptsRan: The page's scripts ran (17 ms of virtual time, 2 tasks; 0 canvases drawn).
User-Agent とそのメタデータ
var result = HtmlToPdf.Render(html, new HtmlPdfOptions
{
    UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36 InvoiceBot/2.1",
    UserAgentMetadata = HtmlUserAgentMetadata.ChromeOnThisMachine with
    {
        Platform = "Windows",
        Brands = [new("InvoiceBot", "2"), new("Chromium", "156")],
    },
});
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
45,030 バイト(44.0 KB)· PDF
ノート
  • Info scriptsRan: The page's scripts ran (17 ms of virtual time, 2 tasks; 0 canvases drawn).

ロケール、タイムゾーン、時計

ページのスクリプトから見える外の世界です。毎回同じ値なので、同じ入力からは同じ PDF ができます。

リファレンス: HtmlPdfOptions.Locale、HtmlPdfOptions.TimeZone、HtmlPdfOptions.Now

印刷されたページ:locale.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Locale, time zone and clock</title>
<style>body { font: 12pt/1.5 Arial, sans-serif; margin: 0; } dt { font-weight: bold; margin-top: 3mm; } dd { margin: 0; }</style></head>
<body>
<h1>Dates and numbers, as the page's scripts write them</h1>
<dl id="out"></dl>
<script>
  const out = document.getElementById('out');
  const show = (name, value) => { const dt = document.createElement('dt'); dt.textContent = name; const dd = document.createElement('dd'); dd.textContent = value; out.append(dt, dd); };
  const now = new Date();
  show('new Date().toString()', now.toString());
  show('toLocaleDateString(undefined, { dateStyle: "full" })', now.toLocaleDateString(undefined, { dateStyle: 'full' }));
  show('Intl.NumberFormat(undefined, { style: "currency", currency: "EUR" })', new Intl.NumberFormat(undefined, { style: 'currency', currency: 'EUR' }).format(1234567.891));
  show('Intl.DateTimeFormat().resolvedOptions().timeZone', Intl.DateTimeFormat().resolvedOptions().timeZone);
</script>
</body></html>
既定
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
48,120 バイト(47.0 KB)· PDF
ノート
  • Info scriptsRan: The page's scripts ran (17 ms of virtual time, 2 tasks; 0 canvases drawn).
de-CH、Europe/Zurich、固定の時計
var result = HtmlToPdf.Render(html, new HtmlPdfOptions
{
    Locale = "de-CH",
    TimeZone = "Europe/Zurich",
    Now = new DateTimeOffset(2026, 11, 7, 9, 30, 0, TimeSpan.FromHours(1)),
});
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
50,717 バイト(49.5 KB)· PDF
ノート
  • Info scriptsRan: The page's scripts ran (17 ms of virtual time, 2 tasks; 0 canvases drawn).

インストール済みフォント

既定では、ページ自身の @font-face フォントの次にマシンのフォントが使われます。オフにすると同梱のフォントだけになります。これらのページは macOS で作成しました。

リファレンス: HtmlPdfOptions.UseInstalledFonts

印刷されたページ:fonts.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Fonts</title>
<style>body { font: 13pt/1.6 Arial, sans-serif; margin: 0; } p { margin: 2mm 0; } span { color: #6b7280; font: 9pt Arial; }</style></head>
<body>
<h1>Families, by name</h1>
<p style="font-family: Arial">Arial: The quick brown fox jumps. <span>(bundled metric-compatible face)</span></p>
<p style="font-family: 'Times New Roman'">Times New Roman: The quick brown fox jumps. <span>(bundled)</span></p>
<p style="font-family: 'Segoe UI'">Segoe UI: The quick brown fox jumps. <span>(bundled)</span></p>
<p style="font-family: 'Helvetica Neue', serif">Helvetica Neue: The quick brown fox jumps. <span>(installed on macOS; else the serif fallback)</span></p>
<p style="font-family: Avenir, serif">Avenir: The quick brown fox jumps. <span>(installed on macOS; else the serif fallback)</span></p>
<p style="font-family: 'Hiragino Sans', serif">日本語のテキスト <span>(Japanese: the machine's fonts)</span></p>
</body></html>
true (既定)
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
104,493 バイト(102.0 KB)· PDF
ノート
  • Info fontFamilyMissing: The font family "Segoe UI" is not on this machine: its text goes to the next family.
false: 同梱のフォントのみ
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { UseInstalledFonts = false });
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
36,473 バイト(35.6 KB)· PDF
ノート
  • Info fontFamilyMissing: The font family "Helvetica Neue" is not in the font profile "reference": its text goes to the next family.
  • Info fontFamilyMissing: The font family "Avenir" is not in the font profile "reference": its text goes to the next family.
  • Info fontFamilyMissing: The font family "Hiragino Sans" is not in the font profile "reference": its text goes to the next family.
  • Warning missingGlyph: No font of 'Hiragino Sans, serif' nor the fallback has a glyph for a character: drawn as .notdef.

スクロールで読み込まれるリスト

無限リストは、下端が表示されるたびに次の十件の注文を読み込みます。スクロールのオプションがない場合、Weblitz の IntersectionObserver は下端が見えたことを一度だけ通知するため、PDF には二十件が入ります。ScrollThrough はウィンドウを一画面分ずつ下へスクロールし、各ステップで仮想時間 ScrollStepWait だけ待つので、リストは終わりまで読み込まれ、六十件すべてが印刷されます。

リファレンス: HtmlPdfOptions.ScrollThrough、HtmlPdfOptions.ScrollStepWait

印刷されたページ:lazy-list.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>A list that loads as you scroll</title>
<style>
  body { font: 11pt/1.4 Arial, sans-serif; margin: 0; }
  li { padding: 2.2mm 3mm; min-height: 24mm; box-sizing: border-box; border-bottom: 1px solid #e5e7eb; }
  li:nth-child(odd) { background: #f8fafc; }
  #more { padding: 3mm; color: #6b7280; font-style: italic; }
</style></head>
<body>
<h1>Orders, loaded 10 at a time as you scroll</h1>
<ol id="list"></ol>
<p id="more">Scroll for more…</p>
<script>
  // An infinite list as sites write it: the next ten orders load when the bottom comes into view, up to 60
  let loaded = 0;
  const list = document.getElementById('list');
  function loadMore() {
    for (let i = 0; i < 10 && loaded < 60; i++) {
      loaded++;
      const li = document.createElement('li');
      li.textContent = `Order #${1000 + loaded}: ${['Frankfurt', 'Lyon', 'Milano', 'Basel', 'Gent'][loaded % 5]}, ${(loaded * 37) % 900 + 100} EUR`;
      list.append(li);
    }
    if (loaded >= 60) document.getElementById('more').textContent = 'All 60 orders loaded.';
  }
  loadMore();
  new IntersectionObserver((entries) => { if (entries.some(e => e.isIntersecting)) setTimeout(loadMore, 100); })
    .observe(document.getElementById('more'));
</script>
</body></html>
既定: スクロールしない
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目 このコードで印刷した PDF の 2 ページ目
ページ
2
ファイル
53,524 バイト(52.3 KB)· PDF
ノート
  • Info scriptsRan: The page's scripts ran (133 ms of virtual time, 5 tasks; 0 canvases drawn).
ScrollThrough
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { ScrollThrough = true });
このコードで印刷した PDF の 1 ページ目 このコードで印刷した PDF の 2 ページ目
ページ
6
ファイル
58,677 バイト(57.3 KB)· PDF
ノート
  • Info scriptsRan: The page's scripts ran (883 ms of virtual time, 40 tasks; 0 canvases drawn).

先にセクションまでスクロールする

ScrollTo は、ユーザーが印刷を押す前にするのと同じように、印刷前にウィンドウをスクロールします。ページのスクロールイベントや IntersectionObserver のコールバックはそれを受け取ります(ここでは画面外に出たセクションと scrollY)。ページ自身のスクロールに連動するスクロールタイムラインは、Chrome と同じく、いずれにしても開始位置で印刷されます。PDF には引き続き文書全体が先頭から入ります。

リファレンス: HtmlPdfOptions.ScrollTo

印刷されたページ:scroll-effects.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Effects that wait for the scroll</title>
<style>
  body { font: 11pt/1.5 Arial, sans-serif; margin: 0; }
  .progress { height: 5mm; background: #e5e7eb; border-radius: 3mm; overflow: hidden; margin: 2mm 0 5mm; }
  .progress i { display: block; height: 100%; width: 100%; transform-origin: left; background: linear-gradient(90deg, #22d3ee, #a855f7);
                animation: fill linear both; animation-timeline: scroll(root); }
  @keyframes fill { from { transform: scaleX(0); } to { transform: scaleX(1); } }
  section { min-height: 230mm; padding: 6mm; border-left: 4px solid #e5e7eb; margin-bottom: 4mm; }
  section.on { border-left-color: #ff5a1f; background: #fff7ed; }
  .state { font: bold 10pt "Courier New", monospace; color: #6b7280; }
  section.on .state { color: #c2410c; }
  #scrolled { font: 10pt "Courier New", monospace; }
</style></head>
<body>
<h1>Sections that change when they are seen</h1>
<p id="scrolled">window.scrollY when printed: 0</p>
<p>Reading progress (a scroll-driven animation on the page's scroll: printed at its start, scrolled or not, as Chrome prints it):</p>
<div class="progress"><i></i></div>
<section id="intro"><h2>1. Introduction</h2><p class="state">not seen yet</p></section>
<section id="method"><h2>2. Method</h2><p class="state">not seen yet</p></section>
<section id="results"><h2>3. Results</h2><p class="state">not seen yet</p></section>
<script>
  // Each section says whether it is on screen (IntersectionObserver); the bar above follows the page's scroll
  const observer = new IntersectionObserver((entries) => {
    for (const e of entries) {
      e.target.classList.toggle('on', e.isIntersecting);
      e.target.querySelector('.state').textContent = e.isIntersecting ? 'on screen' : 'off screen';
    }
  });
  document.querySelectorAll('section').forEach(s => observer.observe(s));
  addEventListener('scroll', () => { document.getElementById('scrolled').textContent = 'window.scrollY when printed: ' + Math.round(scrollY); });
</script>
</body></html>
既定: スクロールしない
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目 このコードで印刷した PDF の 2 ページ目 このコードで印刷した PDF の 3 ページ目
ページ
3
ファイル
69,331 バイト(67.7 KB)· PDF
ノート
  • Info scriptsRan: The page's scripts ran (17 ms of virtual time, 2 tasks; 0 canvases drawn).
ScrollTo "#results"
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { ScrollTo = "#results" });
このコードで印刷した PDF の 1 ページ目 このコードで印刷した PDF の 2 ページ目 このコードで印刷した PDF の 3 ページ目
ページ
3
ファイル
70,553 バイト(68.9 KB)· PDF
ノート
  • Info scriptsRan: The page's scripts ran (33 ms of virtual time, 4 tasks; 0 canvases drawn).

ページの一部を印刷する

Region は文書の一部を、開始位置(スクロール位置、長さ、要素)から指定した高さ分だけ印刷します。その上下にあるものは除かれ、その部分は文書全体と同じようにページに組まれます。

リファレンス: HtmlPdfOptions.Region、HtmlPrintRegion

印刷されたページ:chapters.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Five chapters</title>
<style>
  body { font: 11pt/1.6 Arial, sans-serif; margin: 0; }
  .chapter { padding: 6mm; margin-bottom: 6mm; border: 2px solid #0b1020; border-radius: 3mm; }
  .chapter h2 { margin-top: 0; }
  .chapter:nth-child(odd) { background: #f0fdfa; }
  .ruler { height: 100mm; background: repeating-linear-gradient(#fff 0 9.5mm, #cbd5e1 9.5mm 10mm); border: 1px solid #cbd5e1; }
</style></head>
<body>
<div class="chapter" id="chapter-1"><h2>Chapter 1</h2><div class="ruler"></div></div>
<div class="chapter" id="chapter-2"><h2>Chapter 2</h2><div class="ruler"></div></div>
<div class="chapter" id="chapter-3"><h2>Chapter 3: printed from here</h2><p>The region starts at this chapter's top and runs 1200 CSS pixels down.</p><div class="ruler"></div></div>
<div class="chapter" id="chapter-4"><h2>Chapter 4</h2><div class="ruler"></div></div>
<div class="chapter" id="chapter-5"><h2>Chapter 5</h2><div class="ruler"></div></div>
</body></html>
既定: 文書全体
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目 このコードで印刷した PDF の 2 ページ目 このコードで印刷した PDF の 3 ページ目
ページ
3
ファイル
46,933 バイト(45.8 KB)· PDF
1200 px from #chapter-3
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { Region = HtmlPrintRegion.Of("#chapter-3", "1200px") });
このコードで印刷した PDF の 1 ページ目 このコードで印刷した PDF の 2 ページ目
ページ
2
ファイル
56,560 バイト(55.2 KB)· PDF

Chrome の試験運用機能

既定ではオフで、安定版 Chrome と同じ印刷になります。オンにすると、Chrome 156 が「Experimental Web Platform features」フラグの後ろに置いている機能のうち、Weblitz が実装したものが有効になります。grid lanes もその一つです。

リファレンス: HtmlPdfOptions.ExperimentalWebFeatures

印刷されたページ:grid-lanes.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Grid lanes</title>
<style>
  body { font: 11pt/1.4 Arial, sans-serif; margin: 0; }
  .wall { display: grid-lanes; grid-template-columns: repeat(3, 1fr); gap: 4mm; }
  .card { border-radius: 3mm; padding: 4mm; color: #fff; }
  .card:nth-child(4n+1) { background: #0b1020; } .card:nth-child(4n+2) { background: #7c3aed; }
  .card:nth-child(4n+3) { background: #0891b2; } .card:nth-child(4n) { background: #ff5a1f; }
</style></head>
<body>
<h1>A wall of cards in grid lanes</h1>
<div class="wall">
  <div class="card" style="height:30mm">1</div><div class="card" style="height:55mm">2</div><div class="card" style="height:22mm">3</div>
  <div class="card" style="height:40mm">4</div><div class="card" style="height:26mm">5</div><div class="card" style="height:48mm">6</div>
  <div class="card" style="height:34mm">7</div><div class="card" style="height:20mm">8</div><div class="card" style="height:44mm">9</div>
</div>
<p>With grid lanes, each card goes into the shortest column, masonry-style; without, the browser ignores <code>display: grid-lanes</code> and the cards stack as blocks.</p>
</body></html>
既定: 安定版 Chrome と同じ
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目
ページ
2
ファイル
55,360 バイト(54.1 KB)· PDF
ExperimentalWebFeatures = true
var result = HtmlToPdf.Render(html, new HtmlPdfOptions { ExperimentalWebFeatures = true });
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
55,515 バイト(54.2 KB)· PDF

メニューなしでドキュメントを印刷する

独自の印刷スタイルを持たないドキュメントページは、上部バー、サイドバー、フッター、Cookie バナーまで印刷します。Hide は CSS セレクターで要素を除外し (それぞれ display: none !important のルール)、Styles は独自の CSS を追加します。ここでは本文をページ幅いっぱいに使わせ、長いコード行を折り返す印刷スタイルです。どちらも、Puppeteer の page.addStyleTag が page.pdf() の前に CSS を追加するのと同じように追加されます。ページのスクリプトが落ち着いた後、beforeprint の前です。

リファレンス: HtmlPdfOptions.Hide、HtmlPdfOptions.Styles

印刷されたページ:docs-page.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Installing the client</title>
<style>
  body { font: 15px/1.6 Arial, sans-serif; margin: 0; color: #0b1020; }
  .topbar { display: flex; gap: 24px; align-items: center; padding: 12px 24px; background: #0b1020; color: #fff; }
  .topbar a { color: #cbd5e1; text-decoration: none; }
  .topbar .logo { font-weight: bold; color: #fff; margin-right: auto; }
  .layout { display: flex; }
  .sidebar { width: 220px; flex: none; padding: 16px 24px; background: #f1f5f9; min-height: 900px; }
  .sidebar h4 { margin: 16px 0 4px; font-size: 12px; text-transform: uppercase; color: #64748b; }
  .sidebar a { display: block; color: #0f766e; text-decoration: none; padding: 2px 0; }
  main { padding: 16px 32px; max-width: 720px; }
  pre { background: #0b1020; color: #e2e8f0; padding: 12px 16px; border-radius: 6px; overflow-x: auto; }
  .note { border-left: 4px solid #0f766e; background: #f0fdfa; padding: 8px 16px; }
  footer { padding: 16px 24px; background: #e2e8f0; font-size: 13px; color: #475569; }
  #cookie-banner { position: fixed; left: 16px; right: 16px; bottom: 16px; padding: 16px; background: #fde68a;
    border: 1px solid #f59e0b; border-radius: 8px; }
</style></head>
<body>
<nav class="topbar"><span class="logo">Acme Docs</span><a href="#">Guides</a><a href="#">API</a><a href="#">Changelog</a><a href="#">Support</a></nav>
<div class="layout">
  <aside class="sidebar">
    <h4>Getting started</h4><a href="#">Overview</a><a href="#">Installing the client</a><a href="#">Your first call</a>
    <h4>Guides</h4><a href="#">Authentication</a><a href="#">Pagination</a><a href="#">Webhooks</a><a href="#">Errors</a>
    <h4>Reference</h4><a href="#">Clients</a><a href="#">Orders</a><a href="#">Invoices</a>
  </aside>
  <main>
    <h1>Installing the client</h1>
    <p>The client library talks to the Acme API for you: it signs requests, retries the ones that fail for a moment and
    turns errors into exceptions you can catch. Install it from your package manager:</p>
    <pre>dotnet add package Acme.Client --version 4.2.0</pre>
    <p>Then create one client for your application and keep it: it holds a pool of connections that later calls reuse.</p>
    <pre>var client = new AcmeClient(new AcmeOptions { ApiKey = Environment.GetEnvironmentVariable("ACME_KEY") });
var orders = await client.Orders.ListAsync(new OrderQuery { Status = OrderStatus.Open, PageSize = 50 });</pre>
    <p class="note">Keep the key out of your source: read it from the environment or your secret store.</p>
    <h2>Proxies</h2>
    <p>Behind a proxy, give the client its address in <code>AcmeOptions.Proxy</code>; the client honours the system's
    proxy settings otherwise.</p>
    <h2>Next steps</h2>
    <p>Make your first call, then read how pagination and webhooks work.</p>
  </main>
</div>
<footer>© Acme Inc. · Privacy · Terms · Status · Edit this page on GitHub</footer>
<div id="cookie-banner">We use cookies to improve the docs. <button>Accept</button> <button>Decline</button></div>
</body></html>
既定: ページそのまま
var result = HtmlToPdf.Render(html);
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
67,355 バイト(65.8 KB)· PDF
ノート
  • Info contentShrunk: the document is 1052 px wide on a 720 px page: printed at 68.44% (laid out 1052 px wide).
  • Info wideElement: reaches 1052 px, past the 720 px page.
Hide と Styles
var result = HtmlToPdf.Render(html, new HtmlPdfOptions
{
    Hide = ["nav", ".sidebar", "footer", "#cookie-banner"],
    Styles = ["@media print { main { max-width: none } pre { white-space: pre-wrap } }"],
});
このコードで印刷した PDF の 1 ページ目
ページ
1
ファイル
58,225 バイト(56.9 KB)· PDF

広告とトラッカーなしで

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.