Best Puppeteer-sharp code snippet using PuppeteerSharp.Page.PdfStreamAsync
PdfTests.cs
Source:PdfTests.cs
...32 }33 [Fact]34 public async Task ShouldDefaultToPrintInLetterFormat()35 {36 var document = PdfReader.Open(await Page.PdfStreamAsync(), PdfDocumentOpenMode.ReadOnly);37 Assert.Equal(1, document.Pages.Count);38 Assert.Equal(8.5, TruncateDouble(document.Pages[0].Width.Inch, 1));39 Assert.Equal(11, TruncateDouble(document.Pages[0].Height.Inch, 0));40 }41 [Fact]42 public async Task ShouldSupportSettingCustomFormat()43 {44 var document = PdfReader.Open(await Page.PdfStreamAsync(new PdfOptions45 {46 Format = PaperFormat.A447 }), PdfDocumentOpenMode.ReadOnly);48 Assert.Equal(1, document.Pages.Count);49 Assert.Equal(8.2, TruncateDouble(document.Pages[0].Width.Inch, 1));50 Assert.Equal(842, document.Pages[0].Height.Point);51 }52 [Fact]53 public async Task ShouldSupportSettingPaperWidthAndHeight()54 {55 var document = PdfReader.Open(await Page.PdfStreamAsync(new PdfOptions56 {57 Width = "10in",58 Height = "10in"59 }), PdfDocumentOpenMode.ReadOnly);60 Assert.Equal(1, document.Pages.Count);61 Assert.Equal(10, TruncateDouble(document.Pages[0].Width.Inch, 0));62 Assert.Equal(10, TruncateDouble(document.Pages[0].Height.Inch, 0));63 }64 [Fact]65 public async Task ShouldPrintMultiplePages()66 {67 await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");68 // Define width and height in CSS pixels.69 var width = (50 * 5) + 1;70 var height = (50 * 5) + 1;71 var document = PdfReader.Open(await Page.PdfStreamAsync(new PdfOptions72 {73 Width = width,74 Height = height75 }));76 Assert.Equal(8, document.Pages.Count);77 Assert.Equal(CssPixelsToInches(width), TruncateDouble(document.Pages[0].Width.Inch, 0));78 Assert.Equal(CssPixelsToInches(height), TruncateDouble(document.Pages[0].Height.Inch, 0));79 }80 [Fact]81 public async Task ShouldSupportPageRanges()82 {83 await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");84 // Define width and height in CSS pixels.85 var width = (50 * 5) + 1;86 var height = (50 * 5) + 1;87 var document = PdfReader.Open(await Page.PdfStreamAsync(new PdfOptions88 {89 Width = width,90 Height = height,91 PageRanges = "1,4-7"92 }));93 Assert.Equal(5, document.Pages.Count);94 }95 [Fact]96 public async Task ShouldThrowIfUnitsAreUnknown()97 {98 var exception = await Assert.ThrowsAsync<ArgumentException>(async () =>99 {100 await Page.PdfDataAsync(new PdfOptions101 {...
Html2PdfApiService.cs
Source:Html2PdfApiService.cs
...40 _logger.LogDebug("Generating PDF");41 await using var browser = await Puppeteer.LaunchAsync(_options);42 await using var page = await browser.NewPageAsync();43 await page.SetContentAsync(request.Html);44 await using var pdfStream = await page.PdfStreamAsync(_pdfOptions);45 46 await responseStream.WriteAsync(new CreatePdfReply47 {48 Pdf = await ByteString.FromStreamAsync(pdfStream) 49 });50 _logger.LogDebug("Export completed");51 }52 }53}...
PdfGenerator.cs
Source:PdfGenerator.cs
...22 {23 await Task.WhenAll(page.SetContentAsync(html), 24 page.SetCacheEnabledAsync(false));25 26 return await page.PdfStreamAsync(_converter.Convert(pdfOptions));27 }28 }29 public async Task<Stream> CreatePdfFromUrlAsync(string url, Configuration.PdfOptions pdfOptions)30 {31 using (var browser = await Puppeteer.ConnectAsync(GetConnectionOptions()))32 using (var page = await browser.NewPageAsync())33 {34 await page.GoToAsync(url);35 return await page.PdfStreamAsync(_converter.Convert(pdfOptions));36 }37 }38 private ConnectOptions GetConnectionOptions()39 {40 return new ConnectOptions()41 {42 BrowserWSEndpoint = _pdfGeneratorOptions.Connection43 };44 }45 }46}...
PdfService.cs
Source:PdfService.cs
...22 return await GetPdfStream(page);23 }24 private static async Task<Stream> GetPdfStream (Page page)25 {26 return await page.PdfStreamAsync(new PdfOptions27 {28 Format = PaperFormat.A4,29 MarginOptions = new MarginOptions30 {31 Top = "20px",32 Right = "20px",33 Bottom = "40px",34 Left = "20px"35 },36 });37 }38 }39}...
InvoiceController.cs
Source:InvoiceController.cs
...17 Headless = true18 });19 var page = await browser.NewPageAsync();20 await page.GoToAsync("https://localhost:7044/");21 var stream = await page.PdfStreamAsync(new PdfOptions22 {23 Format = PaperFormat.A4,24 MarginOptions = new MarginOptions25 {26 Bottom = "0.5in",27 Left = "0.5in",28 Right = "0.5in",29 Top = "0.5in"30 }31 });32 await browser.CloseAsync();33 return File(stream, "application/pdf", "invoice.pdf");34 }35 }...
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}...
ConvertController.cs
Source:ConvertController.cs
...21 await page.EvaluateExpressionAsync(@"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}...
Program.cs
Source:Program.cs
...12});13await using var page = await browser.NewPageAsync();14await page.EmulateMediaTypeAsync(MediaType.Screen);15await page.SetContentAsync(content);16var pdfContent = await page.PdfStreamAsync(new PdfOptions17{18 Format = PaperFormat.A4,19 PrintBackground = true20});21using (FileStream outputFileStream = new FileStream("test.pdf", FileMode.Create))22{23 pdfContent.CopyTo(outputFileStream);24}...
PdfStreamAsync
Using AI Code Generation
1using System.IO;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var options = new LaunchOptions { Headless = true };9 var browser = await Puppeteer.LaunchAsync(options);10 var page = await browser.NewPageAsync();11 var stream = await page.PdfStreamAsync();12 using (var fileStream = File.Create("google.pdf"))13 {14 await stream.CopyToAsync(fileStream);15 }16 await browser.CloseAsync();17 }18 }19}
PdfStreamAsync
Using AI Code Generation
1var page = await browser.NewPageAsync();2 var stream = await page.PdfStreamAsync(new PdfOptions3 {4 Margin = new PdfMargins { Top = "1cm", Bottom = "1cm" }5 });6 using (var fileStream = new FileStream(@"C:\test\test.pdf", FileMode.Create))7 {8 await stream.CopyToAsync(fileStream);9 }10var page = await browser.NewPageAsync();11 var data = await page.PdfDataAsync(new PdfOptions12 {13 Margin = new PdfMargins { Top = "1cm", Bottom = "1cm" }14 });15 File.WriteAllBytes(@"C:\test\test.pdf", data);16var page = await browser.NewPageAsync();17 await page.PdfAsync(@"C:\test\test.pdf", new PdfOptions18 {19 Margin = new PdfMargins { Top = "1cm", Bottom = "1cm" }20 });21var page = await browser.NewPageAsync();22 await page.PdfAsync(@"C:\test\test.pdf", new PdfOptions23 {24 Margin = new PdfMargins { Top = "1cm", Bottom = "1cm" }25 });26var page = await browser.NewPageAsync();27 await page.PdfAsync(@"C:\test\test.pdf", new PdfOptions28 {29 Margin = new PdfMargins { Top = "1cm", Bottom = "1cm" }30 });31var page = await browser.NewPageAsync();32 await page.PdfAsync(@"C:\test\
PdfStreamAsync
Using AI Code Generation
1using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions2{3 ExecutablePath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",4 Args = new string[] { "--no-sandbox" }5}))6{7 var page = await browser.NewPageAsync();8 await page.PdfStreamAsync();9 await page.PdfStreamAsync("C:\\Users\\user\\Desktop\\test.pdf");10}11using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions12{13 ExecutablePath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",14 Args = new string[] { "--no-sandbox" }15}))16{17 var page = await browser.NewPageAsync();18 var pdf = await page.PdfStreamAsync();19 File.WriteAllBytes("C:\\Users\\user\\Desktop\\test.pdf", pdf);20}21using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions22{23 ExecutablePath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",24 Args = new string[] { "--no-sandbox" }25}))26{27 var page = await browser.NewPageAsync();28 var pdf = await page.PdfStreamAsync();29 File.WriteAllBytes("C:\\Users\\user\\Desktop\\test.pdf", pdf);30}31using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions32{
PdfStreamAsync
Using AI Code Generation
1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5{6 {7 static async Task Main(string[] args)8 {9 {10 };11 using (var browser = await Puppeteer.LaunchAsync(options))12 using (var page = await browser.NewPageAsync())13 {14 await page.PdfAsync("google.pdf", new PdfOptions15 {16 });17 }18 }19 }20}21using System;22using System.IO;23using System.Threading.Tasks;24using PuppeteerSharp;25{26 {27 static async Task Main(string[] args)28 {29 {30 };31 using (var browser = await Puppeteer.LaunchAsync(options))32 using (var page = await browser.NewPageAsync())33 {34 await page.PdfAsync("google.pdf", new PdfOptions35 {36 });37 }38 }39 }40}41using System;42using System.IO;43using System.Threading.Tasks;44using PuppeteerSharp;45{46 {47 static async Task Main(string[] args)48 {49 {50 };51 using (var browser = await Puppeteer.LaunchAsync(options))52 using (
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!!