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
12 changes: 8 additions & 4 deletions Sources/PSWritePDF/Cmdlets/CmdletConvertHTMLToPDF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
using System.IO;
using System.Management.Automation;
using System.Net.Http;
using System.Threading.Tasks;

namespace PSWritePDF.Cmdlets;

[Cmdlet(VerbsData.Convert, "HTMLToPDF", DefaultParameterSetName = ParameterSetNames.Uri, SupportsShouldProcess = true)]
public class CmdletConvertHTMLToPDF : PSCmdlet
public class CmdletConvertHTMLToPDF : AsyncPSCmdlet
{
private static class ParameterSetNames
{
Expand All @@ -33,7 +34,7 @@ private static class ParameterSetNames
[Parameter]
public SwitchParameter Force { get; set; }

protected override void ProcessRecord()
protected override async Task ProcessRecordAsync()
{
string html = Content;

Expand All @@ -51,7 +52,7 @@ protected override void ProcessRecord()
try
{
using var client = new HttpClient();
html = client.GetStringAsync(Uri).GetAwaiter().GetResult();
html = await client.GetStringAsync(Uri).ConfigureAwait(false);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -90,7 +91,10 @@ protected override void ProcessRecord()
System.Diagnostics.Process.Start(psi);
}

WriteObject(OutputFilePath);
if (ShouldProcess(OutputFilePath, "Write output"))
{
WriteObject(OutputFilePath);
}
}
catch (Exception ex)
{
Expand Down
13 changes: 12 additions & 1 deletion Tests/Convert-HTMLToPDF.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Describe 'Convert-HTMLToPDF' {
BeforeAll {
New-Item -Path $PSScriptRoot -Force -ItemType Directory -Name 'Output' | Out-Null
$outputDir = Join-Path $PSScriptRoot 'Output'
if (Test-Path $outputDir) {
Remove-Item -LiteralPath $outputDir -Recurse -Force
}
New-Item -Path $outputDir -ItemType Directory | Out-Null
}

It 'converts HTML string to PDF and outputs file path' {
Expand All @@ -21,6 +25,13 @@ Describe 'Convert-HTMLToPDF' {
(Get-Item $file).LastWriteTime | Should -Not -Be $timestamp
}

It 'converts HTML from a URI' {
$file = Join-Path $PSScriptRoot 'Output' 'uri.pdf'
$result = Convert-HTMLToPDF -Uri 'https://example.com/' -OutputFilePath $file -Confirm:$false
Test-Path $file | Should -BeTrue
$result | Should -Be $file
}

AfterAll {
Remove-Item -LiteralPath (Join-Path $PSScriptRoot 'Output') -Recurse -Force
}
Expand Down
Loading