2

I'm trying to set up the CUI, but when I try to send a request, I get a code error

Uncaught CloudConvert\Exceptions\HttpClientException: tasks: The tasks field is required

I need to send a file for conversion from my computer and receive html in response. Where is my error? Thank you in advance!

$job = (new Job())
   ->addTask(
       (new Task('import/upload', 'import-my-file'))
       ->set('file', fopen($DocumentPath, 'r'))
     )
   ->addTask(
       (new Task('convert', 'convert-doc-to-html'))
         ->set('input_format', 'doc')
         ->set('output_format', 'html')
         ->set('engine', 'office')
         ->set('input', ["import-my-file"])
     )
   ->addTask(
       (new Task('export/url', 'export-my-file'))
         ->set('input', ["convert-doc-to-html"])
         ->set('inline', false)
         ->set('archive_multiple_files', false)
     ); 
     
$cloudconvert->jobs()->create($job);
$uploadTask = $job->getTasks()->whereName('import-my-file')[0];

$cloudconvert->tasks()->upload($uploadTask, fopen($DocumentPath, 'r'), 'myfile.doc');
Denis Mak
  • 23
  • 2

2 Answers2

1

I had the same issue and it turned out it was the file I opened, I had to do the following for it to work:

$file = rtrim(file_get_contents($file, FILE_TEXT)); $file = mb_convert_encoding($file, 'UTF-8', mb_detect_encoding($file, 'UTF-8, ISO-8859-1', true));

and then put $file in to ->set('file', $file)

You can see, what I found out here: cloundconvert api v2 in php return The tasks field is required

Martin
  • 58
  • 1
  • 6
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 08 '21 at 20:26
1

I found this question and it helped me solving my problem for using CloudConvert API v2 to convert a file from .docx to .pdf. Here is a working php example for anyone else having troubles.

public static function convertUsingCloudconvert($input_file_path, $output_file_path)
{
    $cloudconvert = new CloudConvert([
        'api_key' => 'YOUR_API_KEY_HERE',
        'sandbox' => false
    ]);


    //create process names
    $upload_process = 'your-upload-name';
    $convert_process = 'your-convert-name';
    $export_process = 'your-export-name';
    $upload_filename = 'your-file-upload-name.docx';

    $convert_job = (new Job())
        ->addTask(new Task('import/upload', $upload_process)) //task for file upload
        ->addTask( //task for conversion process
            (new Task('convert', $convert_process))
                ->set('input', $upload_process)
                ->set('input_format', 'docx')
                ->set('output_format', 'pdf')
        )->addTask( //task for file download
            (new Task('export/url', $export_process))
                ->set('input', $convert_process)
        );

    $cloudconvert->jobs()->create($convert_job); //create conversion process

    $uploadTask = $convert_job->getTasks()->whereName($upload_process)[0];
    $cloudconvert->tasks()->upload($uploadTask, fopen($input_file_path, 'r'), $upload_filename); //upload file to convert

    $cloudconvert->jobs()->wait($convert_job); //wait for process to finish

    $export_urls = $convert_job->getExportUrls(); //get generated files
    if (!empty($export_urls)) { 
        foreach ($export_urls as $file) {
            $source = $cloudconvert->getHttpTransport()->download($file->url)->detach();
            file_put_contents($output_file_path, $source); //copy source to output path
        }
    }
    else {
        //some error handling
    }

}