Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Example/Example15.ProviderPaths/Example15-ProviderPaths.ps1
Original file line number Diff line number Diff line change
@@ -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
36 changes: 21 additions & 15 deletions Sources/PSWritePDF/Cmdlets/CmdletConvertHTMLToPDF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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.");
}
Expand All @@ -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)
Expand Down
19 changes: 11 additions & 8 deletions Sources/PSWritePDF/Cmdlets/CmdletConvertPDFToText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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}");
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions Sources/PSWritePDF/Cmdlets/CmdletMergePDF.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Management.Automation;
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
Expand Down Expand Up @@ -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))
{
Expand All @@ -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}");
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions Sources/PSWritePDF/Cmdlets/CmdletNewPDF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
17 changes: 10 additions & 7 deletions Sources/PSWritePDF/Cmdlets/CmdletSplitPDF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PdfDocument> documents;

if (ParameterSetName == SplitCountParameterSet)
Expand Down
6 changes: 6 additions & 0 deletions Tests/Convert-HTMLToPDF.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<html><body>Test</body></html>' -OutputFilePath $file | Out-Null
Test-Path $file | Should -BeTrue
}

AfterAll {
Remove-Item -LiteralPath $script:outputDir -Recurse -Force
}
Expand Down
8 changes: 8 additions & 0 deletions Tests/Convert-PDFToText.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
12 changes: 12 additions & 0 deletions Tests/Merge-PDF.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions Tests/New-PDF.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions Tests/Split-PDF.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading