Skip to content

Commit a11fa94

Browse files
authored
PdfOptions should be serializable (#1028)
* PdfOptions should be serializable * Code Factor
1 parent 9398dff commit a11fa94

File tree

4 files changed

+164
-6
lines changed

4 files changed

+164
-6
lines changed

lib/PuppeteerSharp.Tests/PageTests/PdfTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.IO;
22
using System.Threading.Tasks;
3+
using Newtonsoft.Json;
4+
using PuppeteerSharp.Media;
35
using Xunit;
46
using Xunit.Abstractions;
57

@@ -29,5 +31,27 @@ public async Task ShouldBeAbleToSaveFile()
2931
fileInfo.Delete();
3032
}
3133
}
34+
35+
[Fact]
36+
public void PdfOptionsShouldBeSerializable()
37+
{
38+
var pdfOptions = new PdfOptions
39+
{
40+
Format = PaperFormat.A4,
41+
DisplayHeaderFooter = true,
42+
MarginOptions = new MarginOptions
43+
{
44+
Top = "20px",
45+
Right = "20px",
46+
Bottom = "40px",
47+
Left = "20px"
48+
},
49+
FooterTemplate = "<div id=\"footer-template\" style=\"font-size:10px !important; color:#808080; padding-left:10px\">- <span class=\"pageNumber\"></span> - </div>"
50+
};
51+
52+
var serialized = JsonConvert.SerializeObject(pdfOptions);
53+
var newPdfOptions = JsonConvert.DeserializeObject<PdfOptions>(serialized);
54+
Assert.Equal(pdfOptions, newPdfOptions);
55+
}
3256
}
3357
}

lib/PuppeteerSharp/Media/MarginOptions.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
namespace PuppeteerSharp.Media
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace PuppeteerSharp.Media
25
{
36
/// <summary>
47
/// margin options used in <see cref="PdfOptions"/>
58
/// </summary>
6-
public class MarginOptions
9+
public class MarginOptions : IEquatable<MarginOptions>
710
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="PuppeteerSharp.Media.MarginOptions"/> class.
13+
/// </summary>
14+
public MarginOptions() { }
15+
816
/// <summary>
917
/// Top margin, accepts values labeled with units
1018
/// </summary>
@@ -24,5 +32,38 @@ public class MarginOptions
2432
/// Right margin, accepts values labeled with units
2533
/// </summary>
2634
public string Right { get; set; }
35+
36+
/// <inheritdoc/>
37+
public override bool Equals(object obj)
38+
{
39+
if (obj == null || GetType() != obj.GetType())
40+
{
41+
return false;
42+
}
43+
44+
return Equals((MarginOptions)obj);
45+
}
46+
47+
/// <inheritdoc/>
48+
public bool Equals(MarginOptions options)
49+
=> options != null &&
50+
Top == options.Top &&
51+
Left == options.Left &&
52+
Bottom == options.Bottom &&
53+
Right == options.Right;
54+
55+
/// <inheritdoc/>
56+
public override int GetHashCode()
57+
=> -481391125
58+
^ EqualityComparer<string>.Default.GetHashCode(Top)
59+
^ EqualityComparer<string>.Default.GetHashCode(Left)
60+
^ EqualityComparer<string>.Default.GetHashCode(Bottom)
61+
^ EqualityComparer<string>.Default.GetHashCode(Right);
62+
63+
/// <inheritdoc/>
64+
public static bool operator ==(MarginOptions left, MarginOptions right)
65+
=> EqualityComparer<MarginOptions>.Default.Equals(left, right);
66+
/// <inheritdoc/>
67+
public static bool operator !=(MarginOptions left, MarginOptions right) => !(left == right);
2768
}
2869
}

