0

I am running in a Windows10 environment. My php script writes a pdf file and I simply want to have the file open up in the pdf viewer that I specify. I am using the "runAsynchronously" function given in the PHP manual and I have tried many variations. I have no problem getting the process to run in the background - it appears every time in my TaskManager process listing, but no window appears - what am I doing wrong? If I double click the link file that has been written it works fine. It is nothing to do with the path to the executable or the filename - I can replace the pdf viewer with "notepad.exe" and the $file with a suitable text file - the same thing happens, notepad appears as a process, but not as a window, and the link works fine.

Here are some code snippets

$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();
        }
    }
NormB
  • 41
  • 5
  • do you know that php is meant for server side scripts ? IMHO this is program for different language – Jozef Dochan Dec 05 '16 at 18:21
  • 2
    @JozefDochan ["PHP is a popular general-purpose scripting language that is especially suited to web development."](http://www.php.net). "General-purpose" being the key phrase there. – bishop Dec 05 '16 at 18:24
  • Possible duplicate of [php How do I start an external program running - Having trouble with system and exec](http://stackoverflow.com/questions/1403203/php-how-do-i-start-an-external-program-running-having-trouble-with-system-and) – HPierce Dec 05 '16 at 18:41
  • According to this link http://stackoverflow.com/questions/2433973/what-does-the-two-parameters-mean-for-wscript-here you should use 1 when opening a window for the first time, you are specifying windowStyle 7 (minimised) – Darren H Dec 05 '16 at 18:43

1 Answers1

0

If opening in the browser a pdf or downloading it solves your problem, try a class called PDFMerger:

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

It uses an API called setasign FPDI and FPDF (https://packagist.org/packages/setasign/fpdi-fpdf#p-456) I tested it here, because it also interested me and worked ok. (Wamp in Windows 8.1)

I put everything together(classes and folders of fpdi and fpdf) in a folder called pdfmerger because I was having some difficulty with namespaces and my code worked ok, but I know this is not the best way to include the classes - the example in GitHub page must help if you have problems with classes not finding classes):

<?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

If you use the option 'download' you can choose the program that will open the pdf file in your windows settings(Choose default program to open pdfs): https://www.cnet.com/how-to/how-to-set-default-programs-in-windows-10/