How to use PdfOptions method of PuppeteerSharp.PdfOptions class

Best Puppeteer-sharp code snippet using PuppeteerSharp.PdfOptions.PdfOptions

SaveWebPage.xaml.cs

Source:SaveWebPage.xaml.cs Github

copy

Full Screen

...97 var page = await browser.NewPageAsync(); //打开一个新标签98 await page.GoToAsync(this.tbox_Url.Text); //访问页面99100 //设置PDF选项101 PuppeteerSharp.PdfOptions pdfOptions = new PuppeteerSharp.PdfOptions();102 pdfOptions.DisplayHeaderFooter = false; //是否显示页眉页脚103 pdfOptions.FooterTemplate = ""; //页脚文本104105 var width = await page.EvaluateFunctionAsync<int>("function getWidth(){return document.body.scrollWidth}");106 var height = await page.EvaluateFunctionAsync<int>("function getHeight(){return document.body.scrollHeight}");107108 pdfOptions.Width = $"{width}px";109 pdfOptions.Height = $"{height}px";110111 pdfOptions.HeaderTemplate = ""; //页眉文本112 pdfOptions.Landscape = false; //纸张方向 false-垂直 true-水平113 pdfOptions.MarginOptions = new PuppeteerSharp.Media.MarginOptions() { Bottom = "0px", Left = "0px", Right = "0px", Top = "0px" }; //纸张边距,需要设置带单位的值,默认值是None114 pdfOptions.Scale = 1m; //PDF缩放,从0-1115 pdfOptions.PrintBackground = true; ...

Full Screen

Full Screen

Form1.cs

Source:Form1.cs Github

copy

Full Screen

...62 //获取最后的标签页63 var page = (await browser.PagesAsync()).Last();64 var path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "screenshot.pdf");65 //设置PDF选项66 PdfOptions pdfOptions = new PdfOptions();67 pdfOptions.DisplayHeaderFooter = false; //是否显示页眉页脚68 pdfOptions.FooterTemplate = ""; //页脚文本69 pdfOptions.Format = new PuppeteerSharp.Media.PaperFormat(8.27m,11.69m); //pdf纸张格式 英寸为单位 70 pdfOptions.HeaderTemplate = ""; //页眉文本71 pdfOptions.Landscape = false; //纸张方向 false-垂直 true-水平 72 pdfOptions.MarginOptions = new PuppeteerSharp.Media.MarginOptions() { Bottom = "0px", Left = "0px", Right = "0px", Top = "0px" }; //纸张边距,需要设置带单位的值,默认值是None73 pdfOptions.Scale = 1m; //PDF缩放,从0-174 await page.PdfAsync(path, pdfOptions);75 MessageBox.Show($"PDF已经保存至{path}");76 }77 private async void Form1_FormClosing(object sender, FormClosingEventArgs e)78 {79 //dispose browser80 if (browser != null)...

Full Screen

Full Screen

Program.cs

Source:Program.cs Github

copy

Full Screen

...46 {47 await page.WaitForSelectorAsync(options.WaitForSelector);48 }49 await page.EmulateMediaAsync(MediaType.Print);50 var pdfOptions = new PdfOptions51 {52 PrintBackground = options.PrintBackground,53 Landscape = options.Landscape54 };55 await page.PdfAsync(options.SavePath, pdfOptions);56 }57 }58}...

Full Screen

Full Screen

PdfOptionsConverter.cs

Source:PdfOptionsConverter.cs Github

copy

Full Screen

2using PuppeteerSharp;3using PuppeteerSharp.Media;4namespace Caracan.Pdf.Converters5{6 public class PdfOptionsConverter : IPdfOptionsConverter7 {8 public PdfOptions Convert(Configuration.PdfOptions pdfOptions)9 {10 return new PdfOptions11 {12 Scale = pdfOptions.Scale,13 DisplayHeaderFooter = pdfOptions.DisplayHeaderFooter,14 HeaderTemplate = pdfOptions.HeaderTemplate,15 FooterTemplate = pdfOptions.FooterTemplate,16 PrintBackground = pdfOptions.PrintBackground,17 Landscape = pdfOptions.Landscape,18 PageRanges = pdfOptions.PageRanges,19 Format = ConvertFormat(pdfOptions.Format),20 Width = pdfOptions.Width,21 Height = pdfOptions.Height,22 MarginOptions = new PuppeteerSharp.Media.MarginOptions {23 Top = pdfOptions.MarginOptions.Top,24 Bottom = pdfOptions.MarginOptions.Bottom,...

Full Screen

Full Screen

PdfRequest.cs

Source:PdfRequest.cs Github

copy

Full Screen

...4namespace AbdusCo.Matbaa.Abstractions5{6 public class PdfRequest7 {8 public static readonly PdfOptions DefaultPdfOptions = new PdfOptions()9 {10 Format = PaperFormat.A4,11 DisplayHeaderFooter = false,12 PrintBackground = true,13 PreferCSSPageSize = true,14 };15 /// <summary>16 /// HTML to create PDF from17 /// </summary>18 public string Html { get; set; }19 public PageOptions? Options { get; set; }20 /// <summary>21 /// Puppeteer compatible PDF options. See [puppeteer documentation](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagepdfoptions)22 /// </summary>23 public PdfOptions? PdfOptions { get; set; }24 public class PageOptions25 {26 /// <summary>27 /// Enable Javascript evaluation on the page28 /// </summary>29 public bool EnableJavascript { get; set; } = true;30 /// <summary>31 /// Add script tags. See [puppeteer docs](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#frameaddscripttagoptions)32 /// You can also add script tags in HTML33 /// </summary>34 public List<AddTagOptions>? AddScriptTags { get; set; }35 /// <summary>36 /// Add style tags. See [puppeteer docs](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#frameaddstyletagoptions)37 /// You can also add style tags in HTML...

Full Screen

Full Screen

PuppeteerPdfModelService.cs

Source:PuppeteerPdfModelService.cs Github

copy

Full Screen

...10 {11 var model = new PuppeteerPdfModel12 {13 Html = html,14 PdfOptions = GetPdfOptions(headerHtml, footerHtml)15 };16 return JsonSerializer.Serialize(model);17 }18 private static PdfOptions GetPdfOptions(string headerHtml, string footerHtml)19 {20 return new PdfOptions21 {22 DisplayHeaderFooter = true,23 PrintBackground = true,24 HeaderTemplate = headerHtml,25 FooterTemplate = footerHtml,26 Format = PaperFormat.A4,27 MarginOptions = new MarginOptions28 {29 Top = "20mm",30 Right = "0mm",31 Bottom = "20mm",32 Left = "0mm"33 }34 };...

Full Screen

Full Screen

PuppeteerSharpConverter.cs

Source:PuppeteerSharpConverter.cs Github

copy

Full Screen

...5namespace DddDotNet.Infrastructure.PdfConverters.PuppeteerSharp6{7 public class PuppeteerSharpConverter : IPdfConverter8 {9 public Stream Convert(string html, CrossCuttingConcerns.PdfConverter.PdfOptions pdfOptions = null)10 {11 return ConvertAsync(html, pdfOptions).GetAwaiter().GetResult();12 }13 public async Task<Stream> ConvertAsync(string html, CrossCuttingConcerns.PdfConverter.PdfOptions pdfOptions = null)14 {15 await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });16 await using var page = await browser.NewPageAsync();17 await page.SetContentAsync(html);18 return new MemoryStream(await page.PdfDataAsync(new global::PuppeteerSharp.PdfOptions19 {20 PrintBackground = true,21 }));22 }23 }24}...

Full Screen

Full Screen

IPuppeteerReport.cs

Source:IPuppeteerReport.cs Github

copy

Full Screen

...3namespace PuppeteerReportCsharp4{5 public interface IPuppeteerReport6 {7 Task<byte[]> PDF(string file, PdfOptions? options);8 Task PDF(string file, string outputFile, PdfOptions? options);9 Task<byte[]> PDFPage(Page page, PdfOptions? options);10 Task PDFPage(Page page, string outputFile, PdfOptions? options);11 }12}...

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))2{3 using (var page = await browser.NewPageAsync())4 {5 await page.PdfAsync("C:/Users/Downloads/1.pdf", new PdfOptions { PrintBackground = true, MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm" } });6 }7}8using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))9{10 using (var page = await browser.NewPageAsync())11 {12 var data = await page.PdfDataAsync(new PdfOptions { PrintBackground = true, MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm" } });13 File.WriteAllBytes("C:/Users/Downloads/2.pdf", data);14 }15}16using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))17{18 using (var page = await browser.NewPageAsync())19 {20 var stream = await page.PdfStreamAsync(new PdfOptions { PrintBackground = true, MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm" } });21 var fileStream = new FileStream("C:/Users/Downloads/3.pdf", FileMode.Create);22 stream.CopyTo(fileStream);23 fileStream.Close();24 }25}26using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))27{28 using (var page = await browser.NewPageAsync())29 {30 var stream = await page.PdfStreamAsync(new PdfOptions { PrintBackground = true, MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm" } });31 var fileStream = new FileStream("C:/Users/

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions10 {11 });12 var page = await browser.NewPageAsync();13 await page.PdfAsync("google.pdf", new PdfOptions14 {15 {16 }17 });18 }19 }20}

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))10 using (var page = await browser.NewPageAsync())11 {12 await page.PdfAsync("1.pdf", new PdfOptions { Format = PaperFormat.A4 });13 }14 }15 }16}

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System;3using System.IO;4using System.Threading.Tasks;5{6 {7 static void Main(string[] args)8 {9 MainAsync().Wait();10 }11 static async Task MainAsync()12 {13 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);14 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))15 using (var page = await browser.NewPageAsync())16 {17 await page.PdfAsync("google.pdf");18 }19 }20 }21}

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1{2 MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm" },3};4var pdf = await browser.NewPageAsync();5await pdf.PdfAsync("1.pdf", pdfOptions);6{7 MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm" },8};9var pdf = await browser.NewPageAsync();10await pdf.PdfAsync("2.pdf", pdfOptions);11{12 MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm" },13};14var pdf = await browser.NewPageAsync();15await pdf.PdfAsync("3.pdf", pdfOptions);16{17 MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm" },18};19var pdf = await browser.NewPageAsync();20await pdf.PdfAsync("4.pdf", pdfOptions);21{22 MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm" },

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1PdfOptions pdfOptions = new PdfOptions();2pdfOptions.DisplayHeaderFooter = true;3pdfOptions.HeaderTemplate = "<span style='font-size: 9px;'>Header text</span>";4pdfOptions.FooterTemplate = "<span style='font-size: 9px;'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></span>";5pdfOptions.Format = "A4";6pdfOptions.MarginOptions.Top = "1.5cm";7pdfOptions.MarginOptions.Bottom = "1.5cm";8pdfOptions.MarginOptions.Left = "1.5cm";9pdfOptions.MarginOptions.Right = "1.5cm";10pdfOptions.PreferCSSPageSize = true;11await page.PdfAsync("output.pdf", pdfOptions);

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1var pdfOptions = new PdfOptions { PrintBackground = true, Format = PaperFormat.A4 };2await page.PdfAsync("output.pdf", pdfOptions);3var pdfOptions = new PdfOptions { DisplayHeaderFooter = true, Format = PaperFormat.A4 };4await page.PdfAsync("output.pdf", pdfOptions);5var pdfOptions = new PdfOptions { DisplayHeaderFooter = true, Format = PaperFormat.A4, MarginOptions = new MarginOptions { Top = "10px", Bottom = "10px" } };6await page.PdfAsync("output.pdf", pdfOptions);7var pdfOptions = new PdfOptions { DisplayHeaderFooter = true, Format = PaperFormat.A4, MarginOptions = new MarginOptions { Top = "10px", Bottom = "10px" }, PrintBackground = true };8await page.PdfAsync("output.pdf", pdfOptions);9var pdfOptions = new PdfOptions { DisplayHeaderFooter = true, Format = PaperFormat.A4, MarginOptions = new MarginOptions { Top = "10px", Bottom = "10px" }, PrintBackground = true, HeaderTemplate = "<div style='width: 100%; height: 100%; background-color: #0055ff; color: white; font-size: 24px; text-align: center; padding-top: 10px;'>Header</div>" };10await page.PdfAsync("output.pdf", pdfOptions);11var pdfOptions = new PdfOptions { DisplayHeaderFooter = true, Format = PaperFormat.A4, MarginOptions = new MarginOptions { Top = "10px", Bottom = "10px" }, PrintBackground = true, HeaderTemplate = "<div style='width: 100%; height: 100%; background-color: #0055ff; color: white; font-size: 24px; text-align: center; padding-top: 10px;'>Header

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 string path = @"C:\Users\username\AppData\Local\Chromium\Application\chrome.exe";9 string htmlpath = @"C:\Users\username\Desktop\html\1.html";10 string pdfpath = @"C:\Users\username\Desktop\pdf\1.pdf";11 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { ExecutablePath = path, Headless = true }))12 {13 using (var page = await browser.NewPageAsync())14 {15 await page.GoToAsync(htmlpath);16 await page.PdfAsync(pdfpath, new PdfOptions { Format = "A4", PrintBackground = true });17 }18 }19 }20 }21}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in PdfOptions

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful