perlgrl has asked for the wisdom of the Perl Monks concerning the following question:

I am writing an application using Perl and Tk. The application will show a list of pdfs that are in a directory on the users computer. I need for the user to be able to click on these filenames and have them open in Adobe Acrobat. I have tried using the system() command but it hangs the perl application until the user exits all the way out of Adobe Acrobat. Since there will be a lot of PDFs to view, it would be best if the Adobe Acrobat could stay running and the new pdfs could open in the already running adobe. If there is an easy way to convert the pdfs to jpgs, preferably not using ImageMagik, I could just display those in a new Tk window. Any advice would be greatly appreciated!! Thank you!!!

Replies are listed 'Best First'.
Re: Viewing a PDF file from a Perl program
by tachyon (Chancellor) on Mar 24, 2003 at 03:12 UTC

    Acrobat reader comes with an Active X object that lives in PDF.OCX. You can get all the details on the interface here.

    There are PDF to everything converters. Google for them. Foolabs has some good PDF related GPL tools

    I you have an app that need to view PDF and you want a windowing environment it begs the question what's wrong with a Browser/CGI app - just pop the PDF in a new window. Potentially a lot easier than anything else as all the work has been done for you. Followed by a jpg conversion with creating Active X containers and automating it following up the rear.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Viewing a PDF file from a Perl program
by zengargoyle (Deacon) on Mar 24, 2003 at 03:39 UTC

    you don't give an OS, under Linux, both of these work.

    system "acroread poe.pdf &"; # or (needs error checking) if (fork) { print "bye$/" } else { system acroread => "poe.pdf" }

      ++zengargoyle

      IMHO the best (i.e.: more portable) way to do it is fork, since it works on every UNIX-like OS and it is emulated on Win32, and it won't "freeze" your application. Calling system "acroread poe.pdf &"; won't work on non-UNIX systems

      Ciao!
      --bronto


      The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
      --John M. Dlugosz
        ... but it is buggy for at least 5.6.0 and not ported to everywhere. Also will not work in any way with perl-5.005 on Win32.

        So it is no way more portable.

        Courage, the Cowardly Dog

        indeed fork is the only way to go.

        other replys have suggested:

        system start => acrord32 => $pdf_file; # or system start => $pdf_file;
        neither of which has any hope of running anywhere but Windows.

        for cross-platform-ability have a config setting for the reader executable.

        # in child after fork (close STD* for good measure) close STDIN; close STDOUT; close STDERR; system $cfg{PDFViewer}, $pdf_file;

        i'm sure there are modules on CPAN that abstract Running a program for multiple OSs.

      Also, if you want to make sure that acroread doesn't send anything to STDOUT that might mess up your screen, take a look at Proc::Daemon::Init.

        nope, Proc::Daemon::Init doesn't return to the parent after it is called, so you would still have to fork around it. it does have some nice code for fork-ing and close-ing all open handles so it's good to look at.

      Urmm... what's to stop your child from falling through to the parent code? Usually forked kids have an exit() to prevent this from happening. If you used exec() instead of system(), then you'd have an implicit exit. I think you need an exit. If not, I'm curious why.

      blyman
      setenv EXINIT 'set noai ts=2'

Re: Viewing a PDF file from a Perl program
by Thelonius (Priest) on Mar 24, 2003 at 17:05 UTC
    If you are on Windows and they are using the regulare Acrobat Reader, this will work:
    system qq(start acrord32 "$pdffile");
    . But if they might have full Acrobat instead of the reader, in which case I'm not sure that will work. Or, theoretically, they might have some other reader.

    As long as the filename doesn't have spaces, this will open the file in their configured PDF reader:

    system qq(start $pdffile);
    . Unfortunately there doesn't seem to be a way to do that if the file (or path name) contains spaces (quoting does not help). You could translate it to a DOS short (8.3) name, though.
Re: Viewing a PDF file from a Perl program
by aquarium (Curate) on Mar 25, 2003 at 00:44 UTC
    use fork to run "name_of_pdf.pdf" in background, and default viewer is activated. not sure if this is very portable, but is more so that running "acroread.exe" which is not same on other platforms. if they don't have a pdf viewer it pops up the "open with which program" dialog. chris