-
Notifications
You must be signed in to change notification settings - Fork 18
/
UrlConvert.linq
62 lines (50 loc) · 2.08 KB
/
UrlConvert.linq
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
<Query Kind="Program">
<Reference Relative="..\lib\bin\Debug\net5.0\Gotenberg.Sharp.API.Client.dll">C:\Projects\GotenbergSharpApiClient\lib\bin\Debug\net5.0\Gotenberg.Sharp.API.Client.dll</Reference>
<Namespace>Gotenberg.Sharp.API.Client</Namespace>
<Namespace>Gotenberg.Sharp.API.Client.Domain.Builders</Namespace>
<Namespace>Gotenberg.Sharp.API.Client.Domain.Builders.Faceted</Namespace>
<Namespace>Gotenberg.Sharp.API.Client.Domain.Requests</Namespace>
<Namespace>Gotenberg.Sharp.API.Client.Extensions</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
static Random Rand = new Random(Math.Abs( (int) DateTime.Now.Ticks));
async Task Main()
{
var destinationPath = @"C:\Temp\Gotenberg\Dumps";
var headerFooterPath = @$"{Path.GetDirectoryName(Util.CurrentQueryPath)}\Resources\Html";;
var path = await CreateFromUrl(
destinationPath,
@$"{headerFooterPath}\UrlHeader.html",
@$"{headerFooterPath}\UrlFooter.html");
var info = new ProcessStartInfo { FileName = path, UseShellExecute = true };
Process.Start(info);
path.Dump("done");
}
public async Task<string> CreateFromUrl(string destinationPath, string headerPath, string footerPath)
{
var sharpClient = new GotenbergSharpClient("http://localhost:3000");
var builder = new UrlRequestBuilder()
.SetUrl("https://www.cnn.com")
.SetConversionBehaviors(b =>
b.EmulateAsScreen()
.SetBrowserWaitDelay(1)
.SetUserAgent(nameof(GotenbergSharpClient))
).ConfigureRequest(b => b.SetTrace("Linqpad").SetPageRanges("1-2"))
.AddAsyncHeaderFooter(async
b => b.SetHeader(await File.ReadAllBytesAsync(headerPath))
.SetFooter(await File.ReadAllBytesAsync(footerPath)
)).WithDimensions(b =>
b.SetPaperSize(PaperSizes.A4)
.UseChromeDefaults()
.MarginLeft(0)
.MarginRight(0)
);
var request = await builder.BuildAsync();
var response = await sharpClient.UrlToPdfAsync(request);
var resultPath = @$"{destinationPath}\GotenbergFromUrl-{Rand.Next()}.pdf";
using (var destinationStream = File.Create(resultPath))
{
await response.CopyToAsync(destinationStream);
}
return resultPath;
}