You are viewing a plain text version of this content. The canonical link for it is here.
Posted to embperl@perl.apache.org by Michael Stepanov <mi...@francoudi.com> on 2005/01/31 14:40:16 UTC

Re: pdf generation

Ben Kim wrote:
> Dear list,
> 
> 1. pdf generation 
> I'd like to learn what people use to generate pdf files. I want to
> create pdf from an epl page, with all data from the database.

We use Embperl and htmldoc to build PDF files. A fuctionality is 
implemented into Perl module. There is a possibility to send built PDF
file in the browser plugin:

use Embperl;
use mod_perl;
use File::Temp qw/ tempfile /;

my $r = $Embperl::req_rec;

my $body;
Embperl::Execute( { inputfile   => <your/Embperl/template>,
                     param       => [ <some parameters> ],
                     output      => \$html } );

my($fh, $fname) = tempfile('file_XXXXXXXX', SUFFIX => '.pdf', DIR => 
yout/tmp/dir);;

open(WRITE, "|htmldoc -t pdf14 -f $fname $msg --fontsize 10  --header 
... --footer t/D --webpage --size A4 -") or die "Couldn't open htmldoc: 
$! $?";
print WRITE $$html;
close WRITE or warn "Some error was generated in the pipe. Error :$! ";

# Send PDF file into browser plugin.
if($mod_perl::VERSION >= 1.99) {
     $r->content_type('application/pdf');
     $r->sendfile($fname);
} else {
     eval "use Apache::File;";
     open $fh, $fname or die "Cannot open file $fname: $!!";
     # Send created file to HTTP response
     $r->headers_out->set('Accept-Ranges' => 'bytes');
     $r->content_type('application/pdf');
     $r->headers_out->set('Content-Disposition', 'inline; 
filename=report.pdf');
     $r->headers_out->set('Connection' => 'close');
     $r->set_content_length(-s $fname);
     $r->set_etag();
     $range_req = $r->set_byterange();
     $r->send_http_header;
     if( $range_req ) {
         while( my($offset, $length)=$r->each_byterange) {
             seek $fh, $offset, 0;
             $r->send_fd($fh, $length)
         }
     } else {
         $r->send_fd($fh);
     }
}

To produce complicated PDF documents we use a following approach:
1) Make document template with some markers in text editor (MS Word, for 
example);
2) Convert the template into PostScript (just print it to the file using 
PostScript printer);
3) Replace markers by real data using Perl regexp;
4) Convert PostScript file to PDF using utility ps2pdf.

> 
> 2. Teeing the output to browser / filesystem
> 
> Related with this, I'd also like to know whether there's a way to print
> the html results of the database-driven epl page into a static html file.

If you need to save Embperl output to the file do it using some 
temporary directory where apache can right to write.

> 
> The problem I have is that the epl pages are protected, and I don't want
> to mess with session id and other auth tokens when I use htmldoc. So it
> will have to output the result sent to the browser  also to the filesystem.
> (Tee'd.)

Hope it will be useful for you.

> 
> 
> Regards,
> 
> Ben Kim
> Systems Administrator/Database Developer
> College of Education 
> Texas A&M University
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
> For additional commands, e-mail: embperl-help@perl.apache.org
> 
> 


-- 
Best regards,
Michael Stepanov
Perl/Linux Developer
www.stepanoff.org

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: pdf generation

Posted by Ben Kim <bk...@coe.tamu.edu>.
Many thanks. It'd be helpful. 

Regards,
Ben Kim

On Mon, 31 Jan 2005, Michael Stepanov wrote:

> Ben Kim wrote:
> > Dear list,
> > 
> > 1. pdf generation 
> > I'd like to learn what people use to generate pdf files. I want to
> > create pdf from an epl page, with all data from the database.
> 
> We use Embperl and htmldoc to build PDF files. A fuctionality is 
> implemented into Perl module. There is a possibility to send built PDF
> file in the browser plugin:
> 
> use Embperl;
> use mod_perl;
> use File::Temp qw/ tempfile /;
> 
> my $r = $Embperl::req_rec;
> 
> my $body;
> Embperl::Execute( { inputfile   => <your/Embperl/template>,
>                      param       => [ <some parameters> ],
>                      output      => \$html } );
> 
> my($fh, $fname) = tempfile('file_XXXXXXXX', SUFFIX => '.pdf', DIR => 
> yout/tmp/dir);;
> 
> open(WRITE, "|htmldoc -t pdf14 -f $fname $msg --fontsize 10  --header 
> ... --footer t/D --webpage --size A4 -") or die "Couldn't open htmldoc: 
> $! $?";
> print WRITE $$html;
> close WRITE or warn "Some error was generated in the pipe. Error :$! ";
> 
> # Send PDF file into browser plugin.
> if($mod_perl::VERSION >= 1.99) {
>      $r->content_type('application/pdf');
>      $r->sendfile($fname);
> } else {
>      eval "use Apache::File;";
>      open $fh, $fname or die "Cannot open file $fname: $!!";
>      # Send created file to HTTP response
>      $r->headers_out->set('Accept-Ranges' => 'bytes');
>      $r->content_type('application/pdf');
>      $r->headers_out->set('Content-Disposition', 'inline; 
> filename=report.pdf');
>      $r->headers_out->set('Connection' => 'close');
>      $r->set_content_length(-s $fname);
>      $r->set_etag();
>      $range_req = $r->set_byterange();
>      $r->send_http_header;
>      if( $range_req ) {
>          while( my($offset, $length)=$r->each_byterange) {
>              seek $fh, $offset, 0;
>              $r->send_fd($fh, $length)
>          }
>      } else {
>          $r->send_fd($fh);
>      }
> }
> 
> To produce complicated PDF documents we use a following approach:
> 1) Make document template with some markers in text editor (MS Word, for 
> example);
> 2) Convert the template into PostScript (just print it to the file using 
> PostScript printer);
> 3) Replace markers by real data using Perl regexp;
> 4) Convert PostScript file to PDF using utility ps2pdf.
> 
> > 
> > 2. Teeing the output to browser / filesystem
> > 
> > Related with this, I'd also like to know whether there's a way to print
> > the html results of the database-driven epl page into a static html file.
> 
> If you need to save Embperl output to the file do it using some 
> temporary directory where apache can right to write.
> 
> > 
> > The problem I have is that the epl pages are protected, and I don't want
> > to mess with session id and other auth tokens when I use htmldoc. So it
> > will have to output the result sent to the browser  also to the filesystem.
> > (Tee'd.)
> 
> Hope it will be useful for you.
> 
> > 
> > 
> > Regards,
> > 
> > Ben Kim
> > Systems Administrator/Database Developer
> > College of Education 
> > Texas A&M University
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
> > For additional commands, e-mail: embperl-help@perl.apache.org
> > 
> > 
> 
> 
> -- 
> Best regards,
> Michael Stepanov
> Perl/Linux Developer
> www.stepanoff.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
> For additional commands, e-mail: embperl-help@perl.apache.org
> 
> 

Regards,

Ben Kim
Database Developer/Systems Administrator
434E Harrington Tower / College of Education 
Texas A&M University


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org