Best Puppeteer-sharp code snippet using PuppeteerSharp.PdfOptions.PdfOptions
SaveWebPage.xaml.cs
Source:SaveWebPage.xaml.cs
...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;
...
Form1.cs
Source:Form1.cs
...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)...
Program.cs
Source:Program.cs
...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}...
PdfOptionsConverter.cs
Source:PdfOptionsConverter.cs
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,...
PdfRequest.cs
Source:PdfRequest.cs
...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...
PuppeteerPdfModelService.cs
Source:PuppeteerPdfModelService.cs
...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 };...
PuppeteerSharpConverter.cs
Source:PuppeteerSharpConverter.cs
...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}...
IPuppeteerReport.cs
Source:IPuppeteerReport.cs
...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}...
PdfOptions
Using AI Code Generation
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/
PdfOptions
Using AI Code Generation
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}
PdfOptions
Using AI Code Generation
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}
PdfOptions
Using AI Code Generation
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}
PdfOptions
Using AI Code Generation
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" },
PdfOptions
Using AI Code Generation
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);
PdfOptions
Using AI Code Generation
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
PdfOptions
Using AI Code Generation
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}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!