从PHP

时间:2016-12-05 18:16:39

标签: php

我在Windows10环境中运行。我的php脚本写了一个pdf文件,我只想在我指定的pdf查看器中打开该文件。我正在使用" runAsynchronously" PHP手册中给出的功能,我尝试了很多变化。我没有问题让进程在后台运行 - 它每次出现在我的TaskManager进程列表中,但没有出现窗口 - 我做错了什么?如果我双击已写入的链接文件,它可以正常工作。它与可执行文件或文件名的路径无关 - 我可以用" notepad.exe替换pdf查看器"和带有合适文本文件的$文件 - 同样的事情发生,记事本显示为一个进程,但不是一个窗口,链接工作正常。

以下是一些代码段

$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();
        }
    }

1 个答案:

答案 0 :(得分:0)

如果在浏览器中打开pdf或下载它可以解决您的问题,请尝试一个名为PDFMerger的课程:

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

它使用名为setasign FPDI和FPDF(https://packagist.org/packages/setasign/fpdi-fpdf#p-456)的API 我在这里测试过,因为它对我感兴趣并且工作正常。 (Windows 8.1中的Wamp)

我将所有内容(fpdi和fpdf的类和文件夹)放在一个名为pdfmerger的文件夹中,因为我在命名空间方面遇到了一些困难而且我的代码工作正常,但我知道这不是包含类的最佳方式 - 如果您没有找到类的问题,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

如果您使用选项&#39; download&#39;您可以选择将在Windows设置中打开pdf文件的程序(选择默认程序打开pdfs): https://www.cnet.com/how-to/how-to-set-default-programs-in-windows-10/