2016-12-05 38 views
0

我在Windows10環境中運行。我的php腳本寫了一個pdf文件,我只是想在我指定的pdf查看器中打開文件。我正在使用PHP手冊中給出的「runAsynchronously」函數,並且我嘗試了很多變體。我沒有問題讓進程在後臺運行 - 它每次出現在我的TaskManager進程列表中,但沒有出現窗口 - 我做錯了什麼?如果我雙擊已寫入的鏈接文件,它可以正常工作。這與可執行文件或文件名的路徑無關 - 我可以用「notepad.exe」替換pdf查看器,並用合適的文本文件替換$文件 - 同樣的事情發生,記事本顯示爲一個進程,但不是作爲一個窗口,並且鏈接工作正常。從PHP開始外部程序

下面是一些代碼片段

$cmd = "C:\\Program Files (x86)\\SumatraPDF\\SumatraPDF.exe"; 
runAsynchronously($cmd, $file, 7, null, true); 

function runAsynchronously($path, $arguments, $windowstyle=1, $lnkfile=null, $exec=true) { 
    $tmp = (is_null($lnkfile)) ? 'C:\temp\temp.lnk' : $lnkfile; 
    try { 
     if(file_exists($tmp)) { unlink($tmp); } 
     $WshShell = new COM("WScript.Shell"); 
     $oShellLink = $WshShell->CreateShortcut($tmp); 
     $oShellLink->TargetPath = $path; 
     $oShellLink->Arguments = $arguments; 
     $oShellLink->WorkingDirectory = dirname($path); 
     $oShellLink->WindowStyle = 1; 
     $oShellLink->Save(); 
     $waitforcompletion = false; 
     if($exec) { 
       // Run kicks off the process in the background, but no window gets opened 
       $oExec = $WshShell->Run($tmp, $windowstyle, $waitforcompletion); 
       unlink($tmp); 
      } // if not executed link is left available for manual running 
      unset($WshShell,$oShellLink,$oExec); 
     } catch(Exception $ex) { 
      print $ex->getMessage(); 
     } 
    } 
+0

你知道嗎PHP的意思是服務器端腳本?恕我直言,這是針對不同語言的程序 –

+2

@JozefDochan [「PHP是一種流行的通用腳本語言,特別適合web開發。」](http://www.php.net)。 「通用」是那裏的關鍵詞。 – bishop

+0

可能重複[php如何啓動一個外部程序運行 - 遇到系統和可執行文件](http://stackoverflow.com/questions/1403203/php-how-do-i-start-an-external-program -running-having-trouble-with-system-and) – HPierce

回答

0

如果開盤在瀏覽器中的PDF或下載它解決您的問題,試試名爲PDFMerger類:

https://github.com/clegginabox/pdf-merger

它使用稱爲setasign FPDI和FPDF的API(https://packagist.org/packages/setasign/fpdi-fpdf#p-456) 我在這裏測試了它,因爲它也對我感興趣並且工作正常。 (Wamp in Windows 8.1)

我把所有的東西放在一個名爲PDFmerger的文件夾中(fpdi和fpdf的類和文件夾),因爲我在命名空間中遇到了一些困難,而且我的代碼工作正常,但我知道這不是最好的辦法包括的類 - 在GitHub的頁面的例子一定幫助,如果您有類沒有找到類的問題):

<?php 
include "pdfmerger/fpdf.php"; 
include "pdfmerger/fpdi.php"; 
include 'pdfmerger/PDFMerger.php'; 

$pdf = new PDFMerger(); 

//generate a pdf with the pages 1,2 and 3 and send to browser 
$pdf->addPDF('originalpdfsavedintheserver.pdf', '1,2,3') 
    ->merge('browser', 'nameinbrowser.pdf'); 

// REPLACE 'browser' WITH 'file', 'download', 'string', or 'browser' for output options 

如果使用選項「下載」你可以選擇將打開PDF文件的程序在你的Windows設置(選擇默認程序打開pdf文件): https://www.cnet.com/how-to/how-to-set-default-programs-in-windows-10/