-
Notifications
You must be signed in to change notification settings - Fork 18
/
pdfConvert.linq
59 lines (44 loc) · 2.06 KB
/
pdfConvert.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
<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.Domain.Requests.Facets</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));
const int NumberOfFilesToGet = 1;
//If you get 1, the result is a pdf; get more
//and the API retuns a zip containing the results
//Currently, Gotenberg supports these formats: A1a, A2b & A3b
async Task Main()
{
var p = await DoConversion(@"C:\Temp\Gotenberg\Delivs");
var info = new ProcessStartInfo { FileName = p, UseShellExecute = true };
Process.Start(info);
p.Dump("Done");
}
async Task<string> DoConversion(string destinationPath)
{
var sharpClient = new GotenbergSharpClient("http://localhost:3000");
var items = Directory.GetFiles(destinationPath, "*.pdf", SearchOption.TopDirectoryOnly)
.Select(p => new { Info = new FileInfo(p), Path = p })
.OrderBy(item => item.Info.CreationTime)
.Take(2);
var toConvert = items.Select(item => KeyValuePair.Create(item.Info.Name, File.ReadAllBytes(item.Path)));
var builder = new PdfConversionBuilder()
.WithPdfs(b => b.AddItems(toConvert) )
.SetFormat(PdfFormats.A1a);
var request = builder.Build();
var response = await sharpClient.ConvertPdfDocumentsAsync(request);
//If you send one in -- the result is pdf.
var extension = items.Count() >1 ? "zip" : "pdf";
var outPath = @$"{destinationPath}\GotenbergConvertResult.{extension}";
using (var destinationStream = File.Create(outPath))
{
await response.CopyToAsync(destinationStream, default(CancellationToken));
}
return outPath;
}