Skip to content

Commit 48b4a63

Browse files
authored
Split PdfAsync and PdfStreamAsync functionalities (#66)
1 parent 22687ed commit 48b4a63

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

lib/PuppeteerSharp.Tests/Page/PdfTests.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ public async Task ShouldBeAbleToSaveFile()
2222
}
2323

2424
var page = await Browser.NewPageAsync();
25-
await page.PdfAsync(new PdfOptions
26-
{
27-
Path = outputFile
28-
});
25+
await page.PdfAsync(outputFile);
2926

3027
fileInfo = new FileInfo(outputFile);
3128
Assert.True(new FileInfo(outputFile).Length > 0);
@@ -41,7 +38,7 @@ public async Task ShouldDefaultToPrintInLetterFormat()
4138
{
4239
var page = await Browser.NewPageAsync();
4340

44-
var document = PdfReader.Open(await page.PdfAsync(), PdfDocumentOpenMode.ReadOnly);
41+
var document = PdfReader.Open(await page.PdfStreamAsync(), PdfDocumentOpenMode.ReadOnly);
4542

4643
Assert.Equal(1, document.Pages.Count);
4744
Assert.Equal(8.5, TruncateDouble(document.Pages[0].Width.Inch, 1));
@@ -53,7 +50,7 @@ public async Task ShouldSupportSettingCustomFormat()
5350
{
5451
var page = await Browser.NewPageAsync();
5552

56-
var document = PdfReader.Open(await page.PdfAsync(new PdfOptions
53+
var document = PdfReader.Open(await page.PdfStreamAsync(new PdfOptions
5754
{
5855
Format = "a4"
5956
}), PdfDocumentOpenMode.ReadOnly);
@@ -68,7 +65,7 @@ public async Task ShouldSupportSettingPaperWidthAndHeight()
6865
{
6966
var page = await Browser.NewPageAsync();
7067

71-
var document = PdfReader.Open(await page.PdfAsync(new PdfOptions
68+
var document = PdfReader.Open(await page.PdfStreamAsync(new PdfOptions
7269
{
7370
Width = "10in",
7471
Height = "10in"
@@ -87,7 +84,7 @@ public async Task ShouldPrintMultiplePages()
8784
// Define width and height in CSS pixels.
8885
var width = 50 * 5 + 1;
8986
var height = 50 * 5 + 1;
90-
var document = PdfReader.Open(await page.PdfAsync(new PdfOptions
87+
var document = PdfReader.Open(await page.PdfStreamAsync(new PdfOptions
9188
{
9289
Width = width,
9390
Height = height
@@ -106,7 +103,7 @@ public async Task ShouldSupportPageRanges()
106103
// Define width and height in CSS pixels.
107104
var width = 50 * 5 + 1;
108105
var height = 50 * 5 + 1;
109-
var document = PdfReader.Open(await page.PdfAsync(new PdfOptions
106+
var document = PdfReader.Open(await page.PdfStreamAsync(new PdfOptions
110107
{
111108
Width = width,
112109
Height = height,
@@ -122,7 +119,7 @@ public async Task ShowThrowFormatIsUnknown()
122119
var page = await Browser.NewPageAsync();
123120
var exception = await Assert.ThrowsAsync<ArgumentException>(async () =>
124121
{
125-
await page.PdfAsync(new PdfOptions
122+
await page.PdfStreamAsync(new PdfOptions
126123
{
127124
Format = "something"
128125
});
@@ -137,7 +134,7 @@ public async Task ShouldThrowIfUnitsAreUnknown()
137134
var page = await Browser.NewPageAsync();
138135
var exception = await Assert.ThrowsAsync<ArgumentException>(async () =>
139136
{
140-
await page.PdfAsync(new PdfOptions
137+
await page.PdfStreamAsync(new PdfOptions
141138
{
142139
Width = "10em"
143140
});

lib/PuppeteerSharp.Tests/Page/ScreenshotTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ public async Task ShouldRunInParallelInMultiplePages()
188188
for (var i = 0; i < n; i++)
189189
{
190190
Assert.True(PixelMatch($"grid-cell-{i}.png", screenshotTasks[i].Result));
191-
192191
}
193192

194193
var closeTasks = new List<Task>();

lib/PuppeteerSharp.Tests/TestConstants.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,5 @@ public static class TestConstants
1919
{ "timeout", 0},
2020
{ "keepAliveInterval", 120}
2121
};
22-
23-
2422
}
2523
}

lib/PuppeteerSharp/Page.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,23 @@ await Task.WhenAll(
304304
return request?.Response;
305305
}
306306

307-
public async Task<Stream> PdfAsync() => await PdfAsync(new PdfOptions());
307+
public async Task PdfAsync(string file) => await PdfAsync(file, new PdfOptions());
308308

309-
public async Task<Stream> PdfAsync(PdfOptions options)
309+
public async Task PdfAsync(string file, PdfOptions options)
310+
{
311+
var stream = await PdfStreamAsync(options);
312+
313+
using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write))
314+
{
315+
byte[] bytesInStream = new byte[stream.Length];
316+
stream.Read(bytesInStream, 0, bytesInStream.Length);
317+
fs.Write(bytesInStream, 0, bytesInStream.Length);
318+
}
319+
}
320+
321+
public async Task<Stream> PdfStreamAsync() => await PdfStreamAsync(new PdfOptions());
322+
323+
public async Task<Stream> PdfStreamAsync(PdfOptions options)
310324
{
311325
var paperWidth = 8.5m;
312326
var paperHeight = 11m;
@@ -357,33 +371,24 @@ public async Task<Stream> PdfAsync(PdfOptions options)
357371
});
358372

359373
var buffer = Convert.FromBase64String(result.GetValue("data").Value<string>());
360-
361-
if (!string.IsNullOrEmpty(options.Path))
362-
{
363-
using (var fs = new FileStream(options.Path, FileMode.Create, FileAccess.Write))
364-
{
365-
fs.Write(buffer, 0, buffer.Length);
366-
}
367-
}
368-
369374
return new MemoryStream(buffer);
370375
}
371376

372377
public async Task SetViewport(ViewPortOptions viewport)
373378
{
374379
var needsReload = await _emulationManager.EmulateViewport(_client, viewport);
375380
_viewport = viewport;
381+
376382
if (needsReload)
377383
{
378-
await Reload();
384+
await ReloadAsync();
379385
}
380386
}
381387

382388
public async Task ScreenshotAsync(string file) => await ScreenshotAsync(file, new ScreenshotOptions());
383389

384390
public async Task ScreenshotAsync(string file, ScreenshotOptions options)
385391
{
386-
387392
var fileInfo = new FileInfo(file);
388393
options.Type = fileInfo.Extension.Replace(".", string.Empty);
389394

@@ -666,7 +671,7 @@ private void OnConsoleAPI(MessageEventArgs e)
666671
throw new NotImplementedException();
667672
}
668673

669-
private Task Reload()
674+
private async Task ReloadAsync()
670675
{
671676
throw new NotImplementedException();
672677
}

lib/PuppeteerSharp/PdfOptions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,5 @@ public PdfOptions()
2323
public object Width { get; set; }
2424
public object Height { get; set; }
2525
public MarginOptions MarginOptions { get; set; }
26-
public string Path { get; set; }
2726
}
2827
}

0 commit comments

Comments
 (0)