Skip to content

Commit 01ea4a8

Browse files
Merge pull request #76 from EvotecIT/update-cmdlets-to-resolve-paths
2 parents 67b3a38 + d42df05 commit 01ea4a8

11 files changed

Lines changed: 105 additions & 37 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Demonstrates using PowerShell provider paths with PSWritePDF cmdlets
2+
3+
$null = New-Item -Path (Join-Path $PSScriptRoot 'Output') -ItemType Directory -Force
4+
$null = New-PSDrive -Name Data -PSProvider FileSystem -Root (Join-Path $PSScriptRoot 'Output')
5+
6+
$path = 'Data:\provider-example.pdf'
7+
New-PDF { New-PDFText -Text 'Provider path example' } -FilePath $path

Sources/PSWritePDF/Cmdlets/CmdletConvertHTMLToPDF.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,16 @@ protected override async Task ProcessRecordAsync()
7878
{
7979
string html = Content;
8080

81+
string? filePath = null;
8182
if (ParameterSetName == ParameterSetNames.File)
8283
{
83-
if (!File.Exists(FilePath))
84+
filePath = GetUnresolvedProviderPathFromPSPath(FilePath);
85+
if (!File.Exists(filePath))
8486
{
85-
WriteWarning($"File '{FilePath}' doesn't exist.");
87+
WriteWarning($"File '{filePath}' doesn't exist.");
8688
return;
8789
}
88-
html = File.ReadAllText(FilePath);
90+
html = File.ReadAllText(filePath);
8991
}
9092
else if (ParameterSetName == ParameterSetNames.Uri)
9193
{
@@ -106,27 +108,30 @@ protected override async Task ProcessRecordAsync()
106108
return;
107109
}
108110

109-
if (File.Exists(OutputFilePath) && !Force.IsPresent)
111+
var outputFilePath = GetUnresolvedProviderPathFromPSPath(OutputFilePath);
112+
113+
if (File.Exists(outputFilePath) && !Force.IsPresent)
110114
{
111-
WriteWarning($"File '{OutputFilePath}' already exists. Use -Force to overwrite.");
115+
WriteWarning($"File '{outputFilePath}' already exists. Use -Force to overwrite.");
112116
return;
113117
}
114118

115-
if (!ShouldProcess(OutputFilePath, "Convert HTML to PDF"))
119+
if (!ShouldProcess(outputFilePath, "Convert HTML to PDF"))
116120
{
117121
return;
118122
}
119123

120124
try
121125
{
122-
if (CssFilePath != null && CssFilePath.Length > 0)
126+
var cssFiles = CssFilePath?.Select(p => GetUnresolvedProviderPathFromPSPath(p)).ToArray();
127+
if (cssFiles != null && cssFiles.Length > 0)
123128
{
124129
var cssContent = new StringBuilder();
125-
foreach (var css in CssFilePath.Where(File.Exists))
130+
foreach (var css in cssFiles.Where(File.Exists))
126131
{
127132
cssContent.AppendLine(File.ReadAllText(css));
128133
}
129-
foreach (var missing in CssFilePath.Where(p => !File.Exists(p)))
134+
foreach (var missing in cssFiles.Where(p => !File.Exists(p)))
130135
{
131136
WriteWarning($"CSS file '{missing}' doesn't exist.");
132137
}
@@ -140,26 +145,27 @@ protected override async Task ProcessRecordAsync()
140145
}
141146
}
142147

143-
using var fs = new FileStream(OutputFilePath, FileMode.Create, FileAccess.Write);
148+
using var fs = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write);
144149
var properties = new iText.Html2pdf.ConverterProperties();
145-
if (!string.IsNullOrEmpty(BaseUri))
150+
var baseUri = !string.IsNullOrEmpty(BaseUri) ? GetUnresolvedProviderPathFromPSPath(BaseUri) : null;
151+
if (!string.IsNullOrEmpty(baseUri))
146152
{
147-
properties.SetBaseUri(BaseUri);
153+
properties.SetBaseUri(baseUri);
148154
}
149155
iText.Html2pdf.HtmlConverter.ConvertToPdf(html, fs, properties);
150156
if (Open.IsPresent)
151157
{
152158
var psi = new System.Diagnostics.ProcessStartInfo
153159
{
154-
FileName = OutputFilePath,
160+
FileName = outputFilePath,
155161
UseShellExecute = true,
156162
};
157163
System.Diagnostics.Process.Start(psi);
158164
}
159165

160-
if (ShouldProcess(OutputFilePath, "Write output"))
166+
if (ShouldProcess(outputFilePath, "Write output"))
161167
{
162-
WriteObject(OutputFilePath);
168+
WriteObject(outputFilePath);
163169
}
164170
}
165171
catch (Exception ex)

Sources/PSWritePDF/Cmdlets/CmdletConvertPDFToText.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,16 @@ public class CmdletConvertPDFToText : PSCmdlet
6060

6161
protected override void ProcessRecord()
6262
{
63-
if (!File.Exists(FilePath))
63+
var filePath = GetUnresolvedProviderPathFromPSPath(FilePath);
64+
var outFile = !string.IsNullOrWhiteSpace(OutFile) ? GetUnresolvedProviderPathFromPSPath(OutFile) : null;
65+
66+
if (!File.Exists(filePath))
6467
{
65-
WriteWarning($"Path '{FilePath}' doesn't exist. Terminating.");
68+
WriteWarning($"Path '{filePath}' doesn't exist. Terminating.");
6669
return;
6770
}
6871

69-
using var reader = new PdfReader(FilePath);
72+
using var reader = new PdfReader(filePath);
7073
if (IgnoreProtection)
7174
{
7275
reader.SetUnethicalReading(true);
@@ -82,7 +85,7 @@ protected override void ProcessRecord()
8285
{
8386
if (pageNum < 1 || pageNum > pagesCount)
8487
{
85-
WriteWarning($"File '{FilePath}' doesn't contain page number {pageNum}. Skipping.");
88+
WriteWarning($"File '{filePath}' doesn't contain page number {pageNum}. Skipping.");
8689
continue;
8790
}
8891

@@ -101,20 +104,20 @@ protected override void ProcessRecord()
101104
}
102105
catch (Exception ex)
103106
{
104-
WriteWarning($"Processing document '{FilePath}' failed with error: {ex.Message}");
107+
WriteWarning($"Processing document '{filePath}' failed with error: {ex.Message}");
105108
}
106109
}
107110

108-
if (!string.IsNullOrWhiteSpace(OutFile))
111+
if (!string.IsNullOrWhiteSpace(outFile))
109112
{
110113
try
111114
{
112115
var combined = string.Join(Environment.NewLine, collectedTexts);
113-
File.WriteAllText(OutFile, combined, Encoding.UTF8);
116+
File.WriteAllText(outFile, combined, Encoding.UTF8);
114117
}
115118
catch (Exception ex)
116119
{
117-
WriteWarning($"Saving file '{OutFile}' failed with error: {ex.Message}");
120+
WriteWarning($"Saving file '{outFile}' failed with error: {ex.Message}");
118121
}
119122
}
120123
}

Sources/PSWritePDF/Cmdlets/CmdletMergePDF.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Management.Automation;
45
using iText.Kernel.Pdf;
56
using iText.Kernel.Utils;
@@ -53,13 +54,16 @@ protected override void ProcessRecord()
5354
return;
5455
}
5556

57+
var inputFiles = InputFile.Select(f => GetUnresolvedProviderPathFromPSPath(f)).ToArray();
58+
var outputFile = GetUnresolvedProviderPathFromPSPath(OutputFile);
59+
5660
try
5761
{
58-
using var writer = new PdfWriter(OutputFile);
62+
using var writer = new PdfWriter(outputFile);
5963
using var pdf = new PdfDocument(writer);
6064
var merger = new PdfMerger(pdf);
6165

62-
foreach (var file in InputFile)
66+
foreach (var file in inputFiles)
6367
{
6468
if (!File.Exists(file))
6569
{
@@ -86,7 +90,7 @@ protected override void ProcessRecord()
8690
}
8791
catch (Exception ex)
8892
{
89-
WriteWarning($"Saving document '{OutputFile}' failed with error: {ex.Message}");
93+
WriteWarning($"Saving document '{outputFile}' failed with error: {ex.Message}");
9094
}
9195
}
9296
}

Sources/PSWritePDF/Cmdlets/CmdletNewPDF.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ public class CmdletNewPDF : PSCmdlet {
6262
[Parameter, Alias("Open")] public SwitchParameter Show { get; set; }
6363

6464
protected override void ProcessRecord() {
65+
var filePath = GetUnresolvedProviderPathFromPSPath(FilePath);
66+
6567
iTextPdf.PdfWriter writer;
6668
if (!string.IsNullOrEmpty(Version)) {
6769
var enumName = "PDF_" + Version.Replace('.', '_');
6870
var pdfVersion = (iTextPdf.PdfVersion)Enum.Parse(typeof(iTextPdf.PdfVersion), enumName, true);
6971
var props = new iTextPdf.WriterProperties().SetPdfVersion(pdfVersion);
70-
writer = new iTextPdf.PdfWriter(FilePath, props);
72+
writer = new iTextPdf.PdfWriter(filePath, props);
7173
} else {
72-
writer = new iTextPdf.PdfWriter(FilePath);
74+
writer = new iTextPdf.PdfWriter(filePath);
7375
}
7476

7577
var pdfDocument = new iTextPdf.PdfDocument(writer);
@@ -82,8 +84,8 @@ protected override void ProcessRecord() {
8284
if (PDFContent != null) {
8385
PDFContent.Invoke();
8486
document.Close();
85-
if (Show.IsPresent && File.Exists(FilePath)) {
86-
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(FilePath) { UseShellExecute = true });
87+
if (Show.IsPresent && File.Exists(filePath)) {
88+
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath) { UseShellExecute = true });
8789
}
8890
} else {
8991
WriteObject(pdfDocument);

Sources/PSWritePDF/Cmdlets/CmdletSplitPDF.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,36 @@ public class CmdletSplitPDF : PSCmdlet
8181

8282
protected override void ProcessRecord()
8383
{
84-
if (!File.Exists(FilePath))
84+
var filePath = GetUnresolvedProviderPathFromPSPath(FilePath);
85+
var outputFolder = GetUnresolvedProviderPathFromPSPath(OutputFolder);
86+
87+
if (!File.Exists(filePath))
8588
{
86-
WriteWarning($"Path '{FilePath}' doesn't exist. Terminating.");
89+
WriteWarning($"Path '{filePath}' doesn't exist. Terminating.");
8790
return;
8891
}
8992

90-
if (!Directory.Exists(OutputFolder))
93+
if (!Directory.Exists(outputFolder))
9194
{
92-
WriteWarning($"Destination folder '{OutputFolder}' doesn't exist. Terminating.");
95+
WriteWarning($"Destination folder '{outputFolder}' doesn't exist. Terminating.");
9396
return;
9497
}
9598

9699
try
97100
{
98-
if (!ShouldProcess(FilePath, $"Split into '{OutputFolder}'"))
101+
if (!ShouldProcess(filePath, $"Split into '{outputFolder}'"))
99102
{
100103
return;
101104
}
102105

103-
using var reader = new PdfReader(FilePath);
106+
using var reader = new PdfReader(filePath);
104107
if (IgnoreProtection)
105108
{
106109
reader.SetUnethicalReading(true);
107110
}
108111

109112
using var document = new PdfDocument(reader);
110-
var splitter = new PdfSequentialSplitter(document, OutputFolder, OutputName, Force.IsPresent);
113+
var splitter = new PdfSequentialSplitter(document, outputFolder, OutputName, Force.IsPresent);
111114
IList<PdfDocument> documents;
112115

113116
if (ParameterSetName == SplitCountParameterSet)

Tests/Convert-HTMLToPDF.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ Describe 'Convert-HTMLToPDF' {
4747
Remove-Item $assetDir -Recurse -Force
4848
}
4949

50+
It 'supports provider paths for output' {
51+
$file = Join-Path TestDrive: 'html.pdf'
52+
Convert-HTMLToPDF -Content '<html><body>Test</body></html>' -OutputFilePath $file | Out-Null
53+
Test-Path $file | Should -BeTrue
54+
}
55+
5056
AfterAll {
5157
Remove-Item -LiteralPath $script:outputDir -Recurse -Force
5258
}

Tests/Convert-PDFToText.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@ Describe 'Convert-PDFToText' {
1919
($result | Select-Object -ExpandProperty Text | Out-String) | Should -Match 'Text 1'
2020
(Get-Content $outFile -Raw) | Should -Match 'Text 1'
2121
}
22+
23+
It 'supports provider paths' {
24+
$src = Join-Path $PSScriptRoot 'Input' 'SampleAcroForm.pdf'
25+
$file = Join-Path TestDrive: 'sample.pdf'
26+
Copy-Item $src $file
27+
$text = Convert-PDFToText -FilePath $file
28+
$text[0].Text | Should -Match 'Text 1'
29+
}
2230
}

Tests/Merge-PDF.Tests.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ Describe 'Merge-PDF' {
1111
Test-Path $output | Should -BeTrue
1212
}
1313

14+
It 'merges using provider paths' {
15+
$file1 = Join-Path $PSScriptRoot 'Input' 'SampleAcroForm.pdf'
16+
$file2 = Join-Path $PSScriptRoot 'Input' 'SampleToSplit.pdf'
17+
$td1 = Join-Path TestDrive: 'f1.pdf'
18+
$td2 = Join-Path TestDrive: 'f2.pdf'
19+
Copy-Item $file1 $td1
20+
Copy-Item $file2 $td2
21+
$output = Join-Path TestDrive: 'merged.pdf'
22+
Merge-PDF -InputFile $td1, $td2 -OutputFile $output
23+
Test-Path $output | Should -BeTrue
24+
}
25+
1426
AfterAll {
1527
Remove-Item -LiteralPath (Join-Path $PSScriptRoot 'Output') -Recurse -Force
1628
}

Tests/New-PDF.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ Describe 'New-PDF' {
8585
$Details.PagesNumber | Should -Be 2
8686
}
8787

88+
It 'creates file using provider path' {
89+
$file = Join-Path TestDrive: 'provider.pdf'
90+
New-PDF { New-PDFText -Text 'test' } -FilePath $file
91+
Test-Path $file | Should -BeTrue
92+
}
93+
8894
AfterAll {
8995
Remove-Item -LiteralPath (Join-Path $PSScriptRoot 'Output') -Recurse -Force -ErrorAction SilentlyContinue
9096
}

0 commit comments

Comments
 (0)