Best Puppeteer-sharp code snippet using PuppeteerSharp.Page.PdfDataAsync
BrowserUtils.cs
Source:BrowserUtils.cs
...95 * first blank pdf to calculate the number of pages.96 * Then, knowing how many pages, we can layout the headers and footers,97 * and retrieve the final pdf.98 */99 byte[] blankContents = await page.PdfDataAsync(options);100 using (var blankPdf = new PDFUtils(blankContents)) {101 await page.EvaluateFunctionAsync("window.UltimatePDF.layoutPages", blankPdf.Pages);102 return await page.PdfDataAsync(options);103 }104 } else {105 return await page.PdfDataAsync(options);106 }107 } finally {108 await Cleanup(page);109 }110 } finally {111 Cleanup(browser);112 }113 }114 private Task InjectCustomStylesAsync(Page page, ref PdfOptions options) {115 /*116 * It seems that Puppeteer is not overriding the page styles from the print stylesheet.117 * As a workaround, we inject a <style> tag with the @page overrides at the end of <head>.118 * This issue might be eventually resolved in Puppeteer, and seems to be tracked by https://github.com/GoogleChrome/puppeteer/issues/393119 */...
HtmlToPdfConverter.cs
Source:HtmlToPdfConverter.cs
...58 },59 Scale = scale,60 PrintBackground = true,61 };62 return await page.PdfDataAsync(pdfOptions);63 }64 }65 }66}...
WebBrowser.cs
Source:WebBrowser.cs
...12 {13 using (var page = await browser.NewPageAsync())14 {15 await page.GoToAsync(url);16 var bytes = await page.PdfDataAsync(new PdfOptions { MarginOptions = new PuppeteerSharp.Media.MarginOptions { Top = "1cm", Bottom = "1cm" } });17 var browserPath = ApiFactory.GetBrowserPath();18 var pdfPath = Path.Combine(browserPath, "pdf");19 if (!Directory.Exists(pdfPath))20 {21 Directory.CreateDirectory(pdfPath);22 }23 var pdfFilePath = Path.Combine(pdfPath, Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".pdf");24 await File.WriteAllBytesAsync(pdfFilePath, bytes);25 var fileInfo = new FileInfo(pdfFilePath);26 return fileInfo;27 }28 }29 }30 ...
HomeController.cs
Source:HomeController.cs
...69 var page = await browser.NewPageAsync();7071 await page.GoToAsync(url, WaitUntilNavigation.Networkidle0);7273 var pdfData = await page.PdfDataAsync(new PdfOptions()74 {75 Format = PaperFormat.A476 });7778 await browser.CloseAsync();7980 var response = File(pdfData, "application/pdf");8182 return response;83 }84 }85}
...
Index.cshtml.cs
Source:Index.cshtml.cs
...32 using var browser = await Puppeteer.LaunchAsync(_options);33 using var page = await browser.NewPageAsync();34 var data = new byte[0];35 await page.GoToAsync("http://www.google.com", new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.Networkidle0 } });36 data = await page.PdfDataAsync(new PdfOptions { Format = PaperFormat.A4 });37 return File(data, "application/pdf", "file.pdf");38 }39 40 }41}...
PuppeteerController.cs
Source:PuppeteerController.cs
...22 using (var page = await browser.NewPageAsync())23 {24 await page.SetContentAsync(model.Html);25 var result = await page.GetContentAsync();26 var data = await page.PdfDataAsync(model.PdfOptions);27 return new FileContentResult(data, "application/pdf");28 }29 }30 private static LaunchOptions GetLaunchOptions()31 {32 return new LaunchOptions33 {34 Headless = true,35 Args = new[]36 {37 "--no-sandbox"38 }39 };40 }...
PuppeteerDocumentGenerator.cs
Source:PuppeteerDocumentGenerator.cs
...18 });19 using (var page = await browser.NewPageAsync())20 {21 await page.SetContentAsync(html);22 var pdfData = await page.PdfDataAsync(new PdfOptions23 {24 MarginOptions = new MarginOptions25 {26 Top = "1.5cm",27 Bottom = "1.5cm"28 },29 });30 browser.Dispose();31 return pdfData;32 }33 }34 }35}...
PuppeteerSharpConverter.cs
Source:PuppeteerSharpConverter.cs
...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}...
PdfDataAsync
Using AI Code Generation
1var browser = await Puppeteer.LaunchAsync(new LaunchOptions2{3 Args = new[] { "--no-sandbox" }4});5var page = await browser.NewPageAsync();6var pdf = await page.PdfDataAsync(new PdfOptions { Format = PaperFormat.A4 });7await browser.CloseAsync();8var path = Path.Combine(Directory.GetCurrentDirectory(), "1.pdf");9File.WriteAllBytes(path, pdf);10System.Diagnostics.Process.Start(path);11var browser = await Puppeteer.LaunchAsync(new LaunchOptions12{13 Args = new[] { "--no-sandbox" }14});15var page = await browser.NewPageAsync();16await page.PdfAsync("2.pdf", new PdfOptions { Format = PaperFormat.A4 });17await browser.CloseAsync();18System.Diagnostics.Process.Start("2.pdf");19var browser = await Puppeteer.LaunchAsync(new LaunchOptions20{21 Args = new[] { "--no-sandbox" }22});23var page = await browser.NewPageAsync();24await page.PdfAsync("3.pdf");25await browser.CloseAsync();26System.Diagnostics.Process.Start("3.pdf");27var browser = await Puppeteer.LaunchAsync(new LaunchOptions28{29 Args = new[] { "--no-sandbox" }30});31var page = await browser.NewPageAsync();32var pdf = await page.PdfAsync(new PdfOptions { Format = PaperFormat.A4 });33await browser.CloseAsync();34var path = Path.Combine(Directory.GetCurrentDirectory(), "4.pdf");35File.WriteAllBytes(path, pdf);36System.Diagnostics.Process.Start(path);
PdfDataAsync
Using AI Code Generation
1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5{6 {7 public static async Task Main(string[] args)8 {9 {10 };11 using (var browser = await Puppeteer.LaunchAsync(options))12 {13 using (var page = await browser.NewPageAsync())14 {15 var pdf = await page.PdfDataAsync(new PdfOptions16 {17 });18 File.WriteAllBytes("google.pdf", pdf);19 }20 }21 }22 }23}
PdfDataAsync
Using AI Code Generation
1var pdfData = await page.PdfDataAsync();2var pdfData = await page.PdfDataAsync();3var pdfData = await page.PdfDataAsync();4var pdfData = await page.PdfDataAsync();5var pdfData = await page.PdfDataAsync();6var pdfData = await page.PdfDataAsync();7var pdfData = await page.PdfDataAsync();8var pdfData = await page.PdfDataAsync();9var pdfData = await page.PdfDataAsync();10var pdfData = await page.PdfDataAsync();11var pdfData = await page.PdfDataAsync();
PdfDataAsync
Using AI Code Generation
1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5{6 {7 static void Main(string[] args)8 {9 var task = ConvertHtmlToPdfAsync();10 task.Wait();11 }12 private static async Task ConvertHtmlToPdfAsync()13 {14 {15 Args = new string[] { "--no-sandbox" }16 };17 using (var browser = await Puppeteer.LaunchAsync(options))18 {19 using (var page = await browser.NewPageAsync())20 {21 var pdf = await page.PdfDataAsync();22 File.WriteAllBytes("google.pdf", pdf);23 }24 }25 }26 }27}
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!!