Jump to content

Convert documents to pdf


LearningKid

Recommended Posts

Hi I have this code tht helps to create pdf's out of docs.

PROBLEM is tht it only works on .xls files if i try any other file it only makes a blank pdf page. im assumin its the coding and not the way i set up the printer since it works wit .xls files, (although chances of me being worng are very high since 1st time im doin this kinda stuff.)

I will Accept any help please.

<?php
  //This script runs only with a ghostscript installation and an installed postscript printer.
  //copy the script into the Apache htdocs-Directory and edit the lines 9 and 10 below.

  //If it don`t runs as expected please comment out the lines 173, 187, 188, 191, 192 which
  //deletes the postscriptfiles and uploaded files after conversion process. Then you can see whether
  //the upload was successfull or the postscriptfiles will be generated from the script

  //This Script is for free use and copy. For updates, information and help see http://www.goermezer.de
  // --> http://www.goermezer.de
  //Please only edit the next 2 variables !
  //Path to Ghostscript Executable
  $gsinst="C:\gs\gs8.63\bin\gswin32c.exe";
  //Postscript Printername (you can download an excellent Postscript Driver Archive from http://www.goermezer.de
  $printername="GS PS Printer";

  //Supported File Formats. There are more formats supported from Office. If you know them, edit the next lines...
  $quality = $_POST['quality'];
  $excelfiles = array ("xls", "XLS", "xlt", "XLT", "csv", "CSV");
  $powerpointfiles = array("ppt", "PPT", "pot", "POT");
  $wordfiles = array("doc", "DOC", "dot", "DOT", "rtf", "RTF", "tml", "TML", "htm", "HTM", "txt", "TXT");
  $postscriptfiles = array(".ps", ".PS", "prn", "PRN", "eps", "EPS");

  //f you are not familiar with PHP, do nothing else from here !
  error_reporting(E_ALL ^ E_NOTICE);
  $servername = $HTTP_SERVER_VARS['localhost'];
  $workdir = getcwd()."\\print"; //erzeugt C:\\Apache2\\htdocs\\
  $psfile=$workdir.$_FILES['userfile']['name'].".ps";
  $pdffile=$workdir.$_FILES['userfile']['name'].".pdf";
  $uploaded_doc=$workdir.$_FILES['userfile']['name'];
?>
<html><head><title>PDF Document Converter</title></head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF">
<table width="100%" border="0">
  <tr>
    <td width="94%"><b>Office2PDF Conversion Service</b></td>
  </tr>
  <tr>
    <td>
      <? echo "Supported File Endings: <br>" ?>
      <? foreach ($wordfiles as $value) {
          echo "$value, ";
          }
          foreach ($excelfiles as $value) {
          echo "$value, ";
          }
          foreach ($powerpointfiles as $value) {
          echo "$value, ";
          }
          foreach ($postscriptfiles as $value) {
          echo "$value, ";
          }
        ?>
    </td>
  </tr>
</table>
<form enctype="multipart/form-data" action="<?=$PHP_SELF ?>" method="post">
<table width="100%" border="0" bgcolor="#CCCCCC">
  <tr>
    <td width="17%">Office File: </td>
    <td width="83%"><input name="userfile" type="file"></td>
  </tr>
  <tr>
    <td>Quality: </td>
    <td><table width="800">
      <tr>
        <td><label>
          <input type="radio" checked name="quality" value="-dPDFSETTINGS=/default">
          Default: Standard setting, middle sized PDFs, Acrobat 4 compatible</label>
        </td>
      </tr>
      <tr>
        <td><label>
          <input type="radio" name="quality" value="-dPDFSETTINGS=/screen">
          Screen: small PDFs, especially for Internet, Acrobat 3 compatible</label>
        </td>
      </tr>
      <tr>
        <td><label>
          <input type="radio" name="quality" value="-dPDFSETTINGS=/ebook">
          EBook: PDFs especially for e-Books (= middle Quality), Acrobat 4 compatible</label>
        </td>
      </tr>
      <tr>
        <td><label>
            <input type="radio" name="quality" value="-dPDFSETTINGS=/printer">
            Printer: PDFs especially for Printing Documents (= good Quality), Acrobat 4 compatible</label>
          </td>
      </tr>
      <tr>
        <td><label>
          <input type="radio" name="quality" value="-dPDFSETTINGS=/prepress">
          Prepress: PDFs especially for die Printing (= best Quality). Acrobat 4 compatible</label>
        </td>
      </tr>
    </table>
    </td>
    </tr>
  <tr>
    <td>Start conversion</td>
    <td><input type="submit" name="send" value="send file">
    </td>
  </tr>
</table>
</form>
<?php
//If Document has Password, you can use this variable (works not with Powerpoint
if ($_POST['password']){
  $password=$_POST['password'];
  }
  else { $password="False"; }
list ($name, $suffix) = split ('[.]', $_FILES['userfile']['name']);
function excel($document, $ps_file, $pdf_file, $gs_inst, $printer_name, $out_quality){
  $excel = new COM("excel.application") or die("Unable to instantiate Excel");
  $excel->AskToUpdateLinks = 0;
  $excel->Workbooks->Open($document) or die("Unable to open document");
  $excel->Workbooks[1]->Saved=1;
  $excel->Workbooks[1]->PrintOut(1, 5000, 1, False, $printer_name, True, False, $ps_file);
  $excel->Workbooks[1]->Close(false);
  $excel->Quit();
  $excel = null;
  unset($excel);
  while (exec("$gs_inst -sDEVICE=pdfwrite -r300 $out_quality -dNOPAUSE -dBATCH -dSAFER -sPAPERSIZE=a4 -sOutputFile=\"".$pdf_file."\" \"".$ps_file."\"") > 0){
    sleep(1);
    }
  }
function powerpoint($document, $ps_file, $pdf_file, $gs_inst, $printer_name, $out_quality){
  $powerpoint = new COM("powerpoint.application") or die("Unable to instantiate Powerpoint");
  $powerpoint->Visible = 1;
  $powerpoint->Presentations->Open($document, False, False, False) or die("Unable to open document");
  $powerpoint->Presentations[1]->Saved=1;
  //$powerpoint->ActivePrinter = $printer_name;
  $powerpoint->Presentations[1]->PrintOut(1, 5000, $ps_file, 0, False);
  $powerpoint->Presentations[1]->Close();
  $powerpoint->Quit();
  $powerpoint = null;
  //unset($powerpoint);
  while (exec("$gs_inst -sDEVICE=pdfwrite -r300 ".$out_quality." -dNOPAUSE -dBATCH -dSAFER -sPAPERSIZE=a4 -sOutputFile=\"".$pdf_file."\" \"".$ps_file."\"") > 0) {
    sleep(1);
    }
  }
function word($document, $ps_file, $pdf_file, $passwd, $gs_inst, $printer_name, $out_quality){
  $word = new COM("word.application") or die("Unable to instantiate Word");
  //$word->Visible = 0;
  $word->Documents->Open($document, False, True, False, $passwd) or die("Unable to open document");
  //$word->Documents[1]->Saved = 1;
  //$word->ActivePrinter = $printer_name;
  $word->Documents[1]->PrintOut(True, False, 0, $ps_file);
  while($word->BackgroundPrintingStatus > 0){
    sleep(1);
    }
  $word->Documents[1]->Close(false);
  $word->Quit();
  $word = null;
  unset($word);
  while (exec("$gs_inst -sDEVICE=pdfwrite -r300 $out_quality -dNOPAUSE -dBATCH -dSAFER -sPAPERSIZE=a4 -sOutputFile=\"".$pdf_file."\" \"".$ps_file."\"") > 0){
    sleep(1);
    }
  }
if ($_POST['send']){
    print "<pre>";
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $workdir.$_FILES['userfile']['name'])) {
        if (in_array($suffix, $wordfiles)){
          word($uploaded_doc, $psfile, $pdffile, $password, $gsinst, $printername, $quality);
        }
        elseif (in_array($suffix, $excelfiles)){
          excel($uploaded_doc, $psfile, $pdffile, $gsinst, $printername, $quality);
        }
        elseif (in_array($suffix, $postscriptfiles)){
          while (exec("$gsinst -dBATCH -sDEVICE=pdfwrite -r300 $quality -dNOPAUSE -dBATCH -dSAFER -sPAPERSIZE=a4 -sOutputFile=\"".$pdffile."\" -dNOPAUSE \"$uploaded_doc\"") > 0){
            sleep(1);
            }
        }
        elseif (in_array($suffix, $powerpointfiles)){
          powerpoint($uploaded_doc, $psfile, $pdffile, $gsinst, $printername, $quality);
        } else {
            echo'<strong><font color="#FF0000">File format is not supported!</strong></font><br>';
            unlink($uploaded_doc);//Hochgeladene Datei löschen
            echo'<strong><font color="#FF0000">Erased uploaded file.</strong></font>';
            exit();
            }
    } else {
        echo '<strong><font color="#FF0000">File could not be uploaded!</strong></font>';
        exit();
        }
    while (!(is_writable($pdffile))){   //abfrage ob Datei verfügbar ?!
      sleep(1);
    };
    if (!headers_sent()) {
      $header="http://$servername/".$_FILES['userfile']['name'].".pdf";
      header ("Location: $header");
      unlink($psfile);
      unlink($uploaded_doc);
      exit();
      } else { echo 'The result is for 24 hours see:<a href="http://'.$servername.'/'.$_FILES['userfile']['name'].'.pdf">'.$_FILES['userfile']['name'].'.pdf</a>'; }
    unlink($psfile);//Hochgeladene Datei löschen
    unlink($uploaded_doc);//Hochgeladene Datei löschen
    exit();
}
?>
</BODY>
</HTML>

Link to comment
https://forums.phpfreaks.com/topic/172126-convert-documents-to-pdf/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.