0

我使用以下内容在 PHP 中将 doc 文件转换为 pdf:

function CallToApi($fileToConvert, $pathToSaveOutputFile, $apiKey, &$message,$unique_filename)
{
    try
    {


        $fileName = $unique_filename.".pdf";

        $postdata =  array('OutputFileName' => $fileName, 'ApiKey' => $apiKey, 'file'=>"@".$fileToConvert);
        $ch = curl_init("http://do.convertapi.com/word2pdf");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        $result = curl_exec($ch); 
        $headers = curl_getinfo($ch);

        $header=ParseHeader(substr($result,0,$headers["header_size"]));
        $body=substr($result, $headers["header_size"]);

        curl_close($ch);
        if ( 0 < $headers['http_code'] && $headers['http_code'] < 400 ) 
        {
            // Check for Result = true

            if (in_array('Result',array_keys($header)) ?  !$header['Result']=="True" : true)
            {
                $message = "Something went wrong with request, did not reach ConvertApi service.<br />";
                return false;
            }
            // Check content type 
            if ($headers['content_type']<>"application/pdf")
            {
                $message = "Exception Message : returned content is not PDF file.<br />";
                return false;
            }
            $fp = fopen($pathToSaveOutputFile.$fileName, "wbx");

            fwrite($fp, $body);

            $message = "The conversion was successful! The word file $fileToConvert converted to PDF and saved at $pathToSaveOutputFile$fileName";
            return true;
        }
        else 
        {
         $message = "Exception Message : ".$result .".<br />Status Code :".$headers['http_code'].".<br />";
         return false; 
        }
    }
    catch (Exception $e) 
    {   
        $message = "Exception Message :".$e.Message."</br>";
        return false; 
    }
}

我现在想用同样的方法将 ppt 转换为 pdf。我为此改变

$ch = curl_init("http://do.convertapi.com/word2pdf"); 

$ch = curl_init("http://do.convertapi.com/PowerPoint2Pdf");

但我不确定为什么它不转换给定的输入。有什么我可能会丢失的吗?

4

1 回答 1

0

这是由于 PHP 5.6。

您还需要将此行添加到您的代码中

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

请参阅:向后不兼容的更改(底部)。

于 2016-04-02T11:43:14.710 回答