# PHP Convert Docx to PDF

Here's how you can do it:

1. **Install LibreOffice**:  
    Ensure you have LibreOffice installed on your server. You can install it using the following command:
    
    ```
    sudo apt-get install libreoffice
    ```
    
    <div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto"><div class="zeroclipboard-container position-absolute right-0 top-0"><svg aria-hidden="true" class="octicon octicon-copy js-clipboard-copy-icon m-2" data-view-component="true" height="16" version="1.1" viewbox="0 0 16 16" width="16"></svg>  
    </div></div>
2. **Use PHP to call LibreOffice for conversion**:  
    Use PHP to execute a command that converts the DOCX file to PDF using LibreOffice.

Here is the PHP script:

```
<?php
function convertDocxToPdf($docxFilePath, $outputDir)
{
    // Ensure the output directory exists
    if (!is_dir($outputDir)) {
        mkdir($outputDir, 0777, true);
    }

    // Define the output file path
    $pdfFilePath = $outputDir . '/' . pathinfo($docxFilePath, PATHINFO_FILENAME) . '.pdf';

    // Command to convert DOCX to PDF using LibreOffice
    $command = 'libreoffice --headless --convert-to pdf --outdir ' . escapeshellarg($outputDir) . ' ' . escapeshellarg($docxFilePath);

    // Execute the command
    exec($command, $output, $returnVar);

    // Check if the conversion was successful
    if ($returnVar == 0) {
        return $pdfFilePath;
    } else {
        return false;
    }
}

// Usage example
$docxFilePath = 'path/to/your/document.docx';
$outputDir = 'path/to/output/directory';
$pdfFilePath = convertDocxToPdf($docxFilePath, $outputDir);

if ($pdfFilePath) {
    echo 'PDF created successfully: ' . $pdfFilePath;
} else {
    echo 'Failed to create PDF.';
}
?>
```