lib/PuppeteerSharp/Media/PaperFormat.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
namespace PuppeteerSharp.Media
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace PuppeteerSharp.Media
25
{
36
/// <summary>
47
/// Paper format.
58
/// </summary>
69
/// <seealso cref="PdfOptions.Format"/>
7-
public class PaperFormat
10+
public class PaperFormat : IEquatable<PaperFormat>
811
{
12+
private PaperFormat() { }
913
private PaperFormat(decimal width, decimal height)
1014
{
1115
Width = width;
@@ -68,5 +72,35 @@ private PaperFormat(decimal width, decimal height)
6872
/// A6.
6973
/// </summary>
7074
public static readonly PaperFormat A6 = new PaperFormat(4.13m, 5.83m);
75+
76+
/// <inheritdoc/>
77+
public override bool Equals(object obj)
78+
{
79+
if (obj == null || GetType() != obj.GetType())
80+
{
81+
return false;
82+
}
83+
84+
return Equals((PaperFormat)obj);
85+
}
86+
87+
/// <inheritdoc/>
88+
public bool Equals(PaperFormat format)
89+
=> format != null &&
90+
Width == format.Width &&
91+
Height == format.Height;
92+
93+
/// <inheritdoc/>
94+
public override int GetHashCode()
95+
=> 859600377
96+
^ Width.GetHashCode()
97+
^ Height.GetHashCode();
98+
99+
/// <inheritdoc/>
100+
public static bool operator ==(PaperFormat left, PaperFormat right)
101+
=> EqualityComparer<PaperFormat>.Default.Equals(left, right);
102+
103+
/// <inheritdoc/>
104+
public static bool operator !=(PaperFormat left, PaperFormat right) => !(left == right);
71105
}
72106
}

lib/PuppeteerSharp/PdfOptions.cs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
using PuppeteerSharp.Media;
1+
using System;
2+
using System.Collections.Generic;
3+
using PuppeteerSharp.Media;
24

35
namespace PuppeteerSharp
46
{
57
/// <summary>
68
/// Options to be used in <see cref="Page.PdfAsync(string, PdfOptions)"/>, <see cref="Page.PdfStreamAsync(PdfOptions)"/> and <see cref="Page.PdfDataAsync(PdfOptions)"/>
79
/// </summary>
8-
public class PdfOptions
10+
public class PdfOptions : IEquatable<PdfOptions>
911
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="PuppeteerSharp.PdfOptions"/> class.
14+
/// </summary>
15+
public PdfOptions()
16+
{
17+
}
18+
1019
/// <summary>
1120
/// Scale of the webpage rendering. Defaults to <c>1</c>. Scale amount must be between 0.1 and 2.
1221
/// </summary>
@@ -77,5 +86,55 @@ public class PdfOptions
7786
/// Defaults to <c>false</c>, which will scale the content to fit the paper size.
7887
/// </summary>
7988
public bool PreferCSSPageSize { get; set; }
89+
90+
/// <inheritdoc/>
91+
public override bool Equals(object obj)
92+
{
93+
if (obj == null || GetType() != obj.GetType())
94+
{
95+
return false;
96+
}
97+
98+
return Equals((PdfOptions)obj);
99+
}
100+
101+
/// <inheritdoc/>
102+
public bool Equals(PdfOptions options)
103+
=> options != null &&
104+
Scale == options.Scale &&
105+
DisplayHeaderFooter == options.DisplayHeaderFooter &&
106+
HeaderTemplate == options.HeaderTemplate &&
107+
FooterTemplate == options.FooterTemplate &&
108+
PrintBackground == options.PrintBackground &&
109+
Landscape == options.Landscape &&
110+
PageRanges == options.PageRanges &&
111+
EqualityComparer<PaperFormat>.Default.Equals(Format, options.Format) &&
112+
EqualityComparer<object>.Default.Equals(Width, options.Width) &&
113+
EqualityComparer<object>.Default.Equals(Height, options.Height) &&
114+
EqualityComparer<MarginOptions>.Default.Equals(MarginOptions, options.MarginOptions) &&
115+
PreferCSSPageSize == options.PreferCSSPageSize;
116+
117+
/// <inheritdoc/>
118+
public override int GetHashCode()
119+
=> -711844102
120+
^ Scale.GetHashCode()
121+
^ DisplayHeaderFooter.GetHashCode()
122+
^ EqualityComparer<string>.Default.GetHashCode(HeaderTemplate)
123+
^ EqualityComparer<string>.Default.GetHashCode(FooterTemplate)
124+
^ PrintBackground.GetHashCode()
125+
^ Landscape.GetHashCode()
126+
^ EqualityComparer<string>.Default.GetHashCode(PageRanges)
127+
^ EqualityComparer<PaperFormat>.Default.GetHashCode(Format)
128+
^ EqualityComparer<object>.Default.GetHashCode(Width)
129+
^ EqualityComparer<object>.Default.GetHashCode(Height)
130+
^ EqualityComparer<MarginOptions>.Default.GetHashCode(MarginOptions)
131+
^ PreferCSSPageSize.GetHashCode();
132+
133+
/// <inheritdoc/>
134+
public static bool operator ==(PdfOptions left, PdfOptions right)
135+
=> EqualityComparer<PdfOptions>.Default.Equals(left, right);
136+
137+
/// <inheritdoc/>
138+
public static bool operator !=(PdfOptions left, PdfOptions right) => !(left == right);
80139
}
81140
}

0 commit comments

Comments
 (0)