Skip to content

Commit 82cff1b

Browse files
Fixed file parameters handling.
1 parent 2153a73 commit 82cff1b

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

ConvertApi.Cli.Tests/DirectCallTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void Setup()
2020
[Test]
2121
public async Task TestConvertPdfToDocx()
2222
{
23-
var outputFile = Path.Combine(TestOutputDir, "simple.docx");
23+
var outputFile = Path.Combine(TestOutputDir, "simple2.docx");
2424
var inputFile = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "simple.pdf");
2525

2626
await Program.Main([ApiToken, outputFile, inputFile]);
@@ -65,6 +65,18 @@ public async Task TestAddWatermarkToPdf()
6565
Assert.IsTrue(File.Exists(outputFile), "Output file was not created.");
6666
}
6767

68+
[Test]
69+
public async Task TestAddWatermarkOverlayToPdf()
70+
{
71+
var outputFile = Path.Combine(TestOutputDir, "watermarkOverlay.pdf");
72+
var inputFile = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "simple.pdf");
73+
var overlayFile = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "simple.pdf");
74+
75+
await Program.Main([ApiToken, TestOutputDir, inputFile, "pdf", "watermark-overlay", "FileName=watermarkOverlay", $"OverlayFile={overlayFile}"]);
76+
77+
Assert.IsTrue(File.Exists(outputFile), "Output file was not created.");
78+
}
79+
6880
[Test]
6981
public async Task TestProtectPdfWithPassword()
7082
{

ConvertApi.Cli/Program.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static async Task Main(string[] args)
1717
DisplayHelp();
1818
return;
1919
}
20-
20+
2121
// Replacing null values with empty string to avoid unexpected behaviour.
2222
args = args.Select(s => s ?? "").ToArray();
2323

@@ -31,15 +31,15 @@ public static async Task Main(string[] args)
3131
: args.Length - parametersCount;
3232

3333
string[] inputFiles = args.Skip(2).Take(inputFilesEndIndex - 2).ToArray();
34-
34+
3535
string fromFormat = args.Length > inputFilesEndIndex
3636
? args[^(2 + parametersCount)] // Second-to-last argument - parameters with '='
3737
: Path.GetExtension(inputFiles[0]).Trim('.').ToLower(); // Infer from first input file
3838

3939
string toFormat = args.Length > inputFilesEndIndex + 1
4040
? args[^(1 + parametersCount)] // Last argument - parameters with '='
4141
: Path.GetExtension(outputDirectoryOrFile).Trim('.').ToLower(); // Infer from output file
42-
42+
4343
ValidateInputFiles(inputFiles, fromFormat);
4444

4545
// Extract dynamic properties
@@ -59,13 +59,13 @@ private static void ValidateInputFiles(string[] inputFiles, string fromFormat)
5959
{
6060
if (fromFormat == "web")
6161
return;
62-
62+
6363
if (inputFiles.Length == 0)
6464
{
6565
Console.WriteLine("Error: At least one input file is required.");
6666
return;
6767
}
68-
68+
6969
foreach (var file in inputFiles)
7070
{
7171
if (!File.Exists(file))
@@ -120,29 +120,25 @@ static async Task ConvertFiles(string apiToken, string fromFormat, string toForm
120120
{
121121
if (inputFiles.Length == 1)
122122
{
123-
form.Add(new StreamContent(File.OpenRead(inputFiles[0]))
124-
{
125-
Headers = { ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = "file", FileName = Path.GetFileName(inputFiles[0]) } }
126-
});
123+
AddFilesToFormParameters(form, "file", inputFiles[0]);
127124
}
128125
else
129126
{
130127
int fileIndex = 0;
131128
foreach (var inputFile in inputFiles)
132129
{
133-
form.Add(new StreamContent(File.OpenRead(inputFile))
134-
{
135-
Headers =
136-
{
137-
ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = $"files[{fileIndex}]", FileName = Path.GetFileName(inputFile) }
138-
}
139-
});
130+
AddFilesToFormParameters(form, $"files[{fileIndex}]", inputFile);
140131
fileIndex++;
141132
}
142133
}
143134
}
144135

145-
foreach (var property in properties)
136+
foreach (var (filePropertyName, filePath) in properties.Where(x => x.Key.ToLower().EndsWith("file")))
137+
{
138+
AddFilesToFormParameters(form, filePropertyName, filePath);
139+
}
140+
141+
foreach (var property in properties.Where(x => !x.Key.ToLower().EndsWith("file")))
146142
{
147143
form.Add(new StringContent(property.Value), property.Key);
148144
}
@@ -186,13 +182,21 @@ static async Task ConvertFiles(string apiToken, string fromFormat, string toForm
186182
}
187183
}
188184

185+
private static void AddFilesToFormParameters(MultipartFormDataContent form, string parameterName, string filePath)
186+
{
187+
form.Add(new StreamContent(File.OpenRead(filePath))
188+
{
189+
Headers = { ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = parameterName, FileName = Path.GetFileName(filePath) } }
190+
});
191+
}
192+
189193
static string GetVersion()
190194
{
191195
var version = Assembly.GetExecutingAssembly()
192196
.GetName()
193197
.Version?
194198
.ToString() ?? "unknown";
195-
199+
196200
return version;
197201
}
198202
}

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,23 @@ convertapi-cli.exe YOUR_API_TOKEN output.docx input.pdf
116116

117117
Merge multiple PDF files into one:
118118
```shell
119-
convertapi-cli.exe YOUR_API_TOKEN merged_output.pdf file1.pdf file2.pdf file3.pdf pdf merge
119+
convertapi-cli.exe YOUR_API_TOKEN output_directory file1.pdf file2.pdf file3.pdf pdf merge
120120
```
121121

122122
Protect a PDF with a password:
123123
```shell
124-
convertapi-cli.exe YOUR_API_TOKEN protected_output.pdf input.pdf pdf protect UserPassword=1234 OwnerPassword=abcd FileName=protected
124+
convertapi-cli.exe YOUR_API_TOKEN output_directory input.pdf pdf protect UserPassword=1234 OwnerPassword=abcd FileName=protected
125125
```
126126

127127
Add a watermark to a PDF:
128128
```shell
129-
convertapi-cli.exe YOUR_API_TOKEN watermarked_output.pdf input.pdf pdf watermark Text=Confidential FileName=watermark
129+
convertapi-cli.exe YOUR_API_TOKEN output_directory input.pdf pdf watermark Text=Confidential FileName=watermark
130+
```
131+
132+
Add watermark-overlay to a PDF:
133+
> **Note:** Overlay file path is added as parameter.
134+
```shell
135+
convertapi-cli.exe YOUR_API_TOKEN output_directory input.pdf pdf watermark-overlay OverlayFile=overlay-file.pdf FileName=watermarkResult
130136
```
131137

132138

0 commit comments

Comments
 (0)