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
1 change: 1 addition & 0 deletions Example/Example10.ConvertFromHTML/Example10-WithAssets.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h1 { color: blue; }
10 changes: 10 additions & 0 deletions Example/Example10.ConvertFromHTML/Example10-WithAssets.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Example with assets</title>
</head>
<body>
<h1>Asset Test</h1>
<img src="../Example09.Images/Evotec-Logo-600x190.png" />
</body>
</html>
4 changes: 4 additions & 0 deletions Example/Example10.ConvertFromHTML/Example10-WithAssets.ps1
Original file line number Diff line number Diff line change
@@ -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
36 changes: 35 additions & 1 deletion Sources/PSWritePDF/Cmdlets/CmdletConvertHTMLToPDF.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = $"<style>{cssContent}</style>";
var headCloseIndex = html.IndexOf("</head>", 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
Expand Down
4 changes: 2 additions & 2 deletions Tests/AdditionalCmdlets.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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()
Expand Down
31 changes: 23 additions & 8 deletions Tests/Convert-HTMLToPDF.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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 '<html><body>Test</body></html>' -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 '<html><body>First</body></html>' -OutputFilePath $file | Out-Null
$timestamp = (Get-Item $file).LastWriteTime
Start-Sleep -Seconds 1
Expand All @@ -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'
'<html><head><title>Assets</title></head><body><h1>Hello</h1><img src="logo.png" /></body></html>' | 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
}
}
39 changes: 11 additions & 28 deletions Tests/New-PDF.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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

Expand All @@ -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
}
Expand All @@ -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")
Expand All @@ -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
}
}
12 changes: 5 additions & 7 deletions Tests/New-PDFImage.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions Tests/New-PDFTable.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' }
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
}
Loading