Error setting image format: Can not process empty Imagick object

I am getting error when i am converting HEIC format to pdf. the code breakes on $imagick->setImageFormat(‘pdf’); line. that gives error Error setting image format: Can not process empty Imagick object.
Your answer will be appreciated.

if ($doc['file_type'] == 'application/octet-stream') {
                    $GLOBALS["log"]->fatal("Converting HEIC document : " . print_r($doc, true));
                    $document_name = $doc['document_revision_id'];
                    $GLOBALS["log"]->fatal("PDF saved at: 1");
                    $directory = "upload/";
                    $img_path = $directory .$document_name;
                    $pdfPath = $directory .$document_name;
                    $GLOBALS["log"]->fatal("PDF saved at: " .$img_path);
                    $GLOBALS["log"]->fatal("PDF saved at: 2");
                    // Load the HEIC image using Imagick
                    $imagick = new Imagick();
                    $GLOBALS["log"]->fatal("PDF saved at: 3");
                    
                    $imagick->readImage('heif:', $img_path);
                    $GLOBALS["log"]->fatal("PDF saved at: 4");
                    // Convert the HEIC image to PDF
                    try {
                        $imagick->setImageFormat('pdf');
                    } catch (Exception $e) {
                        $error_message = $e->getMessage();
                        $GLOBALS["log"]->fatal("Error setting image format: " . $error_message);
                        return false; // or handle the error as needed
                    }
                    // $imagick->setImageFormat('pdf');
                    $GLOBALS["log"]->fatal("PDF saved at: 5");
                    // $pdfPath = "path/to/save/converted/pdf.pdf";
                    $imagick->writeImages($pdfPath, true);
                    
                    $GLOBALS["log"]->fatal("PDF saved at: " . $pdfPath);

                    //Changing file name + file extension
                    $document_revision_bean = BeanFactory::getBean('DocumentRevisions', $doc['document_revision_id']);
                    $GLOBALS["log"]->fatal("Old File Ext : " . $document_revision_bean->file_ext);
                    $GLOBALS["log"]->fatal("Old File Name : " . $document_revision_bean->filename);
                    $file_name_arr = explode(".", $document_revision_bean->filename);
                    $GLOBALS["log"]->fatal("File ext From Name: " . end($file_name_arr));

                    $newName = $this->lastReplace(end($file_name_arr), "pdf", $document_revision_bean->filename);
                    $GLOBALS["log"]->fatal("New File Name : " . $newName);
                    $document_revision_bean->filename = $newName;
                    $document_revision_bean->file_ext = 'pdf';
                    $document_revision_bean->save();
                }