Advertisement
Twissel

PHP Magic

Jun 6th, 2016
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.60 KB | None | 0 0
  1.  
  2.         <?php
  3.         ini_set('dispay_errors',1);
  4.         require_once('tcpdf_include.php');
  5.        
  6.             function Convertion($proc, $xmlFile, $xslFile){
  7.                 $proc->setSourceFromFile($xmlFile);
  8.                 $proc->compileFromFile($xslFile);
  9.                 $handle = fopen($page_name,"w");
  10.                 fclose($handle);
  11.                 $proc->setOutputFile($page_name);
  12.                 $proc->transformToFile();  
  13.                 $proc->clearParameters();
  14.                 $proc->clearProperties();
  15.             }
  16.            
  17.             function Extract($name,$path) {
  18.             $zip = new ZipArchive;
  19.             if ($zip->open($name) === TRUE) {
  20.                 $zip->extractTo($path);
  21.                 $zip->close();
  22.                 echo 'ok';
  23.                 } else {
  24.                 echo 'something is going wrong!';
  25.                 }
  26.             }
  27.            
  28.             function ProducePDF($pdf_name, $html_page){
  29.              // create new PDF document
  30.             $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  31.  
  32.             // set document information
  33.             $pdf->SetCreator(PDF_CREATOR);
  34.             $pdf->SetAuthor('Tony');
  35.             $pdf->SetTitle('A document');
  36.             $pdf->SetSubject('Information');
  37.             $pdf->SetKeywords('TCPDF, PDF');
  38.  
  39.             // set default header data
  40.             $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' test', PDF_HEADER_STRING);
  41.  
  42.             // set header and footer fonts
  43.             $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  44.             $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  45.  
  46.             // set default monospaced font
  47.             $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  48.  
  49.             // set margins
  50.             $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  51.             $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  52.             $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  53.  
  54.             // set auto page breaks
  55.             $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  56.  
  57.             // set image scale factor
  58.             $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  59.  
  60.             // set some language-dependent strings (optional)
  61.             if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
  62.             require_once(dirname(__FILE__).'/lang/eng.php');
  63.             $pdf->setLanguageArray($l);
  64. }
  65.             // ---------------------------------------------------------
  66.  
  67.             // set font
  68.             $pdf->SetFont('helvetica', '', 14);
  69.  
  70.             // add a page
  71.             $pdf->AddPage();
  72.  
  73.  
  74.             $html = file_get_contents($html_page);
  75.             // output the HTML content
  76.             $pdf->writeHTML($html, true, false, true, false, '');
  77.  
  78.             // reset pointer to the last page
  79.             $pdf->lastPage();
  80.  
  81.             // ---------------------------------------------------------
  82.  
  83.            //Close and output PDF document
  84.            $pdf->Output($pdf_name, 'FI');
  85.  
  86.  
  87.         }
  88.        
  89.         function recursiveRemoveDirectory($pathName) {
  90.             foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
  91.             $path->isDir() && !$path->isLink() ? rmdir($path->getPathname()) : unlink($path->getPathname());
  92. }
  93. rmdir($dirPath);
  94. }      
  95.             $doc_name = "commands.docx";
  96.             $result_name = "result.pdf";
  97.             //$FILES['userfile']['name'];
  98.             $tmp_dir = substr(str_shuffle(MD5(microtime())), 0, 10);
  99.             mkdir($tmp_dir);
  100.             $page_name = $tmp_dir."output.html";
  101.             $upload_path = $tmp_dir.$result_name;
  102.             $xmlFile = $tmp_dir."word/document.xml";
  103.             $xslTable = "xsl/docx2html.xsl";
  104.             $saxonProc = new Saxon\SaxonProcessor();
  105.             $proc = $saxonProc->newXsltProcessor();
  106.             Extract($doc_name, $tmp_dir);
  107.             Convertion($proc, $xmlFile, $xslTable);
  108.             unset($proc);
  109.             unset($saxonproc);
  110.             ProducePDF($upload_path,$page_name);
  111.             recursiveRemoveDirectory($tmp_dir);
  112.         ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement