我有一个 powershell 脚本可以从 docx 创建 pdf。我可以手动调用它,它工作正常:
param(
[string]$File, # Path to document
[string]$DestinationFolder, # Path of folder
[string]$NewName # New name for converted file
)
# ======== check if parameter are valid ========
if (-not $File) {
Write-Error "Parameter '-File' must not be empty"
exit 1
}
if (-not $DestinationFolder) {
$DestinationFolder = Split-Path -Path $File
}
if (-not $NewName) {
$NewName = [System.IO.Path]::GetFileNameWithoutExtension($File) + ".pdf"
}
# ======== create destination folder if not exists ========
if (-not (Test-Path -Path $DestinationFolder)) {
$null = New-Item -Path $DestinationFolder -ItemType Directory -Force
Write-Output "Path did not exist. Created folder: $DestinationFolder"
}
# ======== export pdf ========
Write-Output "Export pdf"
$Word = New-Object -ComObject 'Word.Application'
$Document = $Word.Documents.Open($File)
$NewFilePath = [string](Join-Path $DestinationFolder $NewName)
$Document.SaveAs($NewFilePath, 17)
$Document.Close()
$Word.Quit()
Write-Output "Stored to $NewFilePath"
但是如果我从 GitLab 管道调用它,我会在
$Document.SaveAs($NewFilePath, 17)
: 行收到以下异常
At D:\GitLab-Runner\builds\yxFfC2Egu\0\SPS_Entwicklung\test\ci-dev-test\ci\release\docx_to_pdf.ps1
:44 char:5
+ $Document.SaveAs($NewFilePath, 17)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At D:\GitLab-Runner\builds\yxFfC2Egu\0\SPS_Entwicklung\test\ci-dev-test\ci\release\docx_to_pdf.ps1
:45 char:5
+ $Document.Close()
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
剧本没有写完,但是单词占用了越来越多的内存。我只能通过任务管理器结束任务来阻止它。
ValueError('NULL COM pointer access',)
,尝试过this - >同样的错误Microsoft 不推荐或支持 Office 的服务器端自动化
我已经放弃尝试使用 MS Office,现在使用 LibreOffice。这立即生效并且没有任何问题。与使用 MS Office 导出的文档相比,我看不出我们的文档质量有任何差异。
soffice --headless --convert-to pdf --outdir $ExportFolder $DocPath