Best Puppeteer-sharp code snippet using PuppeteerSharp.Media.MarginOptions.MarginOptions
BrowserUtils.cs
Source:BrowserUtils.cs
...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...
ReportService.cs
Source:ReportService.cs
...98 DisplayHeaderFooter = true,99 //classæå®ã§ã®styleãå¹ããªãã100 HeaderTemplate = needCommonHeaderFooter ? $@"<div class='print_header_container' style=""font-family: 'ï¼ï¼³ ï¼°ææ', '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=''> </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 }
...
Form1.cs
Source:Form1.cs
...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 }...
UrlToPDFController.cs
Source:UrlToPDFController.cs
...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);...
GenerateStatisticQueryHandler.cs
Source:GenerateStatisticQueryHandler.cs
...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}...
PdfOptionsConverter.cs
Source:PdfOptionsConverter.cs
...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;...
MarginOptions.cs
Source:MarginOptions.cs
...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}...
PuppeteerPdfModelService.cs
Source:PuppeteerPdfModelService.cs
...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}...
MarginOptions
Using AI Code Generation
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();
MarginOptions
Using AI Code Generation
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}
MarginOptions
Using AI Code Generation
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 });
MarginOptions
Using AI Code Generation
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}
MarginOptions
Using AI Code Generation
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{
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!!