Skip to content

Commit a53335a

Browse files
authored
Introduce OmitBackground option on PrintToPdf (#1812)
1 parent 644643d commit a53335a

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

lib/PuppeteerSharp/Page.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,11 @@ internal async Task<byte[]> PdfInternalAsync(string file, PdfOptions options)
986986
var marginBottom = ConvertPrintParameterToInches(options.MarginOptions.Bottom);
987987
var marginRight = ConvertPrintParameterToInches(options.MarginOptions.Right);
988988

989+
if (options.OmitBackground)
990+
{
991+
await SetTransparentBackgroundColorAsync().ConfigureAwait(false);
992+
}
993+
989994
var result = await Client.SendAsync<PagePrintToPDFResponse>("Page.printToPDF", new PagePrintToPDFRequest
990995
{
991996
TransferMode = "ReturnAsStream",
@@ -1005,6 +1010,11 @@ internal async Task<byte[]> PdfInternalAsync(string file, PdfOptions options)
10051010
PreferCSSPageSize = options.PreferCSSPageSize
10061011
}).ConfigureAwait(false);
10071012

1013+
if (options.OmitBackground)
1014+
{
1015+
await ResetDefaultBackgroundColorAsync().ConfigureAwait(false);
1016+
}
1017+
10081018
return await ProtocolStreamReader.ReadProtocolStreamByteAsync(Client, result.Stream, file).ConfigureAwait(false);
10091019
}
10101020

@@ -1976,16 +1986,7 @@ private async Task<string> PerformScreenshot(ScreenshotType type, ScreenshotOpti
19761986

19771987
if (options?.OmitBackground == true && type == ScreenshotType.Png)
19781988
{
1979-
await Client.SendAsync("Emulation.setDefaultBackgroundColorOverride", new EmulationSetDefaultBackgroundColorOverrideRequest
1980-
{
1981-
Color = new EmulationSetDefaultBackgroundColorOverrideColor
1982-
{
1983-
R = 0,
1984-
G = 0,
1985-
B = 0,
1986-
A = 0
1987-
}
1988-
}).ConfigureAwait(false);
1989+
await SetTransparentBackgroundColorAsync().ConfigureAwait(false);
19891990
}
19901991
}
19911992

@@ -2036,12 +2037,27 @@ private Clip ProcessClip(Clip clip)
20362037
private Task ResetBackgroundColorAndViewportAsync(ScreenshotOptions options)
20372038
{
20382039
var omitBackgroundTask = options?.OmitBackground == true && options.Type == ScreenshotType.Png ?
2039-
Client.SendAsync("Emulation.setDefaultBackgroundColorOverride") : Task.CompletedTask;
2040+
ResetDefaultBackgroundColorAsync() : Task.CompletedTask;
20402041
var setViewPortTask = (options?.FullPage == true && Viewport != null) ?
20412042
SetViewportAsync(Viewport) : Task.CompletedTask;
20422043
return Task.WhenAll(omitBackgroundTask, setViewPortTask);
20432044
}
20442045

2046+
private Task ResetDefaultBackgroundColorAsync()
2047+
=> Client.SendAsync("Emulation.setDefaultBackgroundColorOverride");
2048+
2049+
private Task SetTransparentBackgroundColorAsync()
2050+
=> Client.SendAsync("Emulation.setDefaultBackgroundColorOverride", new EmulationSetDefaultBackgroundColorOverrideRequest
2051+
{
2052+
Color = new EmulationSetDefaultBackgroundColorOverrideColor
2053+
{
2054+
R = 0,
2055+
G = 0,
2056+
B = 0,
2057+
A = 0
2058+
}
2059+
});
2060+
20452061
private decimal ConvertPrintParameterToInches(object parameter)
20462062
{
20472063
if (parameter == null)

lib/PuppeteerSharp/PdfOptions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public PdfOptions()
8787
/// </summary>
8888
public bool PreferCSSPageSize { get; set; }
8989

90+
/// <summary>
91+
/// Hides default white background and allows generating pdfs with transparency.
92+
/// </summary>
93+
public bool OmitBackground { get; set; }
94+
9095
/// <inheritdoc/>
9196
public override bool Equals(object obj)
9297
{
@@ -112,7 +117,8 @@ public bool Equals(PdfOptions options)
112117
EqualityComparer<object>.Default.Equals(Width, options.Width) &&
113118
EqualityComparer<object>.Default.Equals(Height, options.Height) &&
114119
EqualityComparer<MarginOptions>.Default.Equals(MarginOptions, options.MarginOptions) &&
115-
PreferCSSPageSize == options.PreferCSSPageSize;
120+
PreferCSSPageSize == options.PreferCSSPageSize &&
121+
OmitBackground == options.OmitBackground;
116122

117123
/// <inheritdoc/>
118124
public override int GetHashCode()
@@ -122,6 +128,7 @@ public override int GetHashCode()
122128
^ EqualityComparer<string>.Default.GetHashCode(HeaderTemplate)
123129
^ EqualityComparer<string>.Default.GetHashCode(FooterTemplate)
124130
^ PrintBackground.GetHashCode()
131+
^ OmitBackground.GetHashCode()
125132
^ Landscape.GetHashCode()
126133
^ EqualityComparer<string>.Default.GetHashCode(PageRanges)
127134
^ EqualityComparer<PaperFormat>.Default.GetHashCode(Format)

0 commit comments

Comments
 (0)