Skip to content

Commit ff2da3d

Browse files
committed
#1 Allowing to specify the pdftk executable.
1 parent 0549d73 commit ff2da3d

File tree

5 files changed

+110
-13
lines changed

5 files changed

+110
-13
lines changed

src/Kevsoft.PDFtk/PDFtk.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ public sealed class PDFtk : IPDFtk
1616
/// Initializes a new instance of the PDFtk class.
1717
/// </summary>
1818
public PDFtk()
19+
: this(PDFtkOptions.Default())
20+
{
21+
}
22+
23+
/// <inheritdoc cref="PDFtk()"/>
24+
/// <param name="options">The options to use.</param>
25+
public PDFtk(PDFtkOptions options)
1926
{
2027
_xfdfGenerator = new XfdfGenerator();
21-
_pdftkProcess = new PDFtkProcess();
28+
_pdftkProcess = new PDFtkProcess(options);
2229
}
2330

2431
/// <inheritdoc/>
@@ -61,16 +68,16 @@ public PDFtk()
6168
public async Task<IPDFtkResult<byte[]>> GetPagesAsync(byte[] pdfFile, params int[] pages)
6269
{
6370
using var inputFile = await TempPDFtkFile.FromAsync(pdfFile);
64-
71+
6572
return await GetPagesAsync(inputFile.TempFileName, pages);
6673
}
67-
68-
74+
75+
6976
/// <inheritdoc/>
7077
public async Task<IPDFtkResult<byte[]>> GetPagesAsync(Stream pdfFile, params int[] pages)
7178
{
7279
using var inputFile = await TempPDFtkFile.FromAsync(pdfFile);
73-
80+
7481
return await GetPagesAsync(inputFile.TempFileName, pages);
7582
}
7683

@@ -127,7 +134,7 @@ public async Task<IPDFtkResult<IDataField[]>> GetDataFieldsAsync(byte[] pdfFile)
127134

128135
return await GetDataFieldsAsync(inputFile.TempFileName);
129136
}
130-
137+
131138
/// <inheritdoc/>
132139
public async Task<IPDFtkResult<IDataField[]>> GetDataFieldsAsync(Stream pdfFile)
133140
{
@@ -160,7 +167,7 @@ public async Task<IPDFtkResult<byte[]>> ConcatAsync(IEnumerable<byte[]> pdfFiles
160167
var inputFiles = await Task.WhenAll(
161168
pdfFiles.Select(async file => await TempPDFtkFile.FromAsync(file))
162169
.ToList());
163-
170+
164171
try
165172
{
166173
return await ConcatAsync(inputFiles.Select(x => x.TempFileName));
@@ -170,14 +177,14 @@ public async Task<IPDFtkResult<byte[]>> ConcatAsync(IEnumerable<byte[]> pdfFiles
170177
inputFiles.Dispose();
171178
}
172179
}
173-
180+
174181
/// <inheritdoc/>
175182
public async Task<IPDFtkResult<byte[]>> ConcatAsync(IEnumerable<Stream> pdfFiles)
176183
{
177184
var inputFiles = await Task.WhenAll(
178185
pdfFiles.Select(async file => await TempPDFtkFile.FromAsync(file))
179186
.ToList());
180-
187+
181188
try
182189
{
183190
return await ConcatAsync(inputFiles.Select(x => x.TempFileName));
@@ -208,7 +215,7 @@ public async Task<IPDFtkResult<IEnumerable<byte[]>>> SplitAsync(byte[] pdfFile)
208215

209216
return await SplitAsync(inputFile.TempFileName);
210217
}
211-
218+
212219
/// <inheritdoc/>
213220
public async Task<IPDFtkResult<IEnumerable<byte[]>>> SplitAsync(Stream pdfFile)
214221
{
@@ -248,7 +255,7 @@ public async Task<IPDFtkResult<byte[]>> StampAsync(byte[] pdfFile, byte[] stampP
248255

249256
return await StampAsync(inputFile.TempFileName, stampFile.TempFileName);
250257
}
251-
258+
252259
/// <inheritdoc/>
253260
public async Task<IPDFtkResult<byte[]>> StampAsync(Stream pdfFile, Stream stampPdfFile)
254261
{
@@ -284,7 +291,7 @@ public async Task<IPDFtkResult<byte[]>> FillFormAsync(byte[] pdfFile,
284291
flatten,
285292
dropXfa);
286293
}
287-
294+
288295
/// <inheritdoc/>
289296
public async Task<IPDFtkResult<byte[]>> FillFormAsync(Stream pdfFile,
290297
IReadOnlyDictionary<string, string> fieldData,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace Kevsoft.PDFtk
4+
{
5+
/// <summary>
6+
/// Exception raised when the pdftk executable could not be found.
7+
/// </summary>
8+
[Serializable]
9+
public class PDFtkFileNotFoundExceptionException : Exception
10+
{
11+
internal PDFtkFileNotFoundExceptionException(string[] searchedPaths)
12+
: base(CreateMessage(searchedPaths))
13+
{
14+
}
15+
16+
private static string CreateMessage(string[] searchedPaths)
17+
{
18+
var message = "Could not find the pdftk executable, we tried the following paths:";
19+
message += Environment.NewLine;
20+
message += string.Join(Environment.NewLine, searchedPaths);
21+
22+
return message;
23+
}
24+
}
25+
}

src/Kevsoft.PDFtk/PDFtkOptions.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace Kevsoft.PDFtk
4+
{
5+
/// <summary>
6+
/// PDFtk Options
7+
/// </summary>
8+
public sealed class PDFtkOptions
9+
{
10+
/// <summary>
11+
/// Initializes a new instance of the PDFtkOptions class.
12+
/// </summary>
13+
/// <param name="pdftkPath">The full path to the pdftk executable.</param>
14+
public PDFtkOptions(string pdftkPath)
15+
{
16+
PDFtkPath = pdftkPath;
17+
}
18+
19+
/// <summary>
20+
/// The full path to the pdftk executable.
21+
/// </summary>
22+
public string PDFtkPath { get; }
23+
24+
/// <summary>
25+
/// Creates the defaults for the PDFtk options.
26+
/// </summary>
27+
/// <returns></returns>
28+
public static PDFtkOptions Default()
29+
{
30+
var fileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
31+
? "pdftk.exe" : "pdftk";
32+
33+
var pdftkPath = PathEnvironmentVariable.GetFileWithPath(fileName);
34+
35+
return new PDFtkOptions(pdftkPath);
36+
}
37+
}
38+
}

src/Kevsoft.PDFtk/PDFtkProcess.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ namespace Kevsoft.PDFtk
77
{
88
internal sealed class PDFtkProcess
99
{
10+
private readonly PDFtkOptions _options;
11+
12+
public PDFtkProcess(PDFtkOptions options)
13+
=> _options = options;
14+
1015
internal async Task<ExecutionResult> ExecuteAsync(params string[] args)
1116
{
1217
var process = new Process
1318
{
1419
StartInfo = new ProcessStartInfo
1520
{
16-
FileName = "pdftk",
21+
FileName = _options.PDFtkPath,
1722
Arguments = string.Join(" ", args),
1823
UseShellExecute = false,
1924
RedirectStandardOutput = true,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Kevsoft.PDFtk
5+
{
6+
internal static class PathEnvironmentVariable
7+
{
8+
public static string GetFileWithPath(string fileName)
9+
{
10+
var pathEnvironmentVariable = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
11+
var paths = pathEnvironmentVariable.Split(Path.PathSeparator);
12+
foreach (var path in paths)
13+
{
14+
var fullPath = Path.Combine(path, fileName);
15+
if (File.Exists(fullPath))
16+
return fullPath;
17+
}
18+
19+
throw new PDFtkFileNotFoundExceptionException(paths);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)