How to use SetContentAsync method of PuppeteerSharp.Page class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Page.SetContentAsync

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

PuppeteerDocumentGenerator.cs

Source: PuppeteerDocumentGenerator.cs Github

copy
1using PuppeteerSharp;
2using PuppeteerSharp.Media;
3using System.Threading.Tasks;
4
5namespace PuppetPdf
6{
7    public class PuppeteerDocumentGenerator : DocumentGeneratorBase
8    {
9        public async Task<byte[]> Generate(string html)
10        {
11            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
12
13            var browser = await Puppeteer.LaunchAsync(new LaunchOptions
14            {
15                Headless = true,
16
17                // If needed, we can specify a specific version of Chrome
18                //ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
19                //IgnoredDefaultArgs = new string[] { "--disable-extensions" },
20                //IgnoreDefaultArgs = true,
21
22            });
23
24            using (var page = await browser.NewPageAsync())
25            {
26                await page.SetContentAsync(html);
27
28                var pdfData = await page.PdfDataAsync(new PdfOptions
29                {
30                    MarginOptions = new MarginOptions
31                    {
32                        Top = "1.5cm",
33                        Bottom = "1.5cm"
34                    },
35                });
36
37                browser.Dispose();
38                return pdfData;
39            }
40        }
41    }
42}
43
Full Screen

Program.cs

Source: Program.cs Github

copy
1// See https://aka.ms/new-console-template for more information
2using PuppeteerSharp;
3using PuppeteerSharp.Media;
4
5Console.WriteLine("Hello, World!");
6
7
8var contentCSS = File.ReadAllText(@"Content\test.css");
9var contentHTML = File.ReadAllText(@"Content\test.html");
10var content = contentCSS + contentHTML;
11
12await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
13
14await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions
15{
16    Headless = true
17});
18
19await using var page = await browser.NewPageAsync();
20await page.EmulateMediaTypeAsync(MediaType.Screen);
21await page.SetContentAsync(content);
22var pdfContent = await page.PdfStreamAsync(new PdfOptions
23{
24    Format = PaperFormat.A4,
25    PrintBackground = true
26});
27
28
29using (FileStream outputFileStream = new FileStream("test.pdf", FileMode.Create))
30{
31    pdfContent.CopyTo(outputFileStream);
32}
33
34
35
Full Screen

PuppeteerSharpConverter.cs

Source: PuppeteerSharpConverter.cs Github

copy
1using DddDotNet.CrossCuttingConcerns.PdfConverter;
2using PuppeteerSharp;
3using System.IO;
4using System.Threading.Tasks;
5
6namespace DddDotNet.Infrastructure.PdfConverters.PuppeteerSharp
7{
8    public class PuppeteerSharpConverter : IPdfConverter
9    {
10        public Stream Convert(string html, CrossCuttingConcerns.PdfConverter.PdfOptions pdfOptions = null)
11        {
12            return ConvertAsync(html, pdfOptions).GetAwaiter().GetResult();
13        }
14
15        public async Task<Stream> ConvertAsync(string html, CrossCuttingConcerns.PdfConverter.PdfOptions pdfOptions = null)
16        {
17            await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
18            await using var page = await browser.NewPageAsync();
19            await page.SetContentAsync(html);
20            return new MemoryStream(await page.PdfDataAsync(new global::PuppeteerSharp.PdfOptions
21            {
22                PrintBackground = true,
23            }));
24        }
25    }
26}
27
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in Page

Trigger SetContentAsync code on LambdaTest Cloud Grid

Execute automation tests with SetContentAsync on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)