I'm trying convert word file to pdf, but not working my code

I’m trying convert word file to pdf, but not working my code.
Before it’s works.

$docxFile = $filePath;
        $pdfFileName = 'destination.pdf';
        $api = new WordsApi($clientId, $clientSecret);

        $upload_request = new Requests\UploadFileRequest($docxFile, 'fileStoredInCloud.docx');
        $upload_result = $api->uploadFile($upload_request);

        // save as pdf file
        $saveOptions = new PdfSaveOptionsData(
            array(
                "file_name" => 'destination.pdf',
                'compress_images' => false,
                'image_compression' => 'None',
                'jpeg_quality' => 100,
                'text_compression' => 'None',
                'use_core_fonts' => true,
            )
        );

        $request = new Requests\SaveAsRequest('fileStoredInCloud.docx', $saveOptions);

        $result = $api->saveAs($request);

        $downloadRequest = new DownloadFileRequest($pdfFileName);
        $downloadResult = $api->downloadFile($downloadRequest);

I have error:

[ValueError] 
Path cannot be empty (0)
/var/www/www-root/data/www/docs/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/ObjectSerializer.php:535
#0: fopen(string, string)
	/var/www/www-root/data/www/docs/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/ObjectSerializer.php:535
#1: Aspose\Words\ObjectSerializer::deserialize(object, string, array)
	/var/www/www-root/data/www/docs/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php:13583
#2: Aspose\Words\WordsApi->downloadFileWithHttpInfo(object)
	/var/www/www-root/data/www/docs/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php:13517
#3: Aspose\Words\WordsApi->downloadFile(object)
	/var/www/www-root/data/www/docs/local/templates/alldocs/ajax/document_add.php:65
----------

@buterforever

It looks like you’re encountering a ValueError indicating that the path cannot be empty when trying to download the PDF file after converting a Word document. This error typically occurs when the file path provided for the download is not correctly specified or is missing.

Here are a few suggestions to troubleshoot and resolve the issue:

  1. Check the File Name: Ensure that the pdfFileName variable is correctly set to the name of the file you expect to download. In your code, you have it set as 'destination.pdf'. Make sure this matches the name of the file generated during the conversion process.

  2. Verify Upload and Conversion: Before attempting to download the PDF, confirm that the upload and conversion processes are successful. You can add checks to see if $upload_result and $result contain the expected values.

  3. Use Full Path for Download: When creating the DownloadFileRequest, ensure that you are using the correct path where the file is stored in the cloud. If the file is stored in a specific folder, you may need to include that in the path.

  4. Check API Documentation: Review the API documentation for the DownloadFileRequest to ensure that you are using it correctly. There may be specific requirements for the file path or name.

Here’s a modified version of your code snippet that includes a check for the upload and conversion results:

$docxFile = $filePath;
$pdfFileName = 'destination.pdf';
$api = new WordsApi($clientId, $clientSecret);

// Upload the file
$upload_request = new Requests\UploadFileRequest($docxFile, 'fileStoredInCloud.docx');
$upload_result = $api->uploadFile($upload_request);

// Check if upload was successful
if ($upload_result->getStatus() !== 'OK') {
    throw new Exception('File upload failed: ' . $upload_result->getStatus());
}

// Save as PDF
$saveOptions = new PdfSaveOptionsData(
    array(
        "file_name" => $pdfFileName,
        'compress_images' => false,
        'image_compression' => 'None',
        'jpeg_quality' => 100,
        'text_compression' => 'None',
        'use_core_fonts' => true,
    )
);

$request = new Requests\SaveAsRequest('fileStoredInCloud.docx', $saveOptions);
$result = $api->saveAs($request);

// Check if conversion was successful
if ($result->getStatus() !== 'OK') {
    throw new Exception('File conversion failed: ' . $result->getStatus());
}

// Download the PDF file
$downloadRequest = new DownloadFileRequest($pdfFileName);
$downloadResult = $api->downloadFile($downloadRequest);

Make sure to replace the placeholder checks with actual conditions based on the API’s response structure. This should help you identify where the issue lies in your process. If the problem persists, consider checking the API logs for more detailed error messages.

If you need further assistance, feel free to ask!

If i modify code:

$downloadRequest = new DownloadFileRequest($_SERVER['DOCUMENT_ROOT'] . $pdfFileName);

I have error:

[Aspose\Words\ApiException] 
[400] Error connecting to the API (https://api.aspose.cloud/v4.0/words/storage/file/var/www/www-root/data/www/docs/destination.pdf) (400)
/var/www/www-root/data/www/docs/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php:13558
#0: Aspose\Words\WordsApi->downloadFileWithHttpInfo(object)
	/var/www/www-root/data/www/docs/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php:13517
#1: Aspose\Words\WordsApi->downloadFile(object)
	/var/www/www-root/data/www/docs/local/templates/alldocs/ajax/document_add.php:65
----------

Yes, this is okay, as, in DownloadFileRequest, you should pass the path in the storage, not the local path.
I will try to reproduce this issue using the initial code you shared.

Hello! How can i make this request ?

Can you try changing download request to smth like this

$downloadRequest = new DownloadFileRequest($result->getSaveResult()->getDestDocument()->getHref());
$downloadResult = $this->words->downloadFile($downloadRequest);

As I unable reproduce this issue at my side.

Result of
$downloadRequest = new DownloadFileRequest($result->getSaveResult()->getDestDocument()->getHref());
is
Aspose\Words\Model\Requests\DownloadFileRequest Object ( [path] => destination.pdf [storage_name] => [version_id] => )

Result of
$downloadResult = $api->downloadFile($downloadRequest);
is
[ValueError] Path cannot be empty (0) /var/www/www-root/data/www/docs/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/ObjectSerializer.php:535 #0: fopen(string, string) /var/www/www-root/data/www/docs/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/ObjectSerializer.php:535 #1: Aspose\Words\ObjectSerializer::deserialize(object, string, array) /var/www/www-root/data/www/docs/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php:13583 #2: Aspose\Words\WordsApi->downloadFileWithHttpInfo(object) /var/www/www-root/data/www/docs/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php:13517 #3: Aspose\Words\WordsApi->downloadFile(object) /var/www/www-root/data/www/docs/local/templates/alldocs/ajax/document_add.php:80

Okay, then, could you share the source file you converted? I will try it, and maybe the file is the issue.

After I got the error, I checked the storage and saw that the original docx file and the converted pdf file were stored there.

Thanks for sharing the document.

What can i do ?

As I can’t reproduce this issue, I can suggest using the SaveAsOnline method as a workaround. This method’s response contains the converted file.