Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Problem converting HTML to PDF

by ehdonhon (Curate)
on Jan 29, 2003 at 21:05 UTC ( #231091=perlquestion: print w/ replies, xml ) Need Help??
ehdonhon has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks,

I'm having some difficulty with the following chunk of code, from a cgi which is intended to take whatever my web page looks like at the moment, convert it to a pdf and serve that pdf to the client:

use Image::Magick; my $html2ps = '/usr/local/bin/html2ps'; print "Content-type: application/pdf\n\n"; open ( FIN, "$html2ps http://mypage.com/index?param=x&foo=y' |" ); my $image = Image::Magick->new; # I don't think binmode is necessary in ps or pdf, but # I'm adding it just in case it makes a difference. binmode FIN; my $status = $image->Read(file=>\*FIN); warn( "Read status = $status" ); $image->set(format=>'pdf'); binmode STDOUT; $image->set(file=>\*STDOUT); $status = $image->Write(); warn( "Write status = $status" ); $image->set(file=>\*STDERR); $status = $image->Write();

From my debugging, I know that Image::Magick does actually read in the postscript file correctly. I also know that the $status value returned by both Read() and Write() is empty (I think that means success). Also I know that the second Write() call which outputs to STDERR does output a valid pdf file.

My problem, is that the first Write() call which should be sending output to STDOUT isn't doing anything at all! In fact, here is all the output that the web server returns from this cgi:

HTTP/1.1 200 OK
Connection: close
Date: Wed, 29 Jan 2003 20:50:01 GMT
Server: Apache/1.3.26 (Unix) mod_perl/1.27 mod_ssl/2.8.9 OpenSSL/0.9.6e
Content-Type: application/pdf
Client-Date: Wed, 29 Jan 2003 20:50:48 GMT
Client-Peer: 1.2.3.4:80

All its returning back are the headers. No output from the Write(). Can anybody tell me what I'm doing wrong?

Comment on Problem converting HTML to PDF
Download Code
Re: Problem converting HTML to PDF
by Cody Pendant (Prior) on Jan 29, 2003 at 21:33 UTC
    I know nothing about HTML to PDF conversion, but what's with the quotes here:
    open ( FIN, "$html2ps http://mypage.com/index?param=x&foo=y' |" );
    ?

    Plus, as any monk will tell you, you're not testing that open with an "or die"...
    --

    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D

      what's with the quotes...

      Typo from copying the code over to my browser. Should be:  open ( FIN, "$html2ps 'http://mypage.com/index?param=x&foo=y' |" );

      you're not testing that open with an "or die"...

      You are right, I should do that. But, as I said in my original post, I know from debugging that I'm getting past this point correctly.

      Thanks.

Re: Problem converting HTML to PDF
by stephen (Priest) on Jan 29, 2003 at 21:56 UTC

    A couple of points:

    First, what's happening is that your program is dying before it gets the chance to produce any output. In order to see the error message, you need to put

    use CGI::Carp qw(fatalsToBrowser);
    at the top of your script. That way you'll see the error messages. When I try to run your script on my site, I get various errors from html2ps and Image::Magick. Can you run your script from the command line? If so, and it runs correctly, you've probably got a permissions issue.

    Secondly, the 'binmode' line is unnecessary if you're on UNIX. Both the file structure you're using and the HTTP header tell me you're on UNIX, so I'd remove that.

    stephen

      If it's dying, then why is the second call to Write() producing correct output? I would think if it died before that, the second call to Write() would never happen.

      I have tested both with binmode and without binmode. I was pretty certain that binmode didn't do anything in UNIX.

        >I also know that the $status value returned by both
        >Read() and Write() is empty (I think that means success).

        If you only think that, I really think you should check the documentation before you go any further.

        Everything that you call, check its return. Everything you do, print a line to see if it worked or died there.

        There's stuff you say you "know from debugging" -- why don't you post that stuff here?
        --

        “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
        M-J D
Re: Problem converting HTML to PDF
by Dr. Mu (Hermit) on Jan 30, 2003 at 03:54 UTC
    You need to capitalize the method name "set" to "Set".

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://231091]
Approved by Mr. Muskrat
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2014-12-29 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls