Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(NULL);
- ini_set('display_errors', '0');
- function ParseHeader($header='')
- {
- $resArr = array();
- $headerArr = explode("\n",$header);
- foreach ($headerArr as $key => $value) {
- $tmpArr=explode(": ",$value);
- if (count($tmpArr)<1) continue;
- $resArr = array_merge($resArr, array($tmpArr[0] => count($tmpArr) < 2 ? "" : $tmpArr[1]));
- }
- return $resArr;
- }
- function CallToApi($fileToConvert, $pathToSaveOutputFile, $apiKey, &$message)
- {
- try
- {
- $rand=time();
- $fileName = $rand."MyFile.pdf";
- $postdata = array('OutputFileName' => 'MyFile.pdf', '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;
- }
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>
- Conversion example of Word document to PDF using Rest API
- </title>
- </head>
- <body>
- <div style="width: 600px; margin: auto;">
- <h3>Conversion example of Word document to PDF using Rest API <a href="http://www.convertapi.com" target="_blanks">http://www.convertapi.com</a></h3>
- <form method="post" action="" id="word2pdf_form" enctype="multipart/form-data">
- Please choose Word document(docx or doc file) from your computer
- <br />
- <input type="file" name="FileUpload" id="FileUpload" style="width:600px;">
- <br />
- <br />
- <a href="http://www.convertapi.com/prices" target="_blanks">Api Key(leave empty if you don't have one)</a>
- <br />
- <input name="txtApiKey" type="hidden" id="txtApiKey" value="" style="width:600px;">
- <br />
- <br />
- <div style="text-align: center">
- <input type="submit" name="btnConvert" value="Convert" id="btnConvert">
- </div>
- </form>
- <br>
- <?php
- if (isset($_FILES["FileUpload"]['name'])&&$_FILES['FileUpload']['size']>0&&$_FILES['FileUpload']['error']==0)
- {
- $physicalPath = dirname(__FILE__)."/files/";
- if (!file_exists($physicalPath)) {
- mkdir($physicalPath,0777);
- }
- $uploadedFile = $physicalPath.$_FILES["FileUpload"]['name'];
- $apiKey = $_REQUEST["txtApiKey"];
- if (!move_uploaded_file($_FILES["FileUpload"]["tmp_name"], $uploadedFile)) die("CANNOT MOVE {$_FILES['FileUpload']['name']}" . PHP_EOL);
- $message = "";
- chmod($uploadedFile,0755);
- $result = CallToApi($uploadedFile, $physicalPath, $apiKey, &$message);
- echo "<span id='lblStatus'".($result ? "" : "style='color:red'")." >" .$message."</span>";
- }
- else
- {
- echo in_array("btnConvert", $_REQUEST) ? "<span id='lblStatus' style='color:Red;'>Please select Word document!</span>":"<span id='lblStatus'>Please select a Word document and click Convert button.</span>";
- }
- ?>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement