diff --git a/Example/Example15.ProviderPaths/Example15-ProviderPaths.ps1 b/Example/Example15.ProviderPaths/Example15-ProviderPaths.ps1 new file mode 100644 index 0000000..7de8afb --- /dev/null +++ b/Example/Example15.ProviderPaths/Example15-ProviderPaths.ps1 @@ -0,0 +1,7 @@ +# Demonstrates using PowerShell provider paths with PSWritePDF cmdlets + +$null = New-Item -Path (Join-Path $PSScriptRoot 'Output') -ItemType Directory -Force +$null = New-PSDrive -Name Data -PSProvider FileSystem -Root (Join-Path $PSScriptRoot 'Output') + +$path = 'Data:\provider-example.pdf' +New-PDF { New-PDFText -Text 'Provider path example' } -FilePath $path diff --git a/Sources/PSWritePDF/Cmdlets/CmdletConvertHTMLToPDF.cs b/Sources/PSWritePDF/Cmdlets/CmdletConvertHTMLToPDF.cs index a12606f..959cef8 100644 --- a/Sources/PSWritePDF/Cmdlets/CmdletConvertHTMLToPDF.cs +++ b/Sources/PSWritePDF/Cmdlets/CmdletConvertHTMLToPDF.cs @@ -78,14 +78,16 @@ protected override async Task ProcessRecordAsync() { string html = Content; + string? filePath = null; if (ParameterSetName == ParameterSetNames.File) { - if (!File.Exists(FilePath)) + filePath = GetUnresolvedProviderPathFromPSPath(FilePath); + if (!File.Exists(filePath)) { - WriteWarning($"File '{FilePath}' doesn't exist."); + WriteWarning($"File '{filePath}' doesn't exist."); return; } - html = File.ReadAllText(FilePath); + html = File.ReadAllText(filePath); } else if (ParameterSetName == ParameterSetNames.Uri) { @@ -106,27 +108,30 @@ protected override async Task ProcessRecordAsync() return; } - if (File.Exists(OutputFilePath) && !Force.IsPresent) + var outputFilePath = GetUnresolvedProviderPathFromPSPath(OutputFilePath); + + if (File.Exists(outputFilePath) && !Force.IsPresent) { - WriteWarning($"File '{OutputFilePath}' already exists. Use -Force to overwrite."); + WriteWarning($"File '{outputFilePath}' already exists. Use -Force to overwrite."); return; } - if (!ShouldProcess(OutputFilePath, "Convert HTML to PDF")) + if (!ShouldProcess(outputFilePath, "Convert HTML to PDF")) { return; } try { - if (CssFilePath != null && CssFilePath.Length > 0) + var cssFiles = CssFilePath?.Select(p => GetUnresolvedProviderPathFromPSPath(p)).ToArray(); + if (cssFiles != null && cssFiles.Length > 0) { var cssContent = new StringBuilder(); - foreach (var css in CssFilePath.Where(File.Exists)) + foreach (var css in cssFiles.Where(File.Exists)) { cssContent.AppendLine(File.ReadAllText(css)); } - foreach (var missing in CssFilePath.Where(p => !File.Exists(p))) + foreach (var missing in cssFiles.Where(p => !File.Exists(p))) { WriteWarning($"CSS file '{missing}' doesn't exist."); } @@ -140,26 +145,27 @@ protected override async Task ProcessRecordAsync() } } - using var fs = new FileStream(OutputFilePath, FileMode.Create, FileAccess.Write); + using var fs = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write); var properties = new iText.Html2pdf.ConverterProperties(); - if (!string.IsNullOrEmpty(BaseUri)) + var baseUri = !string.IsNullOrEmpty(BaseUri) ? GetUnresolvedProviderPathFromPSPath(BaseUri) : null; + if (!string.IsNullOrEmpty(baseUri)) { - properties.SetBaseUri(BaseUri); + properties.SetBaseUri(baseUri); } iText.Html2pdf.HtmlConverter.ConvertToPdf(html, fs, properties); if (Open.IsPresent) { var psi = new System.Diagnostics.ProcessStartInfo { - FileName = OutputFilePath, + FileName = outputFilePath, UseShellExecute = true, }; System.Diagnostics.Process.Start(psi); } - if (ShouldProcess(OutputFilePath, "Write output")) + if (ShouldProcess(outputFilePath, "Write output")) { - WriteObject(OutputFilePath); + WriteObject(outputFilePath); } } catch (Exception ex) diff --git a/Sources/PSWritePDF/Cmdlets/CmdletConvertPDFToText.cs b/Sources/PSWritePDF/Cmdlets/CmdletConvertPDFToText.cs index a87b0c2..e19c9d7 100644 --- a/Sources/PSWritePDF/Cmdlets/CmdletConvertPDFToText.cs +++ b/Sources/PSWritePDF/Cmdlets/CmdletConvertPDFToText.cs @@ -60,13 +60,16 @@ public class CmdletConvertPDFToText : PSCmdlet protected override void ProcessRecord() { - if (!File.Exists(FilePath)) + var filePath = GetUnresolvedProviderPathFromPSPath(FilePath); + var outFile = !string.IsNullOrWhiteSpace(OutFile) ? GetUnresolvedProviderPathFromPSPath(OutFile) : null; + + if (!File.Exists(filePath)) { - WriteWarning($"Path '{FilePath}' doesn't exist. Terminating."); + WriteWarning($"Path '{filePath}' doesn't exist. Terminating."); return; } - using var reader = new PdfReader(FilePath); + using var reader = new PdfReader(filePath); if (IgnoreProtection) { reader.SetUnethicalReading(true); @@ -82,7 +85,7 @@ protected override void ProcessRecord() { if (pageNum < 1 || pageNum > pagesCount) { - WriteWarning($"File '{FilePath}' doesn't contain page number {pageNum}. Skipping."); + WriteWarning($"File '{filePath}' doesn't contain page number {pageNum}. Skipping."); continue; } @@ -101,20 +104,20 @@ protected override void ProcessRecord() } catch (Exception ex) { - WriteWarning($"Processing document '{FilePath}' failed with error: {ex.Message}"); + WriteWarning($"Processing document '{filePath}' failed with error: {ex.Message}"); } } - if (!string.IsNullOrWhiteSpace(OutFile)) + if (!string.IsNullOrWhiteSpace(outFile)) { try { var combined = string.Join(Environment.NewLine, collectedTexts); - File.WriteAllText(OutFile, combined, Encoding.UTF8); + File.WriteAllText(outFile, combined, Encoding.UTF8); } catch (Exception ex) { - WriteWarning($"Saving file '{OutFile}' failed with error: {ex.Message}"); + WriteWarning($"Saving file '{outFile}' failed with error: {ex.Message}"); } } } diff --git a/Sources/PSWritePDF/Cmdlets/CmdletMergePDF.cs b/Sources/PSWritePDF/Cmdlets/CmdletMergePDF.cs index 9a88915..d020a97 100644 --- a/Sources/PSWritePDF/Cmdlets/CmdletMergePDF.cs +++ b/Sources/PSWritePDF/Cmdlets/CmdletMergePDF.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using System.Management.Automation; using iText.Kernel.Pdf; using iText.Kernel.Utils; @@ -53,13 +54,16 @@ protected override void ProcessRecord() return; } + var inputFiles = InputFile.Select(f => GetUnresolvedProviderPathFromPSPath(f)).ToArray(); + var outputFile = GetUnresolvedProviderPathFromPSPath(OutputFile); + try { - using var writer = new PdfWriter(OutputFile); + using var writer = new PdfWriter(outputFile); using var pdf = new PdfDocument(writer); var merger = new PdfMerger(pdf); - foreach (var file in InputFile) + foreach (var file in inputFiles) { if (!File.Exists(file)) { @@ -86,7 +90,7 @@ protected override void ProcessRecord() } catch (Exception ex) { - WriteWarning($"Saving document '{OutputFile}' failed with error: {ex.Message}"); + WriteWarning($"Saving document '{outputFile}' failed with error: {ex.Message}"); } } } diff --git a/Sources/PSWritePDF/Cmdlets/CmdletNewPDF.cs b/Sources/PSWritePDF/Cmdlets/CmdletNewPDF.cs index 6724178..31f729a 100644 --- a/Sources/PSWritePDF/Cmdlets/CmdletNewPDF.cs +++ b/Sources/PSWritePDF/Cmdlets/CmdletNewPDF.cs @@ -62,14 +62,16 @@ public class CmdletNewPDF : PSCmdlet { [Parameter, Alias("Open")] public SwitchParameter Show { get; set; } protected override void ProcessRecord() { + var filePath = GetUnresolvedProviderPathFromPSPath(FilePath); + iTextPdf.PdfWriter writer; if (!string.IsNullOrEmpty(Version)) { var enumName = "PDF_" + Version.Replace('.', '_'); var pdfVersion = (iTextPdf.PdfVersion)Enum.Parse(typeof(iTextPdf.PdfVersion), enumName, true); var props = new iTextPdf.WriterProperties().SetPdfVersion(pdfVersion); - writer = new iTextPdf.PdfWriter(FilePath, props); + writer = new iTextPdf.PdfWriter(filePath, props); } else { - writer = new iTextPdf.PdfWriter(FilePath); + writer = new iTextPdf.PdfWriter(filePath); } var pdfDocument = new iTextPdf.PdfDocument(writer); @@ -82,8 +84,8 @@ protected override void ProcessRecord() { if (PDFContent != null) { PDFContent.Invoke(); document.Close(); - if (Show.IsPresent && File.Exists(FilePath)) { - System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(FilePath) { UseShellExecute = true }); + if (Show.IsPresent && File.Exists(filePath)) { + System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath) { UseShellExecute = true }); } } else { WriteObject(pdfDocument); diff --git a/Sources/PSWritePDF/Cmdlets/CmdletSplitPDF.cs b/Sources/PSWritePDF/Cmdlets/CmdletSplitPDF.cs index c35c9dd..4cd8aea 100644 --- a/Sources/PSWritePDF/Cmdlets/CmdletSplitPDF.cs +++ b/Sources/PSWritePDF/Cmdlets/CmdletSplitPDF.cs @@ -81,33 +81,36 @@ public class CmdletSplitPDF : PSCmdlet protected override void ProcessRecord() { - if (!File.Exists(FilePath)) + var filePath = GetUnresolvedProviderPathFromPSPath(FilePath); + var outputFolder = GetUnresolvedProviderPathFromPSPath(OutputFolder); + + if (!File.Exists(filePath)) { - WriteWarning($"Path '{FilePath}' doesn't exist. Terminating."); + WriteWarning($"Path '{filePath}' doesn't exist. Terminating."); return; } - if (!Directory.Exists(OutputFolder)) + if (!Directory.Exists(outputFolder)) { - WriteWarning($"Destination folder '{OutputFolder}' doesn't exist. Terminating."); + WriteWarning($"Destination folder '{outputFolder}' doesn't exist. Terminating."); return; } try { - if (!ShouldProcess(FilePath, $"Split into '{OutputFolder}'")) + if (!ShouldProcess(filePath, $"Split into '{outputFolder}'")) { return; } - using var reader = new PdfReader(FilePath); + using var reader = new PdfReader(filePath); if (IgnoreProtection) { reader.SetUnethicalReading(true); } using var document = new PdfDocument(reader); - var splitter = new PdfSequentialSplitter(document, OutputFolder, OutputName, Force.IsPresent); + var splitter = new PdfSequentialSplitter(document, outputFolder, OutputName, Force.IsPresent); IList documents; if (ParameterSetName == SplitCountParameterSet) diff --git a/Tests/Convert-HTMLToPDF.Tests.ps1 b/Tests/Convert-HTMLToPDF.Tests.ps1 index 7a888e5..5ec4b04 100644 --- a/Tests/Convert-HTMLToPDF.Tests.ps1 +++ b/Tests/Convert-HTMLToPDF.Tests.ps1 @@ -47,6 +47,12 @@ Describe 'Convert-HTMLToPDF' { Remove-Item $assetDir -Recurse -Force } + It 'supports provider paths for output' { + $file = Join-Path TestDrive: 'html.pdf' + Convert-HTMLToPDF -Content 'Test' -OutputFilePath $file | Out-Null + Test-Path $file | Should -BeTrue + } + AfterAll { Remove-Item -LiteralPath $script:outputDir -Recurse -Force } diff --git a/Tests/Convert-PDFToText.Tests.ps1 b/Tests/Convert-PDFToText.Tests.ps1 index dc8b293..097749d 100644 --- a/Tests/Convert-PDFToText.Tests.ps1 +++ b/Tests/Convert-PDFToText.Tests.ps1 @@ -19,4 +19,12 @@ Describe 'Convert-PDFToText' { ($result | Select-Object -ExpandProperty Text | Out-String) | Should -Match 'Text 1' (Get-Content $outFile -Raw) | Should -Match 'Text 1' } + + It 'supports provider paths' { + $src = Join-Path $PSScriptRoot 'Input' 'SampleAcroForm.pdf' + $file = Join-Path TestDrive: 'sample.pdf' + Copy-Item $src $file + $text = Convert-PDFToText -FilePath $file + $text[0].Text | Should -Match 'Text 1' + } } diff --git a/Tests/Merge-PDF.Tests.ps1 b/Tests/Merge-PDF.Tests.ps1 index cdd7615..35284f1 100644 --- a/Tests/Merge-PDF.Tests.ps1 +++ b/Tests/Merge-PDF.Tests.ps1 @@ -11,6 +11,18 @@ Describe 'Merge-PDF' { Test-Path $output | Should -BeTrue } + It 'merges using provider paths' { + $file1 = Join-Path $PSScriptRoot 'Input' 'SampleAcroForm.pdf' + $file2 = Join-Path $PSScriptRoot 'Input' 'SampleToSplit.pdf' + $td1 = Join-Path TestDrive: 'f1.pdf' + $td2 = Join-Path TestDrive: 'f2.pdf' + Copy-Item $file1 $td1 + Copy-Item $file2 $td2 + $output = Join-Path TestDrive: 'merged.pdf' + Merge-PDF -InputFile $td1, $td2 -OutputFile $output + Test-Path $output | Should -BeTrue + } + AfterAll { Remove-Item -LiteralPath (Join-Path $PSScriptRoot 'Output') -Recurse -Force } diff --git a/Tests/New-PDF.Tests.ps1 b/Tests/New-PDF.Tests.ps1 index 8820129..05b5f3d 100644 --- a/Tests/New-PDF.Tests.ps1 +++ b/Tests/New-PDF.Tests.ps1 @@ -85,6 +85,12 @@ Describe 'New-PDF' { $Details.PagesNumber | Should -Be 2 } + It 'creates file using provider path' { + $file = Join-Path TestDrive: 'provider.pdf' + New-PDF { New-PDFText -Text 'test' } -FilePath $file + Test-Path $file | Should -BeTrue + } + AfterAll { Remove-Item -LiteralPath (Join-Path $PSScriptRoot 'Output') -Recurse -Force -ErrorAction SilentlyContinue } diff --git a/Tests/Split-PDF.Tests.ps1 b/Tests/Split-PDF.Tests.ps1 index 673453f..c0b3e2b 100644 --- a/Tests/Split-PDF.Tests.ps1 +++ b/Tests/Split-PDF.Tests.ps1 @@ -65,6 +65,17 @@ Describe 'Split-PDF' { $forced.Count | Should -BeGreaterThan 0 } + It 'supports provider paths' { + $src = Join-Path $PSScriptRoot 'Input' 'SampleToSplit.pdf' + $file = Join-Path TestDrive: 'input.pdf' + Copy-Item $src $file + $outDir = Join-Path TestDrive: 'out' + New-Item -Path $outDir -ItemType Directory | Out-Null + $files = Split-PDF -FilePath $file -OutputFolder $outDir -SplitCount 1 + $files.Count | Should -BeGreaterThan 1 + $files | ForEach-Object { Test-Path $_ | Should -BeTrue } + } + AfterAll { Remove-Item -LiteralPath (Join-Path $PSScriptRoot 'Input' 'Bookmarked.pdf') -Force Remove-Item -LiteralPath (Join-Path $PSScriptRoot 'Output') -Recurse -Force