How to use MarginOptions method of PuppeteerSharp.PdfOptions class

Best Puppeteer-sharp code snippet using PuppeteerSharp.PdfOptions.MarginOptions

SaveWebPage.xaml.cs

Source:SaveWebPage.xaml.cs Github

copy

Full Screen

...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;116117 var fileName = Environment.CurrentDirectory + $"\\download\\{await page.GetTitleAsync()}.pdf";118119 if (System.IO.File.Exists(fileName))120 {121 fileName = fileName.Replace(".pdf", $"{DateTime.Now.ToString("ffff")}.pdf");122 }123124 //保存PDF125 await page.PdfAsync(fileName, pdfOptions);126 EMessageBox.Show($"{fileName}保存成功");127 ...

Full Screen

Full Screen

ReportService.cs

Source:ReportService.cs Github

copy

Full Screen

...98 DisplayHeaderFooter = true,99 //class指定でのstyleが効かない。100 HeaderTemplate = needCommonHeaderFooter ? $@"<div class='print_header_container' style=""font-family: 'MS P明朝', 'HGP行書体';font-size:10px;text-align:right;width:100%;border:0;""><span style='vertical-align:top;margin-right:10px;' class=''>{headerTime}</span><span style='vertical-align:top;margin-right:33px;' class=''>&nbsp;</span></div>" : $@"<div class='' style='width:100%;border:0;'></div>",101 FooterTemplate = needCommonHeaderFooter ? $@"<div class='print_footer_container' style='font-size:10px;text-align:center;width:100%;border:0;'><span style='vertical-align:bottom;' class='pageNumber'></span>/<span style='vertical-align:bottom;' class='totalPages'></span></div>" : $@"<div class='' style='width:100%;border:0;'></div>",102 MarginOptions = needCommonHeaderFooter ? new MarginOptions() { Top = "50px", Bottom = "50px", Left = "50px", Right = "50px" } : new MarginOptions() { Top = "50px", Bottom = "30px", Left = "30px", Right = "30px" } //mmは効かない。103 };104105 return defaultOption;106 }107108 public void Dispose()109 {110 if (browser != null)111 {112 browser.Dispose();113 browser = null;114 }115 }116 } ...

Full Screen

Full Screen

Form1.cs

Source:Form1.cs Github

copy

Full Screen

...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)81 {82 await browser.CloseAsync();83 browser.Dispose();84 }85 }86 }...

Full Screen

Full Screen

CreatePdf.cs

Source:CreatePdf.cs Github

copy

Full Screen

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

Full Screen

Full Screen

HtmlToPdfConverter.cs

Source:HtmlToPdfConverter.cs Github

copy

Full Screen

...48 string marginStr = margin + "px";49 var pdfOptions = new PdfOptions50 {51 Format = paperFormat,52 MarginOptions = new MarginOptions53 {54 Left = marginStr,55 Right = marginStr,56 Top = marginStr,57 Bottom = marginStr58 },59 Scale = scale,60 PrintBackground = true,61 };62 return await page.PdfDataAsync(pdfOptions);63 }64 }65 }66}...

Full Screen

Full Screen

PdfOptionsConverter.cs

Source:PdfOptionsConverter.cs Github

copy

Full Screen

...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,25 Left = pdfOptions.MarginOptions.Left,26 Right = pdfOptions.MarginOptions.Right27 },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;...

Full Screen

Full Screen

ExampleController.cs

Source:ExampleController.cs Github

copy

Full Screen

...27 var page = await CreatePageAndSetContent(html, browser);28 PdfOptions pdfOptions = new()29 {30 Format = PaperFormat.A4,31 MarginOptions = new MarginOptions32 {33 Top = "20px",34 Right = "20px",35 Bottom = "40px",36 Left = "20px"37 },38 PrintBackground = true,39 };40 return await page.PdfDataAsync(pdfOptions);41 }42 private static async Task<Browser> InitializeBrowser()43 {44 var browserFetcher = new BrowserFetcher();45 await browserFetcher.DownloadAsync();...

Full Screen

Full Screen

PuppeteerPdfModelService.cs

Source:PuppeteerPdfModelService.cs Github

copy

Full Screen

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

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions10 {11 Args = new string[] { "--no-sandbox" }12 });13 var page = await browser.NewPageAsync();14 var pdf = await page.PdfDataAsync(new PdfOptions15 {16 {17 }18 });19 await page.CloseAsync();20 await browser.CloseAsync();21 }22 }23}

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions10 {11 });12 var page = await browser.NewPageAsync();13 var pdf = await page.PdfDataAsync(new PdfOptions14 {15 {16 }17 });18 await page.PdfAsync(@"C:\Users\Public\Documents\1.pdf", new PdfOptions19 {20 {21 }22 });

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions10 {11 Args = new[] { "--no-sandbox" }12 });13 var page = await browser.NewPageAsync();14 await page.PdfAsync("1.pdf", new PdfOptions15 {16 {17 }18 });19 await browser.CloseAsync();20 }21 }22}23using System;24using System.Threading.Tasks;25using PuppeteerSharp;26{27 {28 static async Task Main(string[] args)29 {30 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);31 var browser = await Puppeteer.LaunchAsync(new LaunchOptions32 {33 Args = new[] { "--no-sandbox" }34 });35 var page = await browser.NewPageAsync();36 await page.PdfAsync("2.pdf", new PdfOptions37 {38 });39 await browser.CloseAsync();40 }41 }42}43using System;44using System.Threading.Tasks;45using PuppeteerSharp;46{47 {48 static async Task Main(string[] args)49 {50 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);51 var browser = await Puppeteer.LaunchAsync(new LaunchOptions52 {53 Args = new[] { "--no-sandbox" }54 });55 var page = await browser.NewPageAsync();56 await page.PdfAsync("3.pdf", new PdfOptions57 {

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 MainAsync().Wait();9 }10 static async Task MainAsync()11 {12 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))13 using (var page = await browser.NewPageAsync())14 {15 await page.PdfAsync("1.pdf", new PdfOptions16 {17 {18 }19 });20 }21 }22 }23}24using System;25using System.Threading.Tasks;26using PuppeteerSharp;27{28 {29 static void Main(string[] args)30 {31 MainAsync().Wait();32 }33 static async Task MainAsync()34 {35 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))36 using (var page = await browser.NewPageAsync())37 {38 await page.PdfAsync("2.pdf", new PdfOptions39 {40 });41 }42 }43 }44}45using System;46using System.Threading.Tasks;47using PuppeteerSharp;48{49 {50 static void Main(string[] args)51 {52 MainAsync().Wait();53 }54 static async Task MainAsync()55 {56 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))57 using (var page = await browser.NewPageAsync())58 {59 await page.PdfAsync("3.pdf", new PdfOptions60 {

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

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 using (var browser = await Puppeteer.LaunchAsync(options))10 {11 var page = await browser.NewPageAsync();12 await page.SetContentAsync("<h1>PDF Margin</h1>");13 await page.PdfAsync("1.pdf", new PdfOptions14 {15 {16 }17 });18 }19 }20 }21}

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 var task = MainAsync(args);9 task.Wait();10 }11 static async Task MainAsync(string[] args)12 {13 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);14 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });15 var page = await browser.NewPageAsync();16 await page.PdfAsync("1.pdf", new PdfOptions17 {18 {19 }20 });21 await browser.CloseAsync();22 }23 }24}

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1var pdfOptions = new PdfOptions { MarginOptions = new MarginOptions { Top = "1.5in", Bottom = "1.5in", Left = "1.5in", Right = "1.5in" } };2await page.PdfAsync("1.pdf", pdfOptions);3var pdfOptions = new PdfOptions { DisplayHeaderFooter = true };4await page.PdfAsync("2.pdf", pdfOptions);5var pdfOptions = new PdfOptions { HeaderTemplate = "Header" };6await page.PdfAsync("3.pdf", pdfOptions);7var pdfOptions = new PdfOptions { FooterTemplate = "Footer" };8await page.PdfAsync("4.pdf", pdfOptions);9var pdfOptions = new PdfOptions { PrintBackground = true };10await page.PdfAsync("5.pdf", pdfOptions);11var pdfOptions = new PdfOptions { Landscape = true };12await page.PdfAsync("6.pdf", pdfOptions);13var pdfOptions = new PdfOptions { PageRanges = "1-2" };14await page.PdfAsync("7.pdf", pdfOptions);15var pdfOptions = new PdfOptions { Format = "A4" };16await page.PdfAsync("8.pdf", pdfOptions);17var pdfOptions = new PdfOptions { Width = "8.5in" };18await page.PdfAsync("9.pdf", pdfOptions);19var pdfOptions = new PdfOptions { Height = "11in" };

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

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

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