Files
deerflow-code/tmp/maintenance-manual/render_with_word.ps1
T
2026-08-10 14:26:49 +08:00

39 lines
1.1 KiB
PowerShell

param(
[Parameter(Mandatory = $true)][string]$InputDocx,
[Parameter(Mandatory = $true)][string]$OutputPdf
)
$ErrorActionPreference = 'Stop'
$word = $null
$doc = $null
try {
$inputPath = (Resolve-Path -LiteralPath $InputDocx).Path
$outputPath = [System.IO.Path]::GetFullPath($OutputPdf)
$outputDir = [System.IO.Path]::GetDirectoryName($outputPath)
[System.IO.Directory]::CreateDirectory($outputDir) | Out-Null
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$word.DisplayAlerts = 0
$doc = $word.Documents.Open($inputPath, $false, $false)
foreach ($toc in $doc.TablesOfContents) {
$toc.Update() | Out-Null
}
$doc.Fields.Update() | Out-Null
$doc.Save()
$doc.ExportAsFixedFormat($outputPath, 17)
}
finally {
if ($null -ne $doc) {
$doc.Close(0)
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($doc)
}
if ($null -ne $word) {
$word.Quit()
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($word)
}
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
}