How to use PdfOptions class of PuppeteerSharp package

Best Puppeteer-sharp code snippet using PuppeteerSharp.PdfOptions

SaveWebPage.xaml.cs

Source:SaveWebPage.xaml.cs Github

copy

Full Screen

...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; ...

Full Screen

Full Screen

Form1.cs

Source:Form1.cs Github

copy

Full Screen

...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)...

Full Screen

Full Screen

Program.cs

Source:Program.cs Github

copy

Full Screen

...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}...

Full Screen

Full Screen

PdfOptionsConverter.cs

Source:PdfOptionsConverter.cs Github

copy

Full Screen

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,...

Full Screen

Full Screen

Html2PdfApiService.cs

Source:Html2PdfApiService.cs Github

copy

Full Screen

...10 public class Html2PdfApiService : Html2PdfApi.Html2PdfApiBase11 {12 private readonly ILogger<Html2PdfApiService> _logger;13 private readonly LaunchOptions _options;14 private readonly PdfOptions _pdfOptions;15 public Html2PdfApiService(ILogger<Html2PdfApiService> logger)16 {17 _logger = logger;18 19 _options = new LaunchOptions20 {21 Headless = true,22 Args = new[]23 {24 "--disable-gpu",25 "--disable-dev-shm-usage",26 "--disable-setuid-sandbox",27 "--no-sandbox",28 }29 };30 31 _pdfOptions = new PdfOptions32 {33 Format = PaperFormat.A4,34 DisplayHeaderFooter = true,35 PrintBackground = true,36 };37 }38 public override async Task CreatePdf(CreatePdfRequest request, IServerStreamWriter<CreatePdfReply> responseStream, ServerCallContext context)39 {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 ...

Full Screen

Full Screen

PdfRequest.cs

Source:PdfRequest.cs Github

copy

Full Screen

...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...

Full Screen

Full Screen

PuppeteerPdfModelService.cs

Source:PuppeteerPdfModelService.cs Github

copy

Full Screen

...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 };...

Full Screen

Full Screen

PuppeteerSharpConverter.cs

Source:PuppeteerSharpConverter.cs Github

copy

Full Screen

...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}...

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System;3using System.Threading.Tasks;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 LaunchOptions10 {11 Args = new string[] { "--no-sandbox" }12 }))13 using (var page = await browser.NewPageAsync())14 {15 await page.PdfAsync("google.pdf", new PdfOptions16 {17 });18 }19 }20 }21}22const puppeteer = require('puppeteer');23(async () => {24 const browser = await puppeteer.launch();25 const page = await browser.newPage();26 await page.pdf({path: 'google.pdf', scale: 0.5, displayHeaderFooter: true, headerTemplate: 'Header', footerTemplate: 'Footer', printBackground: true, landscape: true});27 await browser.close();28})();29using System;30using System.IO;31using System.Threading.Tasks;32using Microsoft.Playwright;33{34 {35 static async Task Main(string[] args)36 {37 using var playwright = await Playwright.CreateAsync();38 await using var browser = await playwright.Chromium.LaunchAsync();39 var page = await browser.NewPageAsync();40 await page.PdfAsync("google.pdf", new PagePdfOptions41 {42 });43 }44 }45}46const playwright = require('playwright');47(async () => {

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });10 var page = await browser.NewPageAsync();11 await page.PdfAsync("google.pdf", new PdfOptions { Format = PaperFormat.Letter });12 await browser.CloseAsync();13 }14 }15}16using PuppeteerSharp;17using System;18using System.Threading.Tasks;19{20 {21 static async Task Main(string[] args)22 {23 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);24 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });25 var page = await browser.NewPageAsync();26 await page.PdfAsync("google.pdf", new PdfOptions { Format = PaperFormat.Letter });27 await browser.CloseAsync();28 }29 }30}31using PuppeteerSharp;32using System;33using System.Threading.Tasks;34{35 {36 static async Task Main(string[] args)37 {38 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);39 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });40 var page = await browser.NewPageAsync();41 await page.PdfAsync("google.pdf", new PdfOptions { Format = PaperFormat.Letter });42 await browser.CloseAsync();43 }44 }45}46using PuppeteerSharp;47using System;48using System.Threading.Tasks;49{50 {51 static async Task Main(string[] args)52 {53 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);54 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 {9 Args = new string[] { "--no-sandbox" }10 };11 using (var browser = await Puppeteer.LaunchAsync(options))12 {13 using (var page = await browser.NewPageAsync())14 {15 await page.PdfAsync("1.pdf", new PdfOptions { Format = PaperFormat.A4 });16 }17 }18 }19 }20}21using PuppeteerSharp;22using System;23using System.Threading.Tasks;24{25 {26 static async Task Main(string[] args)27 {28 {29 Args = new string[] { "--no-sandbox" }30 };31 using (var browser = await Puppeteer.LaunchAsync(options))32 {33 using (var page = await browser.NewPageAsync())34 {35 await page.PdfAsync("2.pdf", new PdfOptions { Format = PaperFormat.A4, Landscape = true });36 }37 }38 }39 }40}41using PuppeteerSharp;42using System;43using System.Threading.Tasks;44{45 {46 static async Task Main(string[] args)47 {48 {49 Args = new string[] { "--no-sandbox" }50 };51 using (var browser = await Puppeteer.LaunchAsync(options))52 {53 using (var page = await browser.NewPageAsync())54 {55 await page.PdfAsync("3.pdf", new PdfOptions { Format = PaperFormat.A4, DisplayHeaderFooter = true });56 }57 }58 }59 }60}61using PuppeteerSharp;

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 static void Main(string[] args)7 {8 MainAsync().Wait();9 }10 static async Task MainAsync()11 {12 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);13 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))14 using (var page = await browser.NewPageAsync())15 {16 await page.PdfAsync("1.pdf", new PdfOptions17 {18 HeaderTemplate = "<div style='font-size: 10px; text-align: center; color: #888'>Header left</div><div style='font-size: 10px; text-align: center; color: #888'>Header right</div>",19 FooterTemplate = "<div style='font-size: 10px; text-align: center; color: #888'>Footer left</div><div style='font-size: 10px; text-align: center; color: #888'>Footer right</div>",20 {21 }22 });23 }24 }25 }26}

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System;3using System.IO;4using System.Threading.Tasks;5{6 {7 static async Task Main(string[] args)8 {9 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);10 var browser = await Puppeteer.LaunchAsync(new LaunchOptions11 {12 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"13 });14 var page = await browser.NewPageAsync();15 await page.PdfAsync("C:\\Users\\user\\Desktop\\google.pdf");16 await browser.CloseAsync();17 }18 }19}20using PuppeteerSharp;21using System;22using System.IO;23using System.Threading.Tasks;24{25 {26 static async Task Main(string[] args)27 {28 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);29 var browser = await Puppeteer.LaunchAsync(new LaunchOptions30 {31 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"32 });33 var page = await browser.NewPageAsync();34 await page.PdfAsync("C:\\Users\\user\\Desktop\\google.pdf", new PdfOptions35 {36 {37 }38 });39 await browser.CloseAsync();40 }41 }42}43using PuppeteerSharp;44using System;45using System.IO;46using System.Threading.Tasks;47{48 {49 static async Task Main(string[] args

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System.Threading.Tasks;3{4 {5 public string Path { get; set; }6 public bool DisplayHeaderFooter { get; set; }7 public string HeaderTemplate { get; set; }8 public string FooterTemplate { get; set; }9 public bool PrintBackground { get; set; }10 public bool Landscape { get; set; }11 public string PaperWidth { get; set; }12 public string PaperHeight { get; set; }13 public string MarginTop { get; set; }14 public string MarginBottom { get; set; }15 public string MarginLeft { get; set; }16 public string MarginRight { get; set; }17 public string PageRanges { get; set; }18 public string Format { get; set; }19 public int? Scale { get; set; }20 public bool PreferCSSPageSize { get; set; }21 }22}23using PuppeteerSharp;24using System.Threading.Tasks;25{26 {27 public string Path { get; set; }28 public bool DisplayHeaderFooter { get; set; }29 public string HeaderTemplate { get; set; }30 public string FooterTemplate { get; set; }31 public bool PrintBackground { get; set; }32 public bool Landscape { get; set; }33 public string PaperWidth { get; set; }34 public string PaperHeight { get; set; }35 public string MarginTop { get; set; }36 public string MarginBottom { get; set; }37 public string MarginLeft { get; set; }38 public string MarginRight { get; set; }39 public string PageRanges { get; set; }40 public string Format { get; set; }41 public int? Scale { get; set; }42 public bool PreferCSSPageSize { get; set; }43 }44}

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5using PuppeteerSharp.Contrib.Extensions;6{7 {8 static async Task Main(string[] args)9 {10 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);11 {12 Args = new[] { "--no-sandbox" },13 };14 using (var browser = await Puppeteer.LaunchAsync(options))15 using (var page = await browser.NewPageAsync())16 {17 await page.ScreenshotAsync("google.png");18 {19 MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm" }20 };21 await page.PdfAsync("google.pdf", pdfOptions);22 }23 }24 }25}26using System;27using System.IO;28using System.Threading.Tasks;29using PuppeteerSharp;30using PuppeteerSharp.Contrib.Extensions;31{32 {33 static async Task Main(string[] args)34 {35 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);36 {37 Args = new[] { "--no-sandbox" },38 };39 using (var browser = await Puppeteer.LaunchAsync(options))40 using (var page = await browser.NewPageAsync())41 {42 await page.ScreenshotAsync("google.png");43 {44 MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm" }45 };46 await page.PdfAsync("google.pdf", pdfOptions

Full Screen

Full Screen

PdfOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5{6 {7 static void Main(string[] args)8 {9 var task = GeneratePdf();10 task.Wait();11 }12 private static async Task GeneratePdf()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 await page.PdfAsync("google.pdf", new PdfOptions22 {23 });24 }25 }26 }27 }28}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

Most used methods in PdfOptions

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful