Best Playwright-dotnet code snippet using Microsoft.Playwright.PagePdfOptions.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
1using Microsoft.Playwright;2using System.Threading.Tasks;3{4 static async Task Main(string[] args)5 {6 using var playwright = await Playwright.CreateAsync();7 await using var browser = await playwright.Chromium.LaunchAsync();8 var page = await browser.NewPageAsync();9 var pdf = await page.PdfAsync(new PagePdfOptions10 {11 {12 }13 });14 await System.IO.File.WriteAllBytesAsync("test.pdf", pdf);15 }16}
PagePdfOptions
Using AI Code Generation
1var playwright = await Playwright.CreateAsync();2var browser = await playwright.Chromium.LaunchAsync();3var page = await browser.NewPageAsync();4await page.PdfAsync("C:\\Users\\Downloads\\google.pdf");5await browser.CloseAsync();6var playwright = await Playwright.CreateAsync();7var browser = await playwright.Chromium.LaunchAsync();8var page = await browser.NewPageAsync();9var pdf = await page.PdfAsync();10await browser.CloseAsync();11var playwright = await Playwright.CreateAsync();12var browser = await playwright.Chromium.LaunchAsync();13var page = await browser.NewPageAsync();14var pdf = await page.PdfAsync(new PagePdfOptions15{16 HeaderTemplate = "<span style='font-size:9px;'>Header</span>",17 FooterTemplate = "<span style='font-size:9px;'>Footer</span>",18});19await browser.CloseAsync();20var playwright = await Playwright.CreateAsync();21var browser = await playwright.Chromium.LaunchAsync();22var page = await browser.NewPageAsync();23var pdf = await page.PdfAsync(new PagePdfOptions24{25 HeaderTemplate = "<span style='font-size:9px;'>Header</span>",26 FooterTemplate = "<span style='font-size:9px;'>
PagePdfOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System.Threading.Tasks;3{4 {5 public static async Task Main(string[] args)6 {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.PdfAsync("c:\\temp\\example.pdf", new PagePdfOptions11 {12 {13 }14 });15 }16 }17}
PagePdfOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System.Threading.Tasks;3{4 {5 public static async Task Main(string[] args)6 {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.PdfAsync("google.pdf", new PagePdfOptions11 {12 {13 }14 })
PagePdfOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System.IO;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 var playwright = await Playwright.CreateAsync();9 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(Path.Combine(Directory.GetCurrentDirectory(), "google.pdf"), new PagePdfOptions15 {16 HeaderTemplate = @"<div style=""text-align: center; font-weight: bold; font-size: 12px;"">17 FooterTemplate = @"<div style=""text-align: center; font-weight: bold; font-size: 12px;"">18 {19 },20 });21 await browser.CloseAsync();22 }23 }24}
PagePdfOptions
Using AI Code Generation
1using Microsoft.Playwright;2{3{4public static async Task Main(string[] args)5{6await using var playwright = await Playwright.CreateAsync();7await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions8{9});10var page = await browser.NewPageAsync();11await page.PdfAsync("C:\\Users\\Downloads\\2.pdf",new PagePdfOptions12{13{14}15});16}17}18}
PagePdfOptions
Using AI Code Generation
1var page = await browser.NewPageAsync();2await page.PdfAsync("2.pdf", new PagePdfOptions3{4});5var page = await browser.NewPageAsync();6await page.ScreenshotAsync("3.png", new PageScreenshotOptions7{8});9var page = await browser.NewPageAsync();10await page.WaitForFunctionAsync("() => window.innerWidth < 100", new PageWaitForFunctionOptions11{12});13var page = await browser.NewPageAsync();14await page.ClickAsync("text=Get Started");15await page.WaitForNavigationAsync(new PageWaitForNavigationOptions16{17});18var page = await browser.NewPageAsync();19await page.WaitForSelectorAsync("text=Get Started", new PageWaitForSelectorOptions20{21});22var page = await browser.NewPageAsync();23await page.WaitForTimeoutAsync(5000);
PagePdfOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System.Threading.Tasks;3{4 {5 static async Task Main(string[] args)6 {7 using var playwright = await Playwright.CreateAsync();8 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions9 {10 });11 var page = await browser.NewPageAsync();12 await page.PdfAsync("google.pdf", new PagePdfOptions13 {14 {15 }16 });17 }18 }19}20using Microsoft.Playwright;21using System.Threading.Tasks;22{23 {24 static async Task Main(string[] args)25 {26 using var playwright = await Playwright.CreateAsync();27 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions28 {29 });30 var page = await browser.NewPageAsync();31 await page.ScreenshotAsync("google.png", new PageScreenshotOptions32 {33 });34 }35 }36}37using Microsoft.Playwright;38using System.Threading.Tasks;39{40 {41 static async Task Main(string[] args)42 {43 using var playwright = await Playwright.CreateAsync();
PagePdfOptions
Using AI Code Generation
1var pdfOptions = new PagePdfOptions();2pdfOptions.Format = PagePdfFormat.A4;3pdfOptions.DisplayHeaderFooter = true;4pdfOptions.HeaderTemplate = "<div style='font-size: 10px; font-family: Helvetica'>Header</div>";5pdfOptions.FooterTemplate = "<div style='font-size: 10px; font-family: Helvetica'>Footer</div>";6pdfOptions.Margin = new PagePdfMarginOptions { Top = "2cm", Bottom = "1cm" };7pdfOptions.Path = "2.pdf";8await page.PdfAsync(pdfOptions);9var pdfOptions = new PagePdfOptions();10pdfOptions.Format = PagePdfFormat.A4;11pdfOptions.DisplayHeaderFooter = true;12pdfOptions.HeaderTemplate = "<div style='font-size: 10px; font-family: Helvetica'>Header</div>";13pdfOptions.FooterTemplate = "<div style='font-size: 10px; font-family: Helvetica'>Footer</div>";14pdfOptions.Margin = new PagePdfMarginOptions { Top = "2cm", Bottom = "1cm" };15pdfOptions.Path = "3.pdf";16await page.PdfAsync(pdfOptions);17var pdfOptions = new PagePdfOptions();18pdfOptions.Format = PagePdfFormat.A4;19pdfOptions.DisplayHeaderFooter = true;20pdfOptions.HeaderTemplate = "<div style='font-size: 10px; font-family: Helvetica'>Header</div>";21pdfOptions.FooterTemplate = "<div style='font-size: 10px; font-family: Helvetica'>Footer</div>";22pdfOptions.Margin = new PagePdfMarginOptions { Top = "2cm", Bottom = "1cm" };23pdfOptions.Path = "4.pdf";24await page.PdfAsync(pdfOptions);25var pdfOptions = new PagePdfOptions();26pdfOptions.Format = PagePdfFormat.A4;27pdfOptions.DisplayHeaderFooter = true;28pdfOptions.HeaderTemplate = "<div style='font-size: 10px; font-family: Helvetica'>Header</div>";29pdfOptions.FooterTemplate = "<div style='font-size: 10px; font-family: Helvetica'>Footer</div>";
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 var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions10 {11 });12 var context = await browser.NewContextAsync();13 var page = await context.NewPageAsync();14 await page.PdfAsync("C:\\Users\\test\\Desktop\\pdf\\google.pdf", new PagePdfOptions15 {16 {17 }18 });19 await browser.CloseAsync();20 }21 }22}
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!!