forked from ghasctl/puppeteer-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPdfTests.cs
103 lines (92 loc) · 3.74 KB
/
PdfTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
using PuppeteerSharp.Media;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;
namespace PuppeteerSharp.Tests.PageTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class PdfTests : PuppeteerPageBaseTest
{
public PdfTests(ITestOutputHelper output) : base(output)
{
}
[PuppeteerFact(Timeout = -1)]
public async Task Usage()
{
var outputFile = Path.Combine(BaseDirectory, "Usage.pdf");
var fileInfo = new FileInfo(outputFile);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
#region PdfAsync
using var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync();
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions {Headless = true});
await using var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com"); // In case of fonts being loaded from a CDN, use WaitUntilNavigation.Networkidle0 as a second param.
await page.EvaluateExpressionHandleAsync("document.fonts.ready"); // Wait for fonts to be loaded. Omitting this might result in no text rendered in pdf.
await page.PdfAsync(outputFile);
#endregion
Assert.True(File.Exists(outputFile));
}
[PuppeteerTest("page.spec.ts", "printing to PDF", "can print to PDF and save to file")]
[PuppeteerFact]
public async Task ShouldBeAbleToSaveFile()
{
var outputFile = Path.Combine(BaseDirectory, "output.pdf");
var fileInfo = new FileInfo(outputFile);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
await Page.PdfAsync(outputFile);
fileInfo = new FileInfo(outputFile);
Assert.True(new FileInfo(outputFile).Length > 0);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
}
[PuppeteerTest("page.spec.ts", "printing to PDF", "can print to PDF and stream the result")]
[PuppeteerFact]
public async Task CanPrintToPDFAndStreamTheResult()
{
// We test this differently compared to puppeteer.
// We will compare that we can get to the same file using both PDF methods
var outputFile = Path.Combine(BaseDirectory, "output.pdf");
var fileInfo = new FileInfo(outputFile);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
await Page.PdfAsync(outputFile);
var stream = await Page.PdfStreamAsync();
Assert.Equal(new FileInfo(outputFile).Length, stream.Length);
}
[PuppeteerFact]
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);
}
}
}