0

I have a powershell script to create a pdf from a docx. I can call it manually an it works fine:

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"

but if I call it from a GitLab pipeline, I get the following exception at the line $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

The script does not finish, but word takes more and more memory. I can only stop it by ending the task with the task manager.

  • The gitlab runner service is started with an admin user
  • I tried to execute the script manually with the same user, and it worked fine
  • I tried the same thing with python and had a similar error ValueError('NULL COM pointer access',), tried this -> same error
3
  • You create COM objects, but never release them from memory with [System.Runtime.Interopservices.Marshal]::ReleaseComObject(). No wonder Word is gobbling up your memory Commented Dec 11, 2024 at 12:01
  • ok, so I have to release the COM object after usage? But I think it doesen't affect my main problem, right? Even the first try after an restart of the server throws the exception. Commented Dec 12, 2024 at 11:12
  • This was one of your problems and I have told you what was wrong in your code regarding the memory usage. Commented Dec 12, 2024 at 14:04

1 Answer 1

0

I found this: https://support.microsoft.com/en-us/topic/considerations-for-server-side-automation-of-office-48bcfe93-8a89-47f1-0bce-017433ad79e2

Microsoft does not recommend or support server-side Automation of Office

I have given up trying with MS Office and am now using LibreOffice. This worked immediately and without any problems. I can't see any difference in the quality of our documents compared to documents exported with MS Office.

soffice --headless --convert-to pdf --outdir $ExportFolder $DocPath

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.