Best Puppeteer-sharp code snippet using PuppeteerSharp.Media.PaperFormat
CreatePdf.cs
Source:CreatePdf.cs
...44 pdfOptions.FooterTemplate = parameters.FooterTemplate;45 pdfOptions.PrintBackground = parameters.PrintBackground;46 pdfOptions.Landscape = parameters.Landscape;47 pdfOptions.PageRanges = parameters.PageRanges;48 pdfOptions.Format = ConvertToPaperFormat(parameters.PaperFormat);49 pdfOptions.MarginOptions = new MarginOptions50 {51 Top = parameters.TopMargin,52 Left = parameters.LeftMargin,53 Bottom = parameters.BottomMargin,54 Right = parameters.RightMargin55 };56 pdfOptions.PreferCSSPageSize = parameters.PreferCSSPageSize;57 pdf = await page.PdfStreamAsync(pdfOptions);58 Console.WriteLine($"{guid} {DateTime.Now}: PDF generated");59 60 var closePageTask = page.CloseAsync();61 await closePageTask;62 Console.WriteLine($"{guid} {DateTime.Now}: page closed");63 }64 var closeBrowserTask = browser.CloseAsync();65 await closeBrowserTask;66 Console.WriteLine($"{guid} {DateTime.Now}: browser closed");67 68 return pdf;69 }70 }71 private static PaperFormat ConvertToPaperFormat(string paperFormat)72 {73 switch (paperFormat)74 {75 case null:76 return null;77 case "Letter":78 return PaperFormat.Letter;79 case "Legal":80 return PaperFormat.Legal;81 case "Tabloid":82 return PaperFormat.Tabloid;83 case "Ledger":84 return PaperFormat.Ledger;85 case "A0":86 return PaperFormat.A0;87 case "A1":88 return PaperFormat.A1;89 case "A2":90 return PaperFormat.A2;91 case "A3":92 return PaperFormat.A3;93 case "A4":94 return PaperFormat.A4;95 case "A5":96 return PaperFormat.A5;97 case "A6":98 return PaperFormat.A6;99 default:100 return null;101 }102 }103 }104}...
DocumentsV2Controller.cs
Source:DocumentsV2Controller.cs
...40 using (StreamReader stringReader = new StreamReader(x.OpenReadStream()))41 using (var page = await s_browser.NewPageAsync()) {42 await page.SetContentAsync(await stringReader.ReadToEndAsync());43 var fileName = $"{Guid.NewGuid()}.pdf";44 await page.PdfAsync($"{Directory.GetCurrentDirectory()}/files/{fileName}", new PdfOptions() { Format = PuppeteerSharp.Media.PaperFormat.A4, Landscape = landscape, OmitBackground = true, PrintBackground = true });45 return fileName;46 }47 });48 var files = await Task.WhenAll(tasks);49 return files.ToList();50 }51 [NonAction]52 public async Task<List<string>> GeneratePdfAsync(List<string> url, bool landscape) {53 s_browser ??= await Puppeteer.LaunchAsync(_browserOptions);54 var tasks = url.Select(async x => {55 using (var page = await s_browser.NewPageAsync()) {56 await page.GoToAsync(x);57 var fileName = $"{Guid.NewGuid()}.pdf";58 await page.PdfAsync($"{Directory.GetCurrentDirectory()}/files/{fileName}", new PdfOptions() { Format = PuppeteerSharp.Media.PaperFormat.A4, Landscape = landscape, OmitBackground = true, PrintBackground = true });59 return fileName;60 }61 });62 var files = await Task.WhenAll(tasks);63 return files.ToList();64 }65 }66}...
HtmlToPdfConverter.cs
Source:HtmlToPdfConverter.cs
...37 process.Start();38 string result = process.StandardOutput.ReadToEnd();39 process.WaitForExit();40 }41 public async Task<byte[]> ConvertToPdf(string resPath, string html, PaperFormat paperFormat, int margin = 32, decimal scale = 1)42 {43 using (var page = await browser.NewPageAsync())44 {45 if(resPath != null)46 await page.GoToAsync(resPath);47 await page.SetContentAsync(html);48 string marginStr = margin + "px";49 var pdfOptions = new PdfOptions50 {51 Format = paperFormat,52 MarginOptions = new MarginOptions53 {54 Left = marginStr,55 Right = marginStr,...
PdfOptionsConverter.cs
Source:PdfOptionsConverter.cs
...27 },28 PreferCSSPageSize = pdfOptions.PreferCSSPageSize29 };30 }31 private PaperFormat ConvertFormat(Format format)32 {33 switch (format.Type)34 {35 case FormatType.A0:36 return PaperFormat.A0;37 case FormatType.A1:38 return PaperFormat.A1;39 case FormatType.A2:40 return PaperFormat.A2;41 case FormatType.A3:42 return PaperFormat.A3;43 case FormatType.A4:44 return PaperFormat.A4;45 case FormatType.A5:46 return PaperFormat.A5;47 case FormatType.A6:48 return PaperFormat.A6;49 case FormatType.Letter:50 return PaperFormat.Letter;51 case FormatType.Legal:52 return PaperFormat.Legal;53 case FormatType.Ledger:54 return PaperFormat.Ledger;55 case FormatType.Tabloid:56 return PaperFormat.Tabloid;57 default:58 return new PaperFormat(format.Width, format.Height);59 }60 }61 }62}...
DocumentsController.cs
Source:DocumentsController.cs
...19 var options = new LaunchOptions { Headless = true, ExecutablePath = @"/usr/bin/chromium-browser", Args = new string[] { "--no-sandbox" } };20 using (var browser = await Puppeteer.LaunchAsync(options))21 using (var page = await browser.NewPageAsync()) {22 await page.GoToAsync(url);23 return await page.PdfStreamAsync(new PdfOptions() { Format = PuppeteerSharp.Media.PaperFormat.A4, Landscape = landscape, OmitBackground = true, PrintBackground = true });24 }25 }26 }27}...
PuppeteerPdfModelService.cs
Source:PuppeteerPdfModelService.cs
...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 };35 }36 }37}
ConvertController.cs
Source:ConvertController.cs
...22 for(var ele of document.querySelectorAll('.pagination')){23 ele.classList.remove('pagination');24 }");25 return File(await page.PdfStreamAsync(new PdfOptions() {26 Format = PaperFormat.A427 }), "application/pdf");28 }29 }30}...
PdfHelper.cs
Source:PdfHelper.cs
...12 foreach (var c in Path.GetInvalidFileNameChars())13 pageTitle = pageTitle.Replace(c, '_');14 return $"{pageTitle}.pdf";15 }16 public static PaperFormat ParsePaperFormat(string format)17 {18 return format switch19 {20 "A0" => PaperFormat.A0,21 "A1" => PaperFormat.A1,22 "A2" => PaperFormat.A2,23 "A3" => PaperFormat.A3,24 "A4" => PaperFormat.A4,25 "A5" => PaperFormat.A5,26 "A6" => PaperFormat.A6,27 _ => PaperFormat.A4,28 };29 }30 }31}...
PaperFormat
Using AI Code Generation
1var browser = await Puppeteer.LaunchAsync(new LaunchOptions2{3 Args = new string[] { "--no-sandbox" },4});5var page = await browser.NewPageAsync();6await page.PdfAsync("output.pdf", new PdfOptions7{8 {9 }10});11await browser.CloseAsync();
PaperFormat
Using AI Code Generation
1using PuppeteerSharp.Media;2await page.PdfAsync("2.pdf", new PdfOptions { Format = PaperFormat.A4 });3using PuppeteerSharp.Media;4await page.PdfAsync("3.pdf", new PdfOptions { Format = PaperFormat.Letter });5using PuppeteerSharp.Media;6await page.PdfAsync("4.pdf", new PdfOptions { Format = PaperFormat.Legal });7using PuppeteerSharp.Media;8await page.PdfAsync("5.pdf", new PdfOptions { Format = PaperFormat.Tabloid });9using PuppeteerSharp.Media;10await page.PdfAsync("6.pdf", new PdfOptions { Format = PaperFormat.Ledger });11using PuppeteerSharp.Media;12await page.PdfAsync("7.pdf", new PdfOptions { Format = PaperFormat.A0 });13using PuppeteerSharp.Media;14await page.PdfAsync("8.pdf", new PdfOptions { Format = PaperFormat.A1 });15using PuppeteerSharp.Media;16await page.PdfAsync("9.pdf", new PdfOptions { Format = PaperFormat.A2 });17using PuppeteerSharp.Media;18await page.PdfAsync("10.pdf", new PdfOptions { Format = PaperFormat.A3 });19using PuppeteerSharp.Media;20await page.PdfAsync("11.pdf", new PdfOptions { Format = PaperFormat.A5 });21using PuppeteerSharp.Media;22await page.PdfAsync("12.pdf", new PdfOptions { Format = PaperFormat.A6 });23using PuppeteerSharp.Media;24await page.PdfAsync("13.pdf", new PdfOptions { Format = PaperFormat.A
PaperFormat
Using AI Code Generation
1using PuppeteerSharp.Media;2using System.Threading.Tasks;3{4 {5 static async Task Main(string[] args)6 {7 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);8 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))9 using (var page = await browser.NewPageAsync())10 {11 await page.PdfAsync("google.pdf", new PdfOptions { Format = PaperFormat.Letter });12 }13 }14 }15}
PaperFormat
Using AI Code Generation
1using PuppeteerSharp.Media;2var paperFormat = new PaperFormat(8.5, 11);3await page.PdfAsync("test.pdf", new PdfOptions { Format = paperFormat });4await page.PdfAsync("test.pdf", new PdfOptions { Format = PaperFormat.Letter });5await page.PdfAsync("test.pdf", new PdfOptions { Format = "Letter" });6await page.PdfAsync("test.pdf", new PdfOptions { Format = "A4" });7await page.PdfAsync("test.pdf", new PdfOptions { Format = "A5" });8await page.PdfAsync("test.pdf", new PdfOptions { Format = "A6" });9await page.PdfAsync("test.pdf", new PdfOptions { Format = "Legal" });10await page.PdfAsync("test.pdf", new PdfOptions { Format = "Tabloid" });11await page.PdfAsync("test.pdf", new PdfOptions { Format = "Ledger" });12await page.PdfAsync("test.pdf", new PdfOptions { Format = "Tabloid" });13await page.PdfAsync("test.pdf", new PdfOptions { Format = "Ledger" });14await page.PdfAsync("test.pdf", new PdfOptions { Format = "A0" });15await page.PdfAsync("test.pdf", new PdfOptions { Format = "A1" });
PaperFormat
Using AI Code Generation
1using PuppeteerSharp.Media;2using System;3using System.Threading.Tasks;4{5 {6 public PaperFormat(double width, double height, bool landscape = false)7 {8 Width = width;9 Height = height;10 Landscape = landscape;11 }12 public double Width { get; set; }13 public double Height { get; set; }14 public bool Landscape { get; set; }15 }16}17{18 {19 public static async Task<BrowserFetcher> CreateAsync(BrowserFetcherOptions options = null)20 {21 return new BrowserFetcher();22 }23 public Task<BrowserFetcherRevisionInfo> DownloadAsync(string revision)24 {25 return Task.FromResult(new BrowserFetcherRevisionInfo());26 }27 }28 {29 public string Revision { get; set; }30 public string FolderPath { get; set; }31 public bool Local { get; set; }32 }33 {34 public string Path { get; set; }35 public int Host { get; set; }36 }37 {38 public bool Headless { get; set; }39 public bool Devtools { get; set; }40 public bool IgnoreHTTPSErrors { get; set; }41 public bool SlowMo { get; set; }42 public bool Timeout { get; set; }43 public bool Dumpio { get; set; }44 public bool ExecutablePath { get; set; }45 public bool Args { get; set; }46 public bool IgnoreDefaultArgs { get; set; }47 public bool HandleSIGINT { get; set; }48 public bool HandleSIGTERM { get; set; }49 public bool HandleSIGHUP { get; set; }50 public bool Timeout { get; set; }51 public bool UserDataDir { get; set; }52 public bool Env { get; set; }53 public bool Pipe { get; set; }54 public bool Stdio { get; set; }55 public bool Dumpio { get; set; }56 public bool ChromiumSandbox { get; set; }57 public bool IgnoreDefaultArgs { get; set; }58 public bool AppMode { get
PaperFormat
Using AI Code Generation
1var paperFormat = new PaperFormat(5, 5);2await page.SetContentAsync("<html><body><h1>Test</h1></body></html>");3await page.PdfAsync("test.pdf", new PdfOptions { Format = paperFormat });4var paperFormat = new PaperFormat(5, 5);5await page.SetContentAsync("<html><body><h1>Test</h1></body></html>");6await page.PdfAsync("test.pdf", new PdfOptions { Format = paperFormat });7var paperFormat = new PaperFormat(5, 5);8await page.SetContentAsync("<html><body><h1>Test</h1></body></html>");9await page.PdfAsync("test.pdf", new PdfOptions { Format = paperFormat });10var paperFormat = new PaperFormat(5, 5);11await page.SetContentAsync("<html><body><h1>Test</h1></body></html>");12await page.PdfAsync("test.pdf", new PdfOptions { Format = paperFormat });13var paperFormat = new PaperFormat(5, 5);14await page.SetContentAsync("<html><body><h1>Test</h1></body></html>");15await page.PdfAsync("test.pdf", new PdfOptions { Format = paperFormat });16var paperFormat = new PaperFormat(5, 5);17await page.SetContentAsync("<html><body><h1>Test</h1></body></html>");18await page.PdfAsync("test.pdf", new PdfOptions { Format = paperFormat });
PaperFormat
Using AI Code Generation
1var pdf = new PuppeteerSharp.PdfOptions();2pdf.Format = PaperFormat.A3;3pdf.Landscape = true;4pdf.Path = "2.pdf";5await page.PdfAsync(pdf);6var pdf = new PuppeteerSharp.PdfOptions();7pdf.Format = PaperFormat.A4;8pdf.Landscape = true;9pdf.Path = "3.pdf";10await page.PdfAsync(pdf);11var pdf = new PuppeteerSharp.PdfOptions();12pdf.Format = PaperFormat.A5;13pdf.Landscape = true;14pdf.Path = "4.pdf";15await page.PdfAsync(pdf);16var pdf = new PuppeteerSharp.PdfOptions();17pdf.Format = PaperFormat.A6;18pdf.Landscape = true;19pdf.Path = "5.pdf";20await page.PdfAsync(pdf);21var pdf = new PuppeteerSharp.PdfOptions();22pdf.Format = PaperFormat.Letter;23pdf.Landscape = true;24pdf.Path = "6.pdf";25await page.PdfAsync(pdf);26var pdf = new PuppeteerSharp.PdfOptions();27pdf.Format = PaperFormat.Legal;28pdf.Landscape = true;29pdf.Path = "7.pdf";30await page.PdfAsync(pdf);31var pdf = new PuppeteerSharp.PdfOptions();32pdf.Format = PaperFormat.Tabloid;33pdf.Landscape = true;34pdf.Path = "8.pdf";35await page.PdfAsync(pdf);36var pdf = new PuppeteerSharp.PdfOptions();37pdf.Format = PaperFormat.Ledger;38pdf.Landscape = true;39pdf.Path = "9.pdf";40await page.PdfAsync(pdf);41var pdf = new PuppeteerSharp.PdfOptions();
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!!