0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
// Include the Nextcloud autoloader
require_once '/path/to/nextcloud/lib/autoload.php';
use OCPHttpClientIClient;
use OCPHttpClientIResponse;
use OCPHttpClientIClientService;
/**
* Convert a DOCX file to PDF using Nextcloud.
*
* @param string $docxFilePath The path to the DOCX file.
* @param string $pdfFilePath The path to save the converted PDF file.
* @param string $nextcloudUrl The URL of your Nextcloud instance.
* @param string $username The username to authenticate with Nextcloud.
* @param string $password The password to authenticate with Nextcloud.
*
* @return bool True if the conversion was successful, false otherwise.
*/
function convertDocxToPdf($docxFilePath, $pdfFilePath, $nextcloudUrl, $username, $password)
{
// Create a new instance of the Nextcloud client
$clientService = OC::$server->get(IClientService::class);
$client = $clientService->newClient();
// Authenticate with Nextcloud
$client->setDefaultOption('auth', [$username, $password]);
// Set the request URL
$url = $nextcloudUrl . '/index.php/apps/officeconverter/api/v1/convert';
// Set the request headers and parameters
$headers = ['Accept' => 'application/json'];
$parameters = [
'format' => 'pdf',
'file' => $docxFilePath,
];
// Send the request to convert the DOCX file to PDF
$response = $client->post($url, [
'headers' => $headers,
'form_params' => $parameters,
]);
// Check if the request was successful
if ($response->getStatusCode() === IResponse::HTTP_OK) {
// Save the converted PDF file
file_put_contents($pdfFilePath, $response->getBody());
return true;
}
return false;
}
// Usage example
$docxFilePath = '/path/to/docx/file.docx';
$pdfFilePath = '/path/to/save/pdf/file.pdf';
$nextcloudUrl = 'https://example.com/nextcloud';
$username = 'your-username';
$password = 'your-password';
if (convertDocxToPdf($docxFilePath, $pdfFilePath, $nextcloudUrl, $username, $password)) {
echo 'File converted successfully.';
} else {
echo 'Failed to convert the file.';
}
Make sure to replace the placeholders with the actual values:
This code uses the Nextcloud API to send a POST request to the Nextcloud Office Converter app, which converts the DOCX file to PDF. The converted PDF file is then saved to the specified location.