Skip to content

Commit

Permalink
PdfOptions should be serializable (#1028)
Browse files Browse the repository at this point in the history
* PdfOptions should be serializable

* Code Factor
  • Loading branch information
kblok authored Mar 22, 2019
1 parent 9398dff commit a11fa94
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 6 deletions.
24 changes: 24 additions & 0 deletions lib/PuppeteerSharp.Tests/PageTests/PdfTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
using PuppeteerSharp.Media;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -29,5 +31,27 @@ public async Task ShouldBeAbleToSaveFile()
fileInfo.Delete();
}
}

[Fact]
public void PdfOptionsShouldBeSerializable()
{
var pdfOptions = new PdfOptions
{
Format = PaperFormat.A4,
DisplayHeaderFooter = true,
MarginOptions = new MarginOptions
{
Top = "20px",
Right = "20px",
Bottom = "40px",
Left = "20px"
},
FooterTemplate = "<div id=\"footer-template\" style=\"font-size:10px !important; color:#808080; padding-left:10px\">- <span class=\"pageNumber\"></span> - </div>"
};

var serialized = JsonConvert.SerializeObject(pdfOptions);
var newPdfOptions = JsonConvert.DeserializeObject<PdfOptions>(serialized);
Assert.Equal(pdfOptions, newPdfOptions);
}
}
}
45 changes: 43 additions & 2 deletions lib/PuppeteerSharp/Media/MarginOptions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
namespace PuppeteerSharp.Media
using System;
using System.Collections.Generic;

namespace PuppeteerSharp.Media
{
/// <summary>
/// margin options used in <see cref="PdfOptions"/>
/// </summary>
public class MarginOptions
public class MarginOptions : IEquatable<MarginOptions>
{
/// <summary>
/// Initializes a new instance of the <see cref="PuppeteerSharp.Media.MarginOptions"/> class.
/// </summary>
public MarginOptions() { }

/// <summary>
/// Top margin, accepts values labeled with units
/// </summary>
Expand All @@ -24,5 +32,38 @@ public class MarginOptions
/// Right margin, accepts values labeled with units
/// </summary>
public string Right { get; set; }

/// <inheritdoc/>
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}

return Equals((MarginOptions)obj);
}

/// <inheritdoc/>
public bool Equals(MarginOptions options)
=> options != null &&
Top == options.Top &&
Left == options.Left &&
Bottom == options.Bottom &&
Right == options.Right;

/// <inheritdoc/>
public override int GetHashCode()
=> -481391125
^ EqualityComparer<string>.Default.GetHashCode(Top)
^ EqualityComparer<string>.Default.GetHashCode(Left)
^ EqualityComparer<string>.Default.GetHashCode(Bottom)
^ EqualityComparer<string>.Default.GetHashCode(Right);

/// <inheritdoc/>
public static bool operator ==(MarginOptions left, MarginOptions right)
=> EqualityComparer<MarginOptions>.Default.Equals(left, right);
/// <inheritdoc/>
public static bool operator !=(MarginOptions left, MarginOptions right) => !(left == right);
}
}
38 changes: 36 additions & 2 deletions lib/PuppeteerSharp/Media/PaperFormat.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
namespace PuppeteerSharp.Media
using System;
using System.Collections.Generic;

namespace PuppeteerSharp.Media
{
/// <summary>
/// Paper format.
/// </summary>
/// <seealso cref="PdfOptions.Format"/>
public class PaperFormat
public class PaperFormat : IEquatable<PaperFormat>
{
private PaperFormat() { }
private PaperFormat(decimal width, decimal height)
{
Width = width;
Expand Down Expand Up @@ -68,5 +72,35 @@ private PaperFormat(decimal width, decimal height)
/// A6.
/// </summary>
public static readonly PaperFormat A6 = new PaperFormat(4.13m, 5.83m);

/// <inheritdoc/>
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}

return Equals((PaperFormat)obj);
}

/// <inheritdoc/>
public bool Equals(PaperFormat format)
=> format != null &&
Width == format.Width &&
Height == format.Height;

/// <inheritdoc/>
public override int GetHashCode()
=> 859600377
^ Width.GetHashCode()
^ Height.GetHashCode();

/// <inheritdoc/>
public static bool operator ==(PaperFormat left, PaperFormat right)
=> EqualityComparer<PaperFormat>.Default.Equals(left, right);

/// <inheritdoc/>
public static bool operator !=(PaperFormat left, PaperFormat right) => !(left == right);
}
}
63 changes: 61 additions & 2 deletions lib/PuppeteerSharp/PdfOptions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
using PuppeteerSharp.Media;
using System;
using System.Collections.Generic;
using PuppeteerSharp.Media;

namespace PuppeteerSharp
{
/// <summary>
/// Options to be used in <see cref="Page.PdfAsync(string, PdfOptions)"/>, <see cref="Page.PdfStreamAsync(PdfOptions)"/> and <see cref="Page.PdfDataAsync(PdfOptions)"/>
/// </summary>
public class PdfOptions
public class PdfOptions : IEquatable<PdfOptions>
{
/// <summary>
/// Initializes a new instance of the <see cref="PuppeteerSharp.PdfOptions"/> class.
/// </summary>
public PdfOptions()
{
}

/// <summary>
/// Scale of the webpage rendering. Defaults to <c>1</c>. Scale amount must be between 0.1 and 2.
/// </summary>
Expand Down Expand Up @@ -77,5 +86,55 @@ public class PdfOptions
/// Defaults to <c>false</c>, which will scale the content to fit the paper size.
/// </summary>
public bool PreferCSSPageSize { get; set; }

/// <inheritdoc/>
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}

return Equals((PdfOptions)obj);
}

/// <inheritdoc/>
public bool Equals(PdfOptions options)
=> options != null &&
Scale == options.Scale &&
DisplayHeaderFooter == options.DisplayHeaderFooter &&
HeaderTemplate == options.HeaderTemplate &&
FooterTemplate == options.FooterTemplate &&
PrintBackground == options.PrintBackground &&
Landscape == options.Landscape &&
PageRanges == options.PageRanges &&
EqualityComparer<PaperFormat>.Default.Equals(Format, options.Format) &&
EqualityComparer<object>.Default.Equals(Width, options.Width) &&
EqualityComparer<object>.Default.Equals(Height, options.Height) &&
EqualityComparer<MarginOptions>.Default.Equals(MarginOptions, options.MarginOptions) &&
PreferCSSPageSize == options.PreferCSSPageSize;

/// <inheritdoc/>
public override int GetHashCode()
=> -711844102
^ Scale.GetHashCode()
^ DisplayHeaderFooter.GetHashCode()
^ EqualityComparer<string>.Default.GetHashCode(HeaderTemplate)
^ EqualityComparer<string>.Default.GetHashCode(FooterTemplate)
^ PrintBackground.GetHashCode()
^ Landscape.GetHashCode()
^ EqualityComparer<string>.Default.GetHashCode(PageRanges)
^ EqualityComparer<PaperFormat>.Default.GetHashCode(Format)
^ EqualityComparer<object>.Default.GetHashCode(Width)
^ EqualityComparer<object>.Default.GetHashCode(Height)
^ EqualityComparer<MarginOptions>.Default.GetHashCode(MarginOptions)
^ PreferCSSPageSize.GetHashCode();

/// <inheritdoc/>
public static bool operator ==(PdfOptions left, PdfOptions right)
=> EqualityComparer<PdfOptions>.Default.Equals(left, right);

/// <inheritdoc/>
public static bool operator !=(PdfOptions left, PdfOptions right) => !(left == right);
}
}

0 comments on commit a11fa94

Please sign in to comment.