G'day Takamoto,

[I created a quick test.pdf, with just the text "Test for PM 11148715", for the tests below. I don't have p2t, but I do have pdftotext, which appears to have the same functionality and accepts the same options.]

You appear to have got bogged down in absolute vs. relative paths and eval code.

If your p2t is in a directory listed in $PATH, you don't technically need a path at all; however, using an absolute path avoids tainting.

If all you want to do is print the PDF text, you can use system() or backticks like one of these:

$ perl -e 'system "pdftotext -nodiag -layout -enc UTF-8 test.pdf -"' Test for PM 11148715 $ perl -e 'print `pdftotext -nodiag -layout -enc UTF-8 test.pdf -`' Test for PM 11148715

If you want something a little more robust, that avoids the overhead of using the shell, consider capturex() from IPC::System::Simple. Here's an example (p2t_capturex.pl):

#!/usr/bin/env perl use strict; use warnings; use IPC::System::Simple 'capturex'; my $p2t_exe = 'C:/cygwin64/bin/pdftotext.exe'; my $pdf_doc = 'test.pdf'; print capturex( $p2t_exe => qw{-nodiag -layout -enc UTF-8}, $pdf_doc, '-' );

You get the same output as before:

$ ./p2t_capturex.pl Test for PM 11148715

In case you were wondering, that's the same pdftotext program throughout. Note the identical inode numbers:

$ ls -i1 `which pdftotext` C:/cygwin64/bin/pdftotext.exe 844424931368301 /usr/bin/pdftotext 844424931368301 C:/cygwin64/bin/pdftotext.exe

— Ken


In reply to Re: use of Backticks to catch console output by kcott
in thread use of Backticks to catch console output by Takamoto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.