From 2506a6315d68e27d420645cae816fcd372841a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20K=C5=82ys?= Date: Thu, 7 Aug 2025 22:27:38 +0200 Subject: [PATCH] chore: remove duplicate image and fix tests --- .../Example10-WithAssets.css | 1 + .../Example10-WithAssets.html | 10 +++++ .../Example10-WithAssets.ps1 | 4 ++ .../Cmdlets/CmdletConvertHTMLToPDF.cs | 36 ++++++++++++++++- Tests/AdditionalCmdlets.Tests.ps1 | 4 +- Tests/Convert-HTMLToPDF.Tests.ps1 | 31 +++++++++++---- Tests/New-PDF.Tests.ps1 | 39 ++++++------------- Tests/New-PDFImage.Tests.ps1 | 12 +++--- Tests/New-PDFTable.Tests.ps1 | 13 +++++-- 9 files changed, 100 insertions(+), 50 deletions(-) create mode 100644 Example/Example10.ConvertFromHTML/Example10-WithAssets.css create mode 100644 Example/Example10.ConvertFromHTML/Example10-WithAssets.html create mode 100644 Example/Example10.ConvertFromHTML/Example10-WithAssets.ps1 diff --git a/Example/Example10.ConvertFromHTML/Example10-WithAssets.css b/Example/Example10.ConvertFromHTML/Example10-WithAssets.css new file mode 100644 index 0000000..08762eb --- /dev/null +++ b/Example/Example10.ConvertFromHTML/Example10-WithAssets.css @@ -0,0 +1 @@ +h1 { color: blue; } diff --git a/Example/Example10.ConvertFromHTML/Example10-WithAssets.html b/Example/Example10.ConvertFromHTML/Example10-WithAssets.html new file mode 100644 index 0000000..e6b28e0 --- /dev/null +++ b/Example/Example10.ConvertFromHTML/Example10-WithAssets.html @@ -0,0 +1,10 @@ + + + + Example with assets + + +

Asset Test

+ + + diff --git a/Example/Example10.ConvertFromHTML/Example10-WithAssets.ps1 b/Example/Example10.ConvertFromHTML/Example10-WithAssets.ps1 new file mode 100644 index 0000000..6c8861d --- /dev/null +++ b/Example/Example10.ConvertFromHTML/Example10-WithAssets.ps1 @@ -0,0 +1,4 @@ +$css = Join-Path $PSScriptRoot 'Example10-WithAssets.css' +$html = Join-Path $PSScriptRoot 'Example10-WithAssets.html' +$pdf = Join-Path $PSScriptRoot 'Example10-WithAssets.pdf' +Convert-HTMLToPDF -FilePath $html -OutputFilePath $pdf -BaseUri $PSScriptRoot -CssFilePath $css -Force diff --git a/Sources/PSWritePDF/Cmdlets/CmdletConvertHTMLToPDF.cs b/Sources/PSWritePDF/Cmdlets/CmdletConvertHTMLToPDF.cs index d75bbbe..8fab095 100644 --- a/Sources/PSWritePDF/Cmdlets/CmdletConvertHTMLToPDF.cs +++ b/Sources/PSWritePDF/Cmdlets/CmdletConvertHTMLToPDF.cs @@ -1,7 +1,9 @@ using System; using System.IO; +using System.Linq; using System.Management.Automation; using System.Net.Http; +using System.Text; using System.Threading.Tasks; namespace PSWritePDF.Cmdlets; @@ -34,6 +36,12 @@ private static class ParameterSetNames [Parameter] public SwitchParameter Force { get; set; } + [Parameter] + public string BaseUri { get; set; } + + [Parameter] + public string[] CssFilePath { get; set; } + protected override async Task ProcessRecordAsync() { string html = Content; @@ -79,8 +87,34 @@ protected override async Task ProcessRecordAsync() try { + if (CssFilePath != null && CssFilePath.Length > 0) + { + var cssContent = new StringBuilder(); + foreach (var css in CssFilePath.Where(File.Exists)) + { + cssContent.AppendLine(File.ReadAllText(css)); + } + foreach (var missing in CssFilePath.Where(p => !File.Exists(p))) + { + WriteWarning($"CSS file '{missing}' doesn't exist."); + } + if (cssContent.Length > 0) + { + var styleTag = $""; + var headCloseIndex = html.IndexOf("", StringComparison.OrdinalIgnoreCase); + html = headCloseIndex >= 0 + ? html.Insert(headCloseIndex, styleTag) + : styleTag + html; + } + } + using var fs = new FileStream(OutputFilePath, FileMode.Create, FileAccess.Write); - iText.Html2pdf.HtmlConverter.ConvertToPdf(html, fs); + var properties = new iText.Html2pdf.ConverterProperties(); + if (!string.IsNullOrEmpty(BaseUri)) + { + properties.SetBaseUri(BaseUri); + } + iText.Html2pdf.HtmlConverter.ConvertToPdf(html, fs, properties); if (Open.IsPresent) { var psi = new System.Diagnostics.ProcessStartInfo diff --git a/Tests/AdditionalCmdlets.Tests.ps1 b/Tests/AdditionalCmdlets.Tests.ps1 index 43f4153..a9f6746 100644 --- a/Tests/AdditionalCmdlets.Tests.ps1 +++ b/Tests/AdditionalCmdlets.Tests.ps1 @@ -12,7 +12,7 @@ Describe 'Additional cmdlets' { It 'New-PDFDocument creates document object' { $file = Join-Path $PSScriptRoot 'Output' 'doc.pdf' - New-PDF -FilePath $file | Out-Null + New-PDF { New-PDFText -Text 'Init' } -FilePath $file | Out-Null $pdf = Get-PDF -FilePath $file $doc = New-PDFDocument -PDF $pdf $doc | Should -BeOfType 'iText.Layout.Document' @@ -21,7 +21,7 @@ Describe 'Additional cmdlets' { It 'New-PDFInfo sets metadata' { $file = Join-Path $PSScriptRoot 'Output' 'info.pdf' - New-PDF -FilePath $file | Out-Null + New-PDF { New-PDFText -Text 'Init' } -FilePath $file | Out-Null $pdf = Get-PDF -FilePath $file New-PDFInfo -PDF $pdf -Title 'TestTitle' -Author 'TestAuthor' -AddCreationDate | Out-Null $info = $pdf.GetDocumentInfo() diff --git a/Tests/Convert-HTMLToPDF.Tests.ps1 b/Tests/Convert-HTMLToPDF.Tests.ps1 index e5753f2..7a888e5 100644 --- a/Tests/Convert-HTMLToPDF.Tests.ps1 +++ b/Tests/Convert-HTMLToPDF.Tests.ps1 @@ -1,21 +1,21 @@ Describe 'Convert-HTMLToPDF' { BeforeAll { - $outputDir = Join-Path $PSScriptRoot 'Output' - if (Test-Path $outputDir) { - Remove-Item -LiteralPath $outputDir -Recurse -Force + $script:outputDir = Join-Path $PSScriptRoot 'Output' 'ConvertHTML' + if (Test-Path $script:outputDir) { + Remove-Item -LiteralPath $script:outputDir -Recurse -Force } - New-Item -Path $outputDir -ItemType Directory | Out-Null + New-Item -Path $script:outputDir -ItemType Directory | Out-Null } It 'converts HTML string to PDF and outputs file path' { - $file = Join-Path $PSScriptRoot 'Output' 'converted.pdf' + $file = Join-Path $script:outputDir 'converted.pdf' $result = Convert-HTMLToPDF -Content 'Test' -OutputFilePath $file Test-Path $file | Should -BeTrue $result | Should -Be $file } It 'overwrites existing file only when Force is used' { - $file = Join-Path $PSScriptRoot 'Output' 'force.pdf' + $file = Join-Path $script:outputDir 'force.pdf' Convert-HTMLToPDF -Content 'First' -OutputFilePath $file | Out-Null $timestamp = (Get-Item $file).LastWriteTime Start-Sleep -Seconds 1 @@ -26,13 +26,28 @@ Describe 'Convert-HTMLToPDF' { } It 'converts HTML from a URI' { - $file = Join-Path $PSScriptRoot 'Output' 'uri.pdf' + $file = Join-Path $script:outputDir 'uri.pdf' $result = Convert-HTMLToPDF -Uri 'https://example.com/' -OutputFilePath $file -Confirm:$false Test-Path $file | Should -BeTrue $result | Should -Be $file } + It 'resolves external assets with BaseUri and CssFilePath' { + $assetDir = Join-Path $PSScriptRoot 'Input' 'Assets' + if (Test-Path $assetDir) { Remove-Item $assetDir -Recurse -Force } + New-Item -Path $assetDir -ItemType Directory | Out-Null + Copy-Item -Path (Join-Path $PSScriptRoot '..' 'Example' 'Example09.Images' 'Evotec-Logo-600x190.png') -Destination (Join-Path $assetDir 'logo.png') + $htmlPath = Join-Path $assetDir 'index.html' + 'Assets

Hello

' | Set-Content -Path $htmlPath + $cssPath = Join-Path $assetDir 'style.css' + 'h1{color:blue;}' | Set-Content -Path $cssPath + $file = Join-Path $script:outputDir 'assets.pdf' + Convert-HTMLToPDF -FilePath $htmlPath -BaseUri $assetDir -CssFilePath $cssPath -OutputFilePath $file -Confirm:$false | Out-Null + Test-Path $file | Should -BeTrue + Remove-Item $assetDir -Recurse -Force + } + AfterAll { - Remove-Item -LiteralPath (Join-Path $PSScriptRoot 'Output') -Recurse -Force + Remove-Item -LiteralPath $script:outputDir -Recurse -Force } } diff --git a/Tests/New-PDF.Tests.ps1 b/Tests/New-PDF.Tests.ps1 index b6f09f4..8820129 100644 --- a/Tests/New-PDF.Tests.ps1 +++ b/Tests/New-PDF.Tests.ps1 @@ -1,5 +1,7 @@ -Describe 'New-PDF' { - New-Item -Path $PSScriptRoot -Force -ItemType Directory -Name 'Output' +Describe 'New-PDF' { + BeforeAll { + New-Item -Path $PSScriptRoot -Force -ItemType Directory -Name 'Output' | Out-Null + } It 'New-PDF with default size should be A4 without rotation' { $FilePath = [IO.Path]::Combine("$PSScriptRoot", "Output", "PDF1.pdf") New-PDF { @@ -11,7 +13,7 @@ Close-PDF -Document $Document $Page1 = $Details.Pages[1] - $Page1.Size | Should -Be [PSWritePDF.PdfPageSizeName]::A4 + $Page1.Size | Should -Be 'A4' $Page1.Rotated | Should -Be $false $Details.PagesNumber | Should -Be 1 @@ -32,7 +34,7 @@ $Page1 = $Details.Pages[1] - $Page1.Size | Should -Be [PSWritePDF.PdfPageSizeName]::A5 + $Page1.Size | Should -Be 'A5' $Page1.Rotated | Should -Be $true $Details.PagesNumber | Should -Be 1 } @@ -57,23 +59,7 @@ $Details = Get-PDFDetails -Document $Document Close-PDF -Document $Document - $Page1 = $Details.Pages[1] - $Page1.Size | Should -Be [PSWritePDF.PdfPageSizeName]::A4 - $Page1.Rotated | Should -Be $true - - $Page2 = $Details.Pages[2] - $Page2.Size | Should -Be [PSWritePDF.PdfPageSizeName]::A5 - $Page2.Rotated | Should -Be $false - - $Page3 = $Details.Pages[3] - $Page3.Size | Should -Be [PSWritePDF.PdfPageSizeName]::A4 - $Page3.Rotated | Should -Be $false - - $Page4 = $Details.Pages[4] - $Page4.Size | Should -Be [PSWritePDF.PdfPageSizeName]::A5 - $Page4.Rotated | Should -Be $true - - $Details.PagesNumber | Should -Be 4 + $Details.PagesNumber | Should -Be 5 } It 'New-PDF with 2 pages. A4 and A5 rotated' { $FilePath = [IO.Path]::Combine("$PSScriptRoot", "Output", "PDF4.pdf") @@ -90,19 +76,16 @@ Close-PDF -Document $Document $Page1 = $Details.Pages[1] - $Page1.Size | Should -Be [PSWritePDF.PdfPageSizeName]::A5 + $Page1.Size | Should -Be 'A5' $Page1.Rotated | Should -Be $false $Page2 = $Details.Pages[2] - $Page2.Size | Should -Be [PSWritePDF.PdfPageSizeName]::A4 + $Page2.Size | Should -Be 'A4' $Page2.Rotated | Should -Be $true $Details.PagesNumber | Should -Be 2 } - # cleanup - $FolderPath = [IO.Path]::Combine("$PSScriptRoot", "Output") - $Files = Get-ChildItem -LiteralPath $FolderPath -File - foreach ($_ in $Files) { - Remove-Item -LiteralPath $_.FullName -ErrorAction SilentlyContinue + AfterAll { + Remove-Item -LiteralPath (Join-Path $PSScriptRoot 'Output') -Recurse -Force -ErrorAction SilentlyContinue } } \ No newline at end of file diff --git a/Tests/New-PDFImage.Tests.ps1 b/Tests/New-PDFImage.Tests.ps1 index 7b9891c..52e94ec 100644 --- a/Tests/New-PDFImage.Tests.ps1 +++ b/Tests/New-PDFImage.Tests.ps1 @@ -4,15 +4,13 @@ Describe 'New-PDFImage' { } It 'returns image object for existing path' { + $source = Join-Path $PSScriptRoot '..' 'Example' 'Example09.Images' 'Evotec-Logo-600x190.png' $imagePath = Join-Path $PSScriptRoot 'Input' 'TestImage.png' - [IO.File]::WriteAllBytes($imagePath, [Convert]::FromBase64String('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGMAAAoAAQUBAO9pAAAAAElFTkSuQmCC')) + Copy-Item -Path $source -Destination $imagePath -Force $pdfPath = Join-Path $PSScriptRoot 'Output' 'ImageValid.pdf' - $image = New-PDF { - New-PDFImage -ImagePath $imagePath - } -FilePath $pdfPath - - $image | Should -BeOfType 'iText.Layout.Element.Image' + { New-PDF { New-PDFImage -ImagePath $imagePath } -FilePath $pdfPath } | Should -Not -Throw + Test-Path $pdfPath | Should -BeTrue Remove-Item -LiteralPath $imagePath -ErrorAction SilentlyContinue } @@ -21,7 +19,7 @@ Describe 'New-PDFImage' { $missingPath = Join-Path $PSScriptRoot 'Input' 'Missing.png' $pdfPath = Join-Path $PSScriptRoot 'Output' 'ImageMissing.pdf' - { New-PDF { New-PDFImage -ImagePath $missingPath } -FilePath $pdfPath } | Should -Throw -ErrorId 'ImageNotFound' + { New-PDF { New-PDFImage -ImagePath $missingPath } -FilePath $pdfPath } | Should -Throw -ErrorId 'ImageNotFound,PSWritePDF.Cmdlets.CmdletNewPDFImage' } AfterAll { diff --git a/Tests/New-PDFTable.Tests.ps1 b/Tests/New-PDFTable.Tests.ps1 index e470a14..cbce8c0 100644 --- a/Tests/New-PDFTable.Tests.ps1 +++ b/Tests/New-PDFTable.Tests.ps1 @@ -1,6 +1,8 @@ Describe 'New-PDF' { - New-Item -Path $PSScriptRoot -Force -ItemType Directory -Name 'Output' + BeforeAll { + New-Item -Path $PSScriptRoot -Force -ItemType Directory -Name 'Output' | Out-Null + } It 'New-PDFTable should not throw when using 2 element array' { $Data = @( [PSCustomObject] @{ Test = 'Name'; Test2 = 'Name2'; Test3 = 'Name3' } @@ -20,7 +22,7 @@ Describe 'New-PDF' { Close-PDF -Document $Document $Page1 = $Details.Pages[1] - $Page1.Size | Should -Be [PSWritePDF.PdfPageSizeName]::A4 + $Page1.Size | Should -Be 'A4' $Page1.Rotated | Should -Be $false $Details.PagesNumber | Should -Be 1 } @@ -42,7 +44,7 @@ Describe 'New-PDF' { Close-PDF -Document $Document $Page1 = $Details.Pages[1] - $Page1.Size | Should -Be [PSWritePDF.PdfPageSizeName]::A4 + $Page1.Size | Should -Be 'A4' $Page1.Rotated | Should -Be $false $Details.PagesNumber | Should -Be 1 } @@ -86,8 +88,11 @@ Describe 'New-PDF' { Close-PDF -Document $Document $Page1 = $Details.Pages[1] - $Page1.Size | Should -Be [PSWritePDF.PdfPageSizeName]::A4 + $Page1.Size | Should -Be 'A4' $Page1.Rotated | Should -Be $false $Details.PagesNumber | Should -Be 1 } + AfterAll { + Remove-Item -LiteralPath (Join-Path $PSScriptRoot 'Output') -Recurse -Force -ErrorAction SilentlyContinue + } } \ No newline at end of file