Как docx файл конвертировать в pdf (LibreOffice, powershell)

Задача - из готового файла docx получить файл pdf. 

Для решения потребуется выполнение скрипта powershell и установленный LibreOffice на сервере. 

1. Ставим LibreOffice

2. Используем скрипт Powershell https://github.com/perplexityjeff/PowerShell-DOCX-PDF-Converter

[cmdletbinding()]
param(
[Parameter(Mandatory=$true)]$InputDirectory,
[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 OutputDirectory exists"
if (!(Test-Path $OutputDirectory))
{
    New-Item -ItemType Directory $Out
}

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

$Files = Get-ChildItem $InputDirectory -Filter "*.$ConvertFrom"

foreach($File in $Files)
{
    if ($File.Exists)
    {
        $Argument = '--headless --convert-to ' + $ConvertTo + ' --outdir "' + $OutputDirectory + '" "' + $File.FullName + '"'
        Write-Verbose "Starting convert using Arguments: $Argument"
        Start-Process $LibreOfficeExe -ArgumentList $Argument -Wait
        Write-Verbose "$File has been converted"
    }
}

Write-Verbose "OK"

3. Команда для выполнения powershell: 

.\Convert-Documents.ps1 -InputDirectory C:\Temp\In -OutputDirectory C:\Temp\Out -LibreOfficeExe "C:\Program Files\LibreOffice\program\soffice.exe

4. Данный скрипт можно вызвать через внешнее действие из платформы (к примеру, сгенерировали docx файл, затем вызвали внешнее действие по конвертации и затем через JS коллбек выдали ссылку на конечный PDF файл пользователю).

Falcon Space - функциональная веб-платформа разработки на узком стеке MS SQL/Bootstrap. Вводная по Falcon Space
Насколько полезной была статья?

Google поиск по нашей документации

Выгода от использования Falcon Space

В 2-3 раза экономнее и быстрее, чем заказная разработка
Более гибкая, чем коробочные решения и облачные сервисы
Используйте готовые решения и изменяйте под свои потребности
Нужна бесплатная консультация?
Планируете делать веб-проект?
Сайт использует Cookie. Правила конфиденциальности OK