Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

PDF::Create - send output to STDOUT?

by George_Sherston (Vicar)
on Jun 04, 2002 at 08:50 UTC ( [id://171430]=perlquestion: print w/replies, xml ) Need Help??

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

I'm using PDF::Create to make on-the-fly pdfs as part of a CGI application. It works fine, but only because I have bodged up a crude alteration to the add function in Create.pm. The original function is:
sub add { my $self = shift; my $data = join '', @_; $self->{'size'} += length $data; if (defined $self->{'fh'}) { my $fh = $self->{'fh'}; print $fh $data; } else { $self->{'data'} .= $data; } }
This does one of two things with any new bit of data: either it prints it to a previously opened file handle, or it stores it in the pdf object for, AFAIK ultimate dumping to... filehandle? (not sure - I'm afraid the close sub is a bit beyond me - it seems to call itself from within itself, though I'm sure there's more to it than that).

In any event, this creates PDF files ok, but I want it to send the PDF data to the web browser. The only way I could find to do that is to install a local copy of the module and alter it to
sub add { #I, George Sherston, edited this function 6/2002; my $self = shift; my $data = join '', @_; $self->{'size'} += length $data; # if (defined $self->{'fh'}) { # my $fh = $self->{'fh'}; # print $fh $data; print $data; # } # else { # $self->{'data'} .= $data; # } }
Now it works (I include a pdf header in my script) - the pdf appears straight on the browser screen as desired, with no intermediate saving and retrieving.

BUT this is obviously a BAD THING (tm) from the point of view of portability and future generations. So my question is whether any monk knows how I can achieve my aim without editing the module. I'd have thought it wd be a thing people wanted to do, so wd probably be possible. But I've tried:
  • setting the 'fh' parameter to 'STDOUT' (gets a can't use string ("STDOUT") as a symbol ref error)
  • setting the 'fh' parameter to '' (similar problem)
  • setting it to undef - d'oh, obviously makes no difference
  • setting the 'filename' parameter to STDOUT - no go.
I think the first of these fails because the filehandle has to be "an already opened filehandle". So maybe the solution lies in making the module think that STDOUT is "already open". But I thought it was the 7/11 of filehandles, and never closed...

I'd be most grateful for any light sibling monks can shed on this.

§ George Sherston

Replies are listed 'Best First'.
Re: PDF::Create - send output to STDOUT?
by shotgunefx (Parson) on Jun 04, 2002 at 09:23 UTC
    I had a similar problem, I just inherited from it and added a method raw_data to give me the completed PDF.

    Also be aware that PDF::Create doesn't escape text which can cause errors when trying to view PDFs created by it.
    package PDF::Create::Raw; use PDF::Create; use 5.006; use strict; use warnings; # Wrapper around PDF::Create that lets us access the raw data. our @ISA = qw(PDF::Create); our $VERSION = '0.02'; # Adds raw_data to grab the PDF data for outputting sub raw_data { my $self = shift; return $self->{'data'}; } 1; __END__ =head1 NAME PDF::Create::Raw - Perl extension for creating PDF files. =head1 DESCRIPTION Same as PDF::Create with one overrided method rawdata. =head1 AUTHOR Pumphret, Lee<lt>E<gt> =head1 SEE ALSO L<perl>. =cut


    -Lee

    "To be civilized is to deny one's nature."
Re: PDF::Create - found a SOLUTION quite by chance!
by George_Sherston (Vicar) on Jun 04, 2002 at 11:57 UTC
    So my computer crashed - this is normal, as it's old and clunky and doesn't cohabit well with my cable modem and runs win98 and I'm too cheap and lazy to get anything else.

    And whilst it reconstructed myself I thought I'd use my time profitably by reading the Camel. Aren't I a diligent monk?

    So what to read? Need to formalise my cargo-cultish understanding of derefencing, so turn to page 251. But for no well explained reason I happen to glance to the left and I see the *foo{THING} syntax... which leads me to the discovery that a filehandle is a THING in the same way an array or a scalar is a THING... well, now, that's interesting sez I, and turns back one page where I read

    References to filehandles or directory handles can be created by referncing the typeglob of the same name

    Well I'll be horn swoggled. Turning to my script, I add a line to the definition of my pdf object:
    my $pdf = new PDF::Create( 'Version' => 1.2, 'Author' => 'me', 'Title' => 'Goom', 'fh' => *STDOUT, # this one );
    ... and now it works, just how I wanted, with the module in its un-butchered format. What were the chances of my blundering across the answer to this question, which has been bothering me for some time, just now?

    § George Sherston
      That didn't even occur to me, I had a different problem than you were having, I wanted to pipe it through a tied GPG filehandle and ran into problems I'm assuming due to the tied filehandle interface so the hack might still come in handy for some.

      One of the things I ended up doing was a rudimentary line oriented PDF printer. (Generates the multiple pages, pagenums, headers and footers automatically). Not clean enough for CPAN yet but if there was some interest in it, I could take the time to polish it.

      -Lee

      "To be civilized is to deny one's nature."
      Glad to know I'm not the only one that reads books backwards :-) One of these days my machines will crash and maybe I could try reading them from front to back...
Re: PDF::Create - send output to STDOUT?
by Aristotle (Chancellor) on Jun 04, 2002 at 12:13 UTC
    Another way is filehandle duplication:open my $stdout_dupe, ">&STDOUT" or die "Failed duplicating STDOUT: $!";(And if it does die you have a serious problem and it ought to.)

    Makeshifts last the longest.

Re: PDF::Create - send output to STDOUT?
by George_Sherston (Vicar) on Jun 04, 2002 at 09:30 UTC
    That's definitely an improvement (which I shall take the liberty of borrowing!) in that it makes it clear there is a non-CPAN module involved, cutting down on confusion. I notice the last version of this very useful module is 1999. I wonder if Fabien Tassin is still maintaining it and cd be persuaded to include this wrinkle in a new release.

    § George Sherston
      I tried but never got a response (Been months.) After a discussion here I'm planning on throwing a patched wrapper on CPAN.

      -Lee

      "To be civilized is to deny one's nature."
Re: PDF::Create - send output to STDOUT?
by Joost (Canon) on Jun 04, 2002 at 15:16 UTC
    All you need to do is send either *STDOUT or \*STDOUT as the fh parameter.

    Also see "Passing Filehandles" in perldoc perlfaq7

    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: PDF::Create - send output to STDOUT?
by jmcnamara (Monsignor) on Jun 04, 2002 at 12:23 UTC

    I'm not in a position to test it for this case but the special Perl filename '-' should cause the output to get redirected to STDOUT and hence to the browser.

    Update: Actually, since you need *STDOUT, rather than STDOUT, '-' won't work.

    --
    John.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://171430]
Approved by IlyaM
Front-paged by Trimbach
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found