How to use MarginOptions method of PuppeteerSharp.Media.MarginOptions class

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

BrowserUtils.cs

Source:BrowserUtils.cs Github

copy

Full Screen

...120 string overrides = string.Empty;121 if (!options.PreferCSSPageSize && options.Width != null && options.Height != null) {122 overrides += $"size: {options.Width} {options.Height}; ";123 }124 if (options.MarginOptions.Top != null) {125 overrides += $"margin-top: {options.MarginOptions.Top}; ";126 }127 if (options.MarginOptions.Right != null) {128 overrides += $"margin-right: {options.MarginOptions.Right}; ";129 }130 if (options.MarginOptions.Bottom != null) {131 overrides += $"margin-bottom: {options.MarginOptions.Bottom}; ";132 }133 if (options.MarginOptions.Left != null) {134 overrides += $"margin-left: {options.MarginOptions.Left}; ";135 }136 if (!string.IsNullOrEmpty(overrides)) {137 /* Change the options so that Puppeteer respects our overrides */138 options.PreferCSSPageSize = true;139 options.Width = options.Height = null;140 options.MarginOptions = new MarginOptions();141 /* We must add the <style> tag at the end of <body> to make sure it is not overriden */142 string pageOverrides = "@page { " + overrides + "}";143 return page.EvaluateExpressionAsync($"const style = document.createElement('style'); style.innerHTML = '{pageOverrides}'; document.head.appendChild(style);");144 } else {145 return Task.CompletedTask;146 }147 }148 public async Task<byte[]> ScreenshotPNG(string url, IEnumerable<CookieParam> cookies, ViewPortOptions viewport, ScreenshotOptions options, RevisionInfo revision) {149 LaunchOptions launchOptions = new LaunchOptions() {150 ExecutablePath = revision.ExecutablePath,151 Args = BrowserArgs,152 Headless = true,153 DefaultViewport = viewport,154 Timeout = 0...

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

UrlToPDFController.cs

Source:UrlToPDFController.cs Github

copy

Full Screen

...36 path += "\\";37 await page.PdfAsync(path + fileName, new PdfOptions()38 {39 PrintBackground = true,40 MarginOptions = new PuppeteerSharp.Media.MarginOptions()41 {42 Left = "10mm",43 Right = "10mm",44 Bottom = "10mm",45 Top = "10mm",46 },47 Format = PuppeteerSharp.Media.PaperFormat.A4,48 Landscape = false//纵向49 });50 NewLife.Log.XTrace.WriteLine("生成附件成功:" + fileName);51 }52 catch (Exception ex)53 {54 NewLife.Log.XTrace.WriteLine("生成附件失败:" + ex.Message);...

Full Screen

Full Screen

GenerateStatisticQueryHandler.cs

Source:GenerateStatisticQueryHandler.cs Github

copy

Full Screen

...50 PrintBackground = true,51 Landscape = true,52 Format = PaperFormat.A4,53 };54 options.MarginOptions.Bottom = "2cm";55 options.MarginOptions.Top = "2cm";56 options.MarginOptions.Left = "1cm";57 options.MarginOptions.Right = "1cm";58 await page.PdfAsync(Directory.GetCurrentDirectory() + "/test2.pdf",options); //debug code59 var data = await page.PdfDataAsync(options);60 61 _logger.Debug($"Created Statistic-Pdf successfull"); 62 return CustomResponse.Success(data);63 }64 catch (Exception e)65 {66 _logger.Error(e.Message);67 return CustomResponse.Error<byte[]>(400, e.Message);68 }69 }70 }71}...

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

MarginOptions.cs

Source:MarginOptions.cs Github

copy

Full Screen

...4{5 /// <summary>6 /// margin options used in <see cref="PdfOptions"/>7 /// </summary>8 public class MarginOptions : IEquatable<MarginOptions>9 {10 /// <summary>11 /// Initializes a new instance of the <see cref="PuppeteerSharp.Media.MarginOptions"/> class.12 /// </summary>13 public MarginOptions() { }14 /// <summary>15 /// Top margin, accepts values labeled with units16 /// </summary>17 public string Top { get; set; }18 /// <summary>19 /// Left margin, accepts values labeled with units20 /// </summary>21 public string Left { get; set; }22 /// <summary>23 /// Bottom margin, accepts values labeled with units24 /// </summary>25 public string Bottom { get; set; }26 /// <summary>27 /// Right margin, accepts values labeled with units28 /// </summary>29 public string Right { get; set; }30 /// <inheritdoc/>31 public override bool Equals(object obj)32 {33 if (obj == null || GetType() != obj.GetType())34 {35 return false;36 }37 return Equals((MarginOptions)obj);38 }39 /// <inheritdoc/>40 public bool Equals(MarginOptions options)41 => options != null &&42 Top == options.Top &&43 Left == options.Left &&44 Bottom == options.Bottom &&45 Right == options.Right;46 /// <inheritdoc/>47 public override int GetHashCode()48 => -48139112549 ^ EqualityComparer<string>.Default.GetHashCode(Top)50 ^ EqualityComparer<string>.Default.GetHashCode(Left)51 ^ EqualityComparer<string>.Default.GetHashCode(Bottom)52 ^ EqualityComparer<string>.Default.GetHashCode(Right);53 /// <inheritdoc/>54 public static bool operator ==(MarginOptions left, MarginOptions right)55 => EqualityComparer<MarginOptions>.Default.Equals(left, right);56 /// <inheritdoc/>57 public static bool operator !=(MarginOptions left, MarginOptions right) => !(left == right);58 }59}...

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 var browser = await Puppeteer.LaunchAsync(new LaunchOptions9 {10 });11 var page = await browser.NewPageAsync();12 await page.PdfAsync("google.pdf", new PdfOptions13 {14 {15 }16 });17 await browser.CloseAsync();18 }19 }20}21using System;22using System.Threading.Tasks;23using PuppeteerSharp;24{25 {26 static async Task Main(string[] args)27 {28 var browser = await Puppeteer.LaunchAsync(new LaunchOptions29 {30 });31 var page = await browser.NewPageAsync();32 await page.PdfAsync("google.pdf", new PdfOptions33 {34 });35 await browser.CloseAsync();36 }37 }38}39using System;40using System.Threading.Tasks;41using PuppeteerSharp;42{43 {44 static async Task Main(string[] args)45 {46 var browser = await Puppeteer.LaunchAsync(new LaunchOptions47 {48 });49 var page = await browser.NewPageAsync();50 await page.PdfAsync("google.pdf", new PdfOptions51 {52 });53 await browser.CloseAsync();

Full Screen

Full Screen

MarginOptions

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 LaunchOptions10 {11 });12 var page = await browser.NewPageAsync();13 await page.PdfAsync("C:\\Users\\Downloads\\google.pdf", new PdfOptions14 {15 {16 }17 });18 await browser.CloseAsync();19 }20 }21}

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1var marginOptions = new PuppeteerSharp.Media.MarginOptions();2marginOptions.Top = 10;3marginOptions.Left = 10;4marginOptions.Right = 10;5marginOptions.Bottom = 10;6await page.PdfAsync("2.pdf", new PuppeteerSharp.PdfOptions { Format = "A4", MarginOptions = marginOptions });7await page.PdfAsync("3.pdf", new PuppeteerSharp.PdfOptions { Format = "A4", PaperFormat = PuppeteerSharp.Media.PaperFormat.A4 });8await page.PdfAsync("4.pdf", new PuppeteerSharp.PdfOptions { PaperWidth = 8.5, PaperHeight = 11 });9await page.PdfAsync("5.pdf", new PuppeteerSharp.PdfOptions { Format = "A4", Landscape = true });10await page.PdfAsync("6.pdf", new PuppeteerSharp.PdfOptions { Format = "A4", Landscape = false });11await page.PdfAsync("7.pdf", new PuppeteerSharp.PdfOptions { Format = "A4", DisplayHeaderFooter = true });12await page.PdfAsync("8.pdf", new PuppeteerSharp.PdfOptions { Format = "A4", Scale = 1 });13await page.PdfAsync("9.pdf", new PuppeteerSharp.PdfOptions { Format = "A4", PrintBackground = true });14await page.PdfAsync("10.pdf", new PuppeteerSharp.PdfOptions { Format = "A4", PreferCSSPageSize = true });

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using System.Threading.Tasks;2using PuppeteerSharp;3{4 {5 public static async Task MarginOptionsSample()6 {7 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))8 using (var page = await browser.NewPageAsync())9 {10 await page.PdfAsync("C:\\Users\\Catalin\\Desktop\\MarginOptions.pdf", new PuppeteerSharp.Media.PdfOptions { MarginOptions = new PuppeteerSharp.Media.MarginOptions { Top = "20px", Bottom = "20px", Left = "20px", Right = "20px" } });11 }12 }13 }14}

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1{2};3await page.PdfAsync("2.pdf", new PdfOptions { MarginOptions = marginOptions });4{5};6await page.PdfAsync("3.pdf", new PdfOptions { MarginOptions = marginOptions });7{8};9await page.PdfAsync("4.pdf", new PdfOptions { MarginOptions = marginOptions });10{11};12await page.PdfAsync("5.pdf", new PdfOptions { MarginOptions = marginOptions });13{14};15await page.PdfAsync("6.pdf", new PdfOptions { MarginOptions = marginOptions });16{17};18await page.PdfAsync("7.pdf", new PdfOptions { MarginOptions = marginOptions });19{

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 MarginOptions

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful