Настройка конвертации в PDF генерируемых по шаблону документов docx, xlsx

Если указать в SELECT 1 doc_XXX_getData процедуры генерации документа по шаблону параметр OutputPDF=1, то платформа на основе сгенерированного документа docx, xlsx запустит конвертацию в PDF через установленный на сервере LibreOffice и выдаст ссылку на созданный PDF файл. 

Чтобы это работало, необходимо сделать следующее: 

  1. Поместить скрипт ConvertDocument.ps1  Powershell в /wwwroot/app_data/scripts/  
  2. На сервере установить Libre Office    

Примечание. Если что-то не работает, то сначала можно попробовать  серверное действие на /asapi с такими данными:

Путь: C:/Program Files/PowerShell/7/pwsh.exe

Agruments: 

C:\Scripts\ConvertDocument.ps1  -InputFile C:\sites\site1\wwwroot\uploads\doc\invoice.docx -OutputDirectory C:\sites\site1\wwwroot\uploads\doc -LibreOfficeExe "C:\Program Files\LibreOffice\program\soffice.exe"

Также через параметры в /appsettings.json можно менять пути к Powershell и Libre: 

При настройке OutputPDF=1 исходный файл docx удаляется после конвертации в PDF. 

Текст скрипта Powershell ConvertDocument.ps1: 

[cmdletbinding()]
param(
[Parameter(Mandatory=$true)]$InputFile,
[Parameter(Mandatory=$true)]$OutputDirectory,
[Parameter(Mandatory=$true)]$LibreOfficeExe, 
$ConvertTo = "pdf",
$ConvertFrom = "docx"
)

Write-Verbose "Checking if LibreOffice EXE Location exists"
If (!(Test-Path $LibreOfficeExe))
{
    Write-Error "LibreOffice EXE location was not found, please specify another location"
}


Write-Verbose "Checking if InputFile exists"
if (!(Test-Path $InputFile))
{
    Write-Error "Input file was not found, please speciy another location"
}

Write-Verbose "Checking if OutputDirectory exists"
if (!(Test-Path $OutputDirectory))
{
    New-Item -ItemType Directory $Out
}

$File = $InputFile  # = Get-ChildItem $InputDirectory -Filter "*.$ConvertFrom"

$Argument = '--headless --convert-to ' + $ConvertTo + ' --outdir "' + $OutputDirectory + '" "' + $File + '"'
        Write "Starting convert using Arguments: $Argument"
        Start-Process $LibreOfficeExe -ArgumentList $Argument -Wait
        Write "$File has been converted"
Страница-источник на сайте falconspace.ru