Skip to content

Commit aacc127

Browse files
committed
Fixes issues with supplying "in" to Gotenberg -- which breaks inches margins.
1 parent 7287351 commit aacc127

2 files changed

Lines changed: 21 additions & 18 deletions

File tree

lib/Domain/Dimensions/Dimension.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace Gotenberg.Sharp.API.Client.Domain.Dimensions;
5454
public sealed class Dimension(double value, DimensionUnitType unitType) : IEquatable<Dimension?>
5555
{
5656
private static readonly Regex ValidDimensionRegex =
57-
new(@"^\s*(\d+(\.\d+)?)\s*(pt|px|in|mm|cm|pc)\s*$", RegexOptions.IgnoreCase);
57+
new(@"^\s*(\d+(\.\d+)?)\s*(pt|px|in|mm|cm|pc)?\s*$", RegexOptions.IgnoreCase);
5858

5959
/// <summary>
6060
/// UnitType value
@@ -73,29 +73,28 @@ public bool Equals(Dimension? other)
7373
UnitType == other.UnitType;
7474
}
7575

76+
/// <summary>
77+
/// Parses a string like "200px", "11in", or defaults to inches if no unit is provided (e.g., "3.4").
78+
/// </summary>
79+
/// <param name="dimension"></param>
80+
/// <returns></returns>
81+
/// <exception cref="ArgumentException"></exception>
7682
public static Dimension Parse(string dimension)
7783
{
7884
if (string.IsNullOrWhiteSpace(dimension))
79-
{
80-
throw new ArgumentException("Dimension cannot be null or empty.",
81-
nameof(dimension));
82-
}
85+
throw new ArgumentException("Dimension cannot be null or empty.", nameof(dimension));
8386

8487
var match = ValidDimensionRegex.Match(dimension);
8588
if (!match.Success)
86-
{
87-
throw new ArgumentException(
88-
"Invalid dimension format. Expected formats: '200px', '11in', etc.",
89-
nameof(dimension));
90-
}
89+
throw new ArgumentException("Invalid dimension format. Expected formats: '200px', '11in', or default to inches.", nameof(dimension));
9190

92-
var value = double.Parse(match.Groups[1].Value);
93-
var unitStr = match.Groups[3].Value.ToLower();
91+
double value = double.Parse(match.Groups[1].Value);
92+
string? unitStr = match.Groups[3].Success ? match.Groups[3].Value.ToLower() : null;
9493

95-
if (!TryParseUnit(unitStr, out var unit))
96-
{
97-
throw new ArgumentException($"Unknown unitType '{unitStr}'", nameof(dimension));
98-
}
94+
// Default to Inches if no unit is provided
95+
DimensionUnitType unit = unitStr is not null && TryParseUnit(unitStr, out var parsedUnit)
96+
? parsedUnit
97+
: DimensionUnitType.Inches;
9998

10099
return new Dimension(value, unit);
101100
}
@@ -147,7 +146,10 @@ public static Dimension FromPicas(double picas)
147146

148147
public override string ToString()
149148
{
150-
return $"{Value}{UnitType.GetDescription()}";
149+
// if it's inches, don't supply anything.
150+
var unitType = UnitType == DimensionUnitType.Inches ? "" : UnitType.GetDescription();
151+
152+
return $"{Value}{unitType}";
151153
}
152154

153155
public override bool Equals(object? obj)

lib/Gotenberg.Sharp.Api.Client.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
</PropertyGroup>
1515

1616
<PropertyGroup Label="PackageInfo">
17-
<PackageVersion>2.6</PackageVersion>
17+
<PackageVersion>2.7</PackageVersion>
1818
<PackageTags>Gotenberg pdf C# ApiClient unoconv</PackageTags>
1919
<Description>
2020
C# API client for interacting with the Gotenberg v7 &amp; v8 micro-service's API, a docker-powered stateless API for converting &amp; merging HTML, Markdown and Office documents to PDF. The client supports a configurable Polly retry policy with exponential back-off for handling transient exceptions.
2121
</Description>
2222
<IncludeSymbols>True</IncludeSymbols>
2323
<PackageReleaseNotes>
24+
v2.7 - Fixes issue with "Inches".
2425
v2.6 - Updated office Extensions. Added document metadata support. Add Dimension.FromUnit() support for dimensional values.
2526
v2.5 - Renamed "Dimentions" to "PageProperties". Added support for 'GenerateDocumentOutline' and 'OmitBackground.'
2627
v2.4 - Updated dependencies. Removed Annotations. Add support for PDF/UA form field. Thank you for the PR @lennartb-!

0 commit comments

Comments
 (0)