Best Playwright-dotnet code snippet using Microsoft.Playwright.PagePdfOptions
IPage.cs
Source:IPage.cs
...1144 /// </para>1145 /// <code>1146 /// // Generates a PDF with 'screen' media type<br/>1147 /// await page.EmulateMediaAsync(new PageEmulateMediaOptions { Media = Media.Screen });<br/>1148 /// await page.PdfAsync(new PagePdfOptions { Path = "page.pdf" });1149 /// </code>1150 /// <para>1151 /// The <paramref name="width"/>, <paramref name="height"/>, and <paramref name="margin"/>1152 /// options accept values labeled with units. Unlabeled values are treated as pixels.1153 /// </para>1154 /// <para>A few examples:</para>1155 /// <list type="bullet">1156 /// <item><description><c>page.pdf({width: 100})</c> - prints with width set to 100 pixels</description></item>1157 /// <item><description><c>page.pdf({width: '100px'})</c> - prints with width set to 100 pixels</description></item>1158 /// <item><description><c>page.pdf({width: '10cm'})</c> - prints with width set to 10 centimeters.</description></item>1159 /// </list>1160 /// <para>All possible units are:</para>1161 /// <list type="bullet">1162 /// <item><description><c>px</c> - pixel</description></item>1163 /// <item><description><c>in</c> - inch</description></item>1164 /// <item><description><c>cm</c> - centimeter</description></item>1165 /// <item><description><c>mm</c> - millimeter</description></item>1166 /// </list>1167 /// <para>The <paramref name="format"/> options are:</para>1168 /// <list type="bullet">1169 /// <item><description><c>Letter</c>: 8.5in x 11in</description></item>1170 /// <item><description><c>Legal</c>: 8.5in x 14in</description></item>1171 /// <item><description><c>Tabloid</c>: 11in x 17in</description></item>1172 /// <item><description><c>Ledger</c>: 17in x 11in</description></item>1173 /// <item><description><c>A0</c>: 33.1in x 46.8in</description></item>1174 /// <item><description><c>A1</c>: 23.4in x 33.1in</description></item>1175 /// <item><description><c>A2</c>: 16.54in x 23.4in</description></item>1176 /// <item><description><c>A3</c>: 11.7in x 16.54in</description></item>1177 /// <item><description><c>A4</c>: 8.27in x 11.7in</description></item>1178 /// <item><description><c>A5</c>: 5.83in x 8.27in</description></item>1179 /// <item><description><c>A6</c>: 4.13in x 5.83in</description></item>1180 /// </list>1181 /// </summary>1182 /// <remarks>1183 /// <para>Generating a pdf is currently only supported in Chromium headless.</para>1184 /// <para>1185 /// By default, <c>page.pdf()</c> generates a pdf with modified colors for printing.1186 /// Use the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-print-color-adjust"><c>-webkit-print-color-adjust</c></a>1187 /// property to force rendering of exact colors.1188 /// </para>1189 /// <para>1190 /// <paramref name="headerTemplate"/> and <paramref name="footerTemplate"/> markup have1191 /// the following limitations: > 1. Script tags inside templates are not evaluated.1192 /// > 2. Page styles are not visible inside templates.1193 /// </para>1194 /// </remarks>1195 /// <param name="options">Call options</param>1196 Task<byte[]> PdfAsync(PagePdfOptions? options = default);1197 /// <summary>1198 /// <para>Focuses the element, and then uses <see cref="IKeyboard.DownAsync"/> and <see cref="IKeyboard.UpAsync"/>.</para>1199 /// <para>1200 /// <paramref name="key"/> can specify the intended <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key">keyboardEvent.key</a>1201 /// value or a single character to generate the text for. A superset of the <paramref1202 /// name="key"/> values can be found <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values">here</a>.1203 /// Examples of the keys are:1204 /// </para>1205 /// <para>1206 /// <c>F1</c> - <c>F12</c>, <c>Digit0</c>- <c>Digit9</c>, <c>KeyA</c>- <c>KeyZ</c>,1207 /// <c>Backquote</c>, <c>Minus</c>, <c>Equal</c>, <c>Backslash</c>, <c>Backspace</c>,1208 /// <c>Tab</c>, <c>Delete</c>, <c>Escape</c>, <c>ArrowDown</c>, <c>End</c>, <c>Enter</c>,1209 /// <c>Home</c>, <c>Insert</c>, <c>PageDown</c>, <c>PageUp</c>, <c>ArrowRight</c>, <c>ArrowUp</c>,1210 /// etc....
PageSynchronous.cs
Source:PageSynchronous.cs
...1162 /// </para>1163 /// <code>1164 /// // Generates a PDF with 'screen' media type<br/>1165 /// await page.EmulateMediaAsync(new PageEmulateMediaOptions { Media = Media.Screen });<br/>1166 /// await page.PdfAsync(new PagePdfOptions { Path = "page.pdf" });1167 /// </code>1168 /// <para>1169 /// The <paramref name="width"/>, <paramref name="height"/>, and <paramref name="margin"/>1170 /// options accept values labeled with units. Unlabeled values are treated as pixels.1171 /// </para>1172 /// <para>A few examples:</para>1173 /// <list type="bullet">1174 /// <item><description><c>page.pdf({width: 100})</c> - prints with width set to 100 pixels</description></item>1175 /// <item><description><c>page.pdf({width: '100px'})</c> - prints with width set to 100 pixels</description></item>1176 /// <item><description><c>page.pdf({width: '10cm'})</c> - prints with width set to 10 centimeters.</description></item>1177 /// </list>1178 /// <para>All possible units are:</para>1179 /// <list type="bullet">1180 /// <item><description><c>px</c> - pixel</description></item>1181 /// <item><description><c>in</c> - inch</description></item>1182 /// <item><description><c>cm</c> - centimeter</description></item>1183 /// <item><description><c>mm</c> - millimeter</description></item>1184 /// </list>1185 /// <para>The <paramref name="format"/> options are:</para>1186 /// <list type="bullet">1187 /// <item><description><c>Letter</c>: 8.5in x 11in</description></item>1188 /// <item><description><c>Legal</c>: 8.5in x 14in</description></item>1189 /// <item><description><c>Tabloid</c>: 11in x 17in</description></item>1190 /// <item><description><c>Ledger</c>: 17in x 11in</description></item>1191 /// <item><description><c>A0</c>: 33.1in x 46.8in</description></item>1192 /// <item><description><c>A1</c>: 23.4in x 33.1in</description></item>1193 /// <item><description><c>A2</c>: 16.54in x 23.4in</description></item>1194 /// <item><description><c>A3</c>: 11.7in x 16.54in</description></item>1195 /// <item><description><c>A4</c>: 8.27in x 11.7in</description></item>1196 /// <item><description><c>A5</c>: 5.83in x 8.27in</description></item>1197 /// <item><description><c>A6</c>: 4.13in x 5.83in</description></item>1198 /// </list>1199 /// </summary>1200 /// <remarks>1201 /// <para>Generating a pdf is currently only supported in Chromium headless.</para>1202 /// <para>1203 /// By default, <c>page.pdf()</c> generates a pdf with modified colors for printing.1204 /// Use the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-print-color-adjust"><c>-webkit-print-color-adjust</c></a>1205 /// property to force rendering of exact colors.1206 /// </para>1207 /// <para>1208 /// <paramref name="headerTemplate"/> and <paramref name="footerTemplate"/> markup have1209 /// the following limitations: > 1. Script tags inside templates are not evaluated.1210 /// > 2. Page styles are not visible inside templates.1211 /// </para>1212 /// </remarks>1213 /// <param name="options">Call options</param>1214 public static byte[] Pdf(this IPage page, PagePdfOptions? options = null)1215 {1216 return page.PdfAsync(options).GetAwaiter().GetResult();1217 }1218 /// <summary><para>Returns the buffer with the captured screenshot.</para></summary>1219 /// <param name="options">Call options</param>1220 public static byte[] Screenshot(this IPage page, PageScreenshotOptions? options = null)1221 {1222 return page.ScreenshotAsync(options).GetAwaiter().GetResult();1223 }1224 /// <summary>1225 /// <para>Adds a script which would be evaluated in one of the following scenarios:</para>1226 /// <list type="bullet">1227 /// <item><description>Whenever the page is navigated.</description></item>1228 /// <item><description>...
Page.cs
Source:Page.cs
...565 public Task ExposeFunctionAsync<T1, T2, T3, TResult>(string name, Func<T1, T2, T3, TResult> callback)566 => ExposeBindingAsync(name, (BindingSource _, T1 t1, T2 t2, T3 t3) => callback(t1, t2, t3));567 public Task ExposeFunctionAsync<T1, T2, T3, T4, TResult>(string name, Func<T1, T2, T3, T4, TResult> callback)568 => ExposeBindingAsync(name, (BindingSource _, T1 t1, T2 t2, T3 t3, T4 t4) => callback(t1, t2, t3, t4));569 public async Task<byte[]> PdfAsync(PagePdfOptions options = default)570 {571 if (!Context.IsChromium)572 {573 throw new NotSupportedException("This browser doesn't support this action.");574 }575 byte[] result = await _channel.PdfAsync(576 scale: options?.Scale,577 displayHeaderFooter: options?.DisplayHeaderFooter,578 headerTemplate: options?.HeaderTemplate,579 footerTemplate: options?.FooterTemplate,580 printBackground: options?.PrintBackground,581 landscape: options?.Landscape,582 pageRanges: options?.PageRanges,583 format: options?.Format,...
PageModel.cs
Source:PageModel.cs
...516 protected virtual void Screenshot(PageScreenshotOptions? options = null)517 {518 this.Page.Screenshot(options);519 }520 protected virtual byte[] Pdf(PagePdfOptions? options = null)521 {522 return this.Page.Pdf(options);523 }524 protected virtual void SetContent(string html, PageSetContentOptions? options = null)525 {526 this.Page.SetContent(html, options);527 }528 protected virtual void SetExtraHTTPHeaders(IEnumerable<KeyValuePair<string, string>> headers)529 {530 this.Page.SetExtraHTTPHeaders(headers);531 }532 protected virtual void SetViewportSize(int width, int height)533 {534 this.Page.SetViewportSize(width, height);...
PagePdfOptions.cs
Source:PagePdfOptions.cs
...35using System.Threading.Tasks;36#nullable enable37namespace Microsoft.Playwright38{39 public class PagePdfOptions40 {41 public PagePdfOptions() { }42 public PagePdfOptions(PagePdfOptions clone)43 {44 if (clone == null)45 {46 return;47 }48 DisplayHeaderFooter = clone.DisplayHeaderFooter;49 FooterTemplate = clone.FooterTemplate;50 Format = clone.Format;51 HeaderTemplate = clone.HeaderTemplate;52 Height = clone.Height;53 Landscape = clone.Landscape;54 Margin = clone.Margin;55 PageRanges = clone.PageRanges;56 Path = clone.Path;...
PdfExporter.cs
Source:PdfExporter.cs
...20 var page = await context.NewPageAsync();21 await page.EmulateMediaAsync(new PageEmulateMediaOptions {Media = Media.Print});22 var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(content));23 await page.GotoAsync($"data:text/html;base64,{encoded}", new PageGotoOptions {WaitUntil = WaitUntilState.NetworkIdle});24 await page.PdfAsync(new PagePdfOptions25 {26 Path = filePath,27 Format = "Letter",28 PrintBackground = true,29 Scale = 0.8f30 });31 }32 }33}...
Program.cs
Source:Program.cs
...9 await using var browser = await playwright.Chromium.LaunchAsync();10 var page = await browser.NewPageAsync();11 await page.GotoAsync("https://balta.io/blog/linguagens-de-programacao");12 // await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });13 await page.PdfAsync(new PagePdfOptions { Path = "linguagens-de-programacao.pdf" });14 }
...
generate-pdf.cs
Source:generate-pdf.cs
...7 using var playwright = await Playwright.CreateAsync();8 await using var browser = await playwright.Chromium.LaunchAsync();9 var page = await browser.NewPageAsync();10 await page.GotoAsync("https://github.com/microsoft/playwright");11 await page.PdfAsync(new PagePdfOptions { Path = "page.pdf" });12 }13}...
PagePdfOptions
Using AI Code Generation
1var playwright = await Playwright.CreateAsync();2var browser = await playwright.Chromium.LaunchAsync();3var context = await browser.NewContextAsync();4var page = await context.NewPageAsync();5await page.PdfAsync("google.pdf");6var playwright = await Playwright.CreateAsync();7var browser = await playwright.Chromium.LaunchAsync();8var context = await browser.NewContextAsync();9var page = await context.NewPageAsync();10await page.PdfAsync("google.pdf", new PagePdfOptions11{12 HeaderTemplate = "<div style='font-size: 10px;'>Header text</div>",13 FooterTemplate = "<div style='font-size: 10px;'>Footer text</div>",14 {15 }16});17var playwright = await Playwright.CreateAsync();18var browser = await playwright.Chromium.LaunchAsync();19var context = await browser.NewContextAsync();20var page = await context.NewPageAsync();21await page.PdfAsync("google.pdf", new PagePdfOptions22{23 HeaderTemplate = "<div style='font-size: 10px;'>Header text</div>",24 FooterTemplate = "<div style='font-size: 10px;'>Footer text</div>",25 {26 },27});28var playwright = await Playwright.CreateAsync();29var browser = await playwright.Chromium.LaunchAsync();
PagePdfOptions
Using AI Code Generation
1var playwright = require("playwright");2(async () => {3 for (const browserType of BROWSER) {4 const browser = await playwright[browserType].launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 await page.pdf({ path: `example-${browserType}.pdf` });8 await browser.close();9 }10})();11var playwright = require("playwright");12(async () => {13 for (const browserType of BROWSER) {14 const browser = await playwright[browserType].launch();15 const context = await browser.newContext();16 const page = await context.newPage();17 await page.pdf({ path: `example-${browserType}.pdf` });18 await browser.close();19 }20})();21var playwright = require("playwright");22(async () => {23 for (const browserType of BROWSER) {24 const browser = await playwright[browserType].launch();25 const context = await browser.newContext();26 const page = await context.newPage();27 await page.pdf({ path: `example-${browserType}.pdf` });28 await browser.close();29 }30})();31var playwright = require("playwright");32(async () => {33 for (const browserType of BROWSER) {34 const browser = await playwright[browserType].launch();35 const context = await browser.newContext();36 const page = await context.newPage();37 await page.pdf({ path: `example-${browserType}.pdf` });38 await browser.close();39 }40})();
PagePdfOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Webkit.LaunchAsync();10 var page = await browser.NewPageAsync();11 await page.PdfAsync("playwright.pdf", new PagePdfOptions12 {13 });14 }15 }16}17using Microsoft.Playwright;18using System;19using System.Threading.Tasks;20{21 {22 static async Task Main(string[] args)23 {24 using var playwright = await Playwright.CreateAsync();25 await using var browser = await playwright.Webkit.LaunchAsync();26 var page = await browser.NewPageAsync();27 await page.PdfAsync("playwright.pdf", new PdfOptions28 {29 });30 }31 }32}33using Microsoft.Playwright;34using System;35using System.Threading.Tasks;36{37 {38 static async Task Main(string[] args)39 {40 using var playwright = await Playwright.CreateAsync();41 await using var browser = await playwright.Webkit.LaunchAsync();42 var page = await browser.NewPageAsync();43 await page.PdfAsync("playwright.pdf", new PagePdfOptions44 {45 });46 }47 }48}49using Microsoft.Playwright;50using System;51using System.Threading.Tasks;52{53 {54 static async Task Main(string[] args)55 {
PagePdfOptions
Using AI Code Generation
1var playwright = await Playwright.CreateAsync();2var browser = await playwright.Chromium.LaunchAsync();3var context = await browser.NewContextAsync();4var page = await context.NewPageAsync();5var options = new PagePdfOptions();6options.Path = "D:\\output.pdf";7await page.PdfAsync(options);8await browser.CloseAsync();9var playwright = await Playwright.CreateAsync();10var browser = await playwright.Chromium.LaunchAsync();11var context = await browser.NewContextAsync();12var page = await context.NewPageAsync();13var options = new PagePdfOptions();14options.Path = "D:\\output.pdf";15options.DisplayHeaderFooter = true;16</div>";17</div>";18await page.PdfAsync(options);19await browser.CloseAsync();20var playwright = await Playwright.CreateAsync();21var browser = await playwright.Chromium.LaunchAsync();22var context = await browser.NewContextAsync();23var page = await context.NewPageAsync();24var options = new PagePdfOptions();25options.Path = "D:\\output.pdf";26options.DisplayHeaderFooter = true;27</div>";28</div>";29options.Margin = new PagePdfMargins { Top = "10px",
PagePdfOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System;3using System.Threading.Tasks;4{5 static async Task Main(string[] args)6 {7 await using var playwright = await Playwright.CreateAsync();8 await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions9 {10 });11 var page = await browser.NewPageAsync();12 await page.PdfAsync("google.pdf", new PagePdfOptions13 {14 {15 }16 });17 }18}19using Microsoft.Playwright;20using System;21using System.Threading.Tasks;22{23 static async Task Main(string[] args)24 {25 await using var playwright = await Playwright.CreateAsync();26 await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions27 {28 });29 var page = await browser.NewPageAsync();30 await page.ScreenshotAsync("google.png", new PageScreenshotOptions31 {32 });33 }34}35using Microsoft.Playwright;36using System;37using System.Threading.Tasks;38{39 static async Task Main(string[] args)40 {41 await using var playwright = await Playwright.CreateAsync();42 await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions43 {44 });45 var page = await browser.NewPageAsync();46 await page.EmulateMediaAsync(new PageEmulateMediaOptions47 {48 });49 }50}
PagePdfOptions
Using AI Code Generation
1var options = new PagePdfOptions();2options.Path = "C:\\Users\\user\\Desktop\\output.pdf";3await page.PdfAsync(options);4var options = new PagePdfOptions();5options.Path = "C:\\Users\\user\\Desktop\\output.pdf";6await page.PdfAsync(options);7var options = new PagePdfOptions();8options.Path = @"C:\Users\user\Desktop\output.pdf";9await page.PdfAsync(options);10var options = new PagePdfOptions();11options.Path = @"C:\Users\user\Desktop\output.pdf";12await page.PdfAsync(options);13var options = new PagePdfOptions();14options.Path = "C:/Users/user/Desktop/output.pdf";15await page.PdfAsync(options);16var options = new PagePdfOptions();17options.Path = "C:/Users/user/Desktop/output.pdf";18await page.PdfAsync(options);19var options = new PagePdfOptions();20options.Path = @"C:/Users/user/Desktop/output.pdf";21await page.PdfAsync(options);22var options = new PagePdfOptions();23options.Path = @"C:/Users/user/Desktop/output.pdf";24await page.PdfAsync(options);25var options = new PagePdfOptions();26options.Path = "C:\\Users\\user\\Desktop\\output.pdf";27await page.PdfAsync("C:\\Users\\user\\Desktop\\output.pdf");28var options = new PagePdfOptions();29options.Path = "C:\\Users\\user\\Desktop\\output.pdf";30await page.PdfAsync("C:\\Users\\user\\Desktop\\output.pdf");
PagePdfOptions
Using AI Code Generation
1using System;2using System.IO;3using System.Threading.Tasks;4using Microsoft.Playwright;5using Microsoft.Playwright.Helpers;6using Microsoft.Playwright.Transport.Channels;7{8 {9 static async Task Main(string[] args)10 {11 using var playwright = await Playwright.CreateAsync();12 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions13 {14 });15 var page = await browser.NewPageAsync();16 await page.PdfAsync("google.pdf", new PagePdfOptions17 {18 });19 await browser.CloseAsync();20 }21 }22}23using System;24using System.IO;25using System.Threading.Tasks;26using Microsoft.Playwright;27using Microsoft.Playwright.Helpers;28using Microsoft.Playwright.Transport.Channels;29{30 {31 static async Task Main(string[] args)32 {33 using var playwright = await Playwright.CreateAsync();34 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions35 {36 });37 var page = await browser.NewPageAsync();38 await page.ScreenshotAsync("google.png", new PageScreenshotOptions39 {40 });41 await browser.CloseAsync();42 }43 }44}45using System;46using System.IO;47using System.Threading.Tasks;48using Microsoft.Playwright;49using Microsoft.Playwright.Helpers;50using Microsoft.Playwright.Transport.Channels;51{52 {53 static async Task Main(string[] args
PagePdfOptions
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4{5 {6 static async Task Main(string[] args)7 {8 using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions10 {11 });12 var context = await browser.NewContextAsync();13 var page = await context.NewPageAsync();14 await page.PdfAsync("google.pdf", new PagePdfOptions15 {16 });17 }18 }19}
PagePdfOptions
Using AI Code Generation
1using Microsoft.Playwright;2{3 HeaderTemplate = "<div style='font-size:10px;'><span class='date'></span></div>",4 FooterTemplate = "<div style='font-size:10px;'><span class='pageNumber'></span></div>",5 {6 },7};8await page.PdfAsync("page.pdf", options);9using Microsoft.Playwright;10{11 HeaderTemplate = "<div style='font-size:10px;'><span class='date'></span></div>",12 FooterTemplate = "<div style='font-size:10px;'><span class='pageNumber'></span></div>",13 {14 },15};16await page.PdfAsync("page.pdf", options);17using Microsoft.Playwright;18{19 HeaderTemplate = "<div style='font-size:10px;'><span class='date'></span></div>",20 FooterTemplate = "<div style='font-size:10px;'><span class='pageNumber'></span></div>",21 {22 },23};
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!