0

我需要将 doc 文件转换为 pdf 格式,因为我正在遵循下面提到的 saaspose 函数(http://api.saaspose.com/v1.0/words/help)我没有收到错误,并且上传了垃圾文件使用 doc 文件,但我需要上传具有相同 doc 内容的 pdf 文件。

 private function convertDocToPdf($inputFileName,$outputFileName)
            {
                $doc = new WordDocument("");//Word document is a class to create new document
                $result = $doc->ConvertLocalFile($inputFileName, $outputFileName,"pdf");

            }

private function ConvertLocalFile($input_path, $output_path, $output_format) {
        try {
            $str_uri = "http://api.saaspose.com/v1.0/words/convert?format=" + $output_format;
            $signed_uri = Sign($str_uri);
            $responseStream = uploadFileBinary($signed_uri, $input_path, "xml");

            $v_output = ValidateOutput($responseStream);

            if ($v_output === "") {
                if ($output_format == "html")
                    $save_format = "zip";
                else
                    $save_format = $output_format;


                saveFile($responseStream,$outputFilename);
                return "";
            }
            else
                return $v_output;
        } catch (Exception $e) { 
            throw new Exception($e->getMessage());
        }
    }

 public static function saveFile($input, $fileName){
            $fh = fopen($fileName, 'w') or die("can't open file");
            fwrite($fh, $input);
            fclose($fh);
        }


public static function Sign($UrlToSign) {
        // parse the url
        $url = parse_url($UrlToSign);

        if (isset($url['query']) == "")
            $urlPartToSign = $url['path'] . "?appSID=" . 1fc17cb5-6c47-462e-9dfd-d1a9525220fa;
        else
            $urlPartToSign = $url['path'] . "?" . str_replace(" ","%20",$url["query"]) . "&appSID=" . SaasposeApp::$AppSID;


        // Decode the private key into its binary format
        $decodedKey = self::decodeBase64UrlSafe(87c027855aebf72e204b3bcd710de1c0);


        // Create a signature using the private key and the URL-encoded
        // string using HMAC SHA1. This signature will be binary.
        $signature = hash_hmac("sha1", $urlPartToSign, $decodedKey, true);

        $encodedSignature = self::encodeBase64UrlSafe($signature);

        if (isset($url['query']) == "")
            return $url["scheme"] . "://" . $url["host"] . str_replace(" ", "%20",$url["path"]) . "?appSID=" . 1fc17cb5-6c47-462e-9dfd-d1a9525220fa  . "&signature=" . $encodedSignature;
        else
            return $url["scheme"] . "://" . $url["host"] . str_replace(" ", "%20",$url["path"]) . "?" . str_replace(" ","%20",$url["query"]) . "&appSID=" . 1fc17cb5-6c47-462e-9dfd-d1a9525220fa . "&signature=" . $encodedSignature;
    }

public static function uploadFileBinary($url, $localfile, $headerType="XML") {

        $headerType = strtoupper($headerType);
        $fp = fopen($localfile, "r");

        $session = curl_init();
        curl_setopt($session, CURLOPT_VERBOSE, 1);
        curl_setopt($session, CURLOPT_URL, $url);
        curl_setopt($session, CURLOPT_PUT, 1);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($session, CURLOPT_HEADER, false);

        if ($headerType == "XML") {
            curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
        } else {
            curl_setopt($session, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
        }
        curl_setopt($session, CURLOPT_INFILE, $fp);
        curl_setopt($session, CURLOPT_INFILESIZE, filesize($localfile));


        $result = curl_exec($session);
        //$error = curl_error($session);
        //$http_code = curl_getinfo($session, CURLINFO_HTTP_CODE);

        curl_close($session);
        fclose($fp);
        return $result;
    }

public static function ValidateOutput($result)
    {
        $string = (string)$result;

        $validate = array("Unknown file format.", "Unable to read beyond the end of the stream", 
        "Index was out of range", "Cannot read that as a ZipFile", "Not a Microsoft PowerPoint 2007 presentation",
        "Index was outside the bounds of the array", "An attempt was made to move the position before the beginning of the stream",
        );

        $invalid = 0;
        foreach ($validate as $key => $value) {
            $pos = strpos($string, $value);

            if ($pos === 1)
            {
                $invalid = 1;
            }
        }

        if($invalid == 1)
           return $string;
        else
           return "";
    }

请为我提供解决方案,因为此错误严重阻碍了我的工作。

4

0 回答 0