[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$OutDir = (Join-Path (Get-Location) 'pdf'), [switch]$Force ) $ErrorActionPreference = 'Stop' $resolved = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path if (-not (Test-Path -LiteralPath $resolved -PathType Leaf)) { throw "Файл не найден: $resolved" } $extension = [IO.Path]::GetExtension($resolved).ToLowerInvariant() if ($extension -notin @('.html', '.htm')) { throw 'Нужен файл .html или .htm' } $candidates = New-Object System.Collections.Generic.List[string] $command = Get-Command -Name msedge.exe -ErrorAction SilentlyContinue if ($command -and $command.Source) { $candidates.Add($command.Source) } $programFilesX86 = [Environment]::GetEnvironmentVariable('ProgramFiles(x86)') $programFiles = [Environment]::GetEnvironmentVariable('ProgramFiles') if ($programFilesX86) { $candidates.Add((Join-Path $programFilesX86 'Microsoft\Edge\Application\msedge.exe')) } if ($programFiles) { $candidates.Add((Join-Path $programFiles 'Microsoft\Edge\Application\msedge.exe')) } $edge = $candidates | Where-Object { $_ -and (Test-Path -LiteralPath $_ -PathType Leaf) } | Select-Object -First 1 if (-not $edge) { throw 'Microsoft Edge не найден. Установите Edge и повторите запуск.' } if (-not (Test-Path -LiteralPath $OutDir)) { New-Item -ItemType Directory -Path $OutDir -Force | Out-Null } $outRoot = (Resolve-Path -LiteralPath $OutDir).Path $target = Join-Path $outRoot ([IO.Path]::GetFileNameWithoutExtension($resolved) + '.pdf') if ((Test-Path -LiteralPath $target) -and -not $Force) { throw "Файл уже существует: $target. Добавьте -Force." } $uri = ([Uri]$resolved).AbsoluteUri $arguments = @('--headless', '--disable-gpu', '--no-pdf-header-footer', "--print-to-pdf=`"$target`"", $uri) $process = Start-Process -FilePath $edge -ArgumentList $arguments -Wait -PassThru -NoNewWindow if ($process.ExitCode -ne 0 -or -not (Test-Path -LiteralPath $target)) { throw "Edge не создал PDF: $target" } Write-Host "Готово: $target"