generating and redirecting to pdfs

View: New views
11 Messages — Rating Filter:   Alert me  

generating and redirecting to pdfs

by Steve Rippl-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I'm using TT for my View templates, and I'm experimenting with
Template::Plugin::Latex for generating pdf reports.  Now I'm generating
pdfs (which is nice!) but not redirecting to them at the end, so I
suspect I'm using the wrong approach.

Right now I've added OUTPUT_PATH => 'root/tmp' to lib/WsdSis/View/TT.pm
and when I hit the /report/test it using the following template (without
a wrapper)

[%
  META no_wrapper = 1;
  META title = 'Latex';
-%]
[% USE Latex(output='example.pdf') -%]
[% FILTER latex %]
\documentclass[letterpaper]{article}
\begin{document}
...LaTeX document...
\end{document}
[% END -%]

which produces root/tmp/example.pdf but leaves me looking at a blank
page on the screen.  I realize my view is supposed to provide the final
view and I've redirected that way to the pdf file, is there a quick way
to redirect to that or do I need a different approach (similar to the
Catalyst::View::Email::Template example in the book?).

Any pointers would be much appreciated...

Thanks,
Steve


--
Steve Rippl
Technology Director
Woodland Public Schools
360 225 9451 x326

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: generating and redirecting to pdfs

by Jason Galea :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Steve,

not sure if you can gleen anything from this but I recently set myself up to produce PDFs from basic HTML produced by TT.
I created a view that processes a TT template then converts to PDF using HTML::Doc and shoves it as a string straight to $c->response->body.

I'm pretty happy with the results and ease of use.. (automatically handles multi page output amoung other things)

#============================================
package Lecstor7::View::PDF;

use base qw/Catalyst::View/;

use HTML::HTMLDoc;

__PACKAGE__->config->{template_extension} = '.tt';
__PACKAGE__->config->{tmp_dir} = '/tmp';
__PACKAGE__->config->{page_size} = 'a4';
__PACKAGE__->config->{header} = ['t', '.', '/'];
__PACKAGE__->config->{footer} = ['t', '.', '/'];

sub process {
  my ( $self, $c ) = @_;

  my $template = $c->stash->{template}
    ||  $c->action . $self->config->{template_extension};

  unless (defined $template) {
    $c->log->debug('No template specified for rendering') if $c->debug;
    return 0;
  }

  my $output = $c->view('TT')->render($c, $template);

  if (UNIVERSAL::isa($output, 'Template::Exception')) {
    my $error = qq/Couldn't render template "$output"/;
    $c->log->error($error);
    $c->error($error);
    return 0;
  }

  my $doc = HTML::HTMLDoc->new( 'mode' => 'file', 'tmpdir' => $self->config->{tmp_dir} );
  $doc->set_page_size($self->config->{page_size});
  $doc->set_header(@{$self->config->{header}});
  $doc->set_footer(@{$self->config->{footer}});
  $doc->set_html_content($output);

  my $pdf = $doc->generate_pdf();

  unless ( $c->response->content_type ) {
    $c->response->content_type('text/pdf; charset=utf-8');
  }

  $c->res->header( 'Content-Disposition' => 'attachment;filename='.$c->stash->{pdf_filename} );

  $c->response->body($pdf->to_string());

  return 1;
}

1;
#============================================

in my Controller I can then do.. (simplified version of my actual controller)

sub packing_slip : Local{
  my ($self, $c) = @_;
  $c->stash(
    pdf_filename => 'packingslip.pdf'
  );
  $c->forward( $c->view('PDF') );
}

cheers,

J

On Wed, Oct 21, 2009 at 8:25 AM, Steve Rippl <rippls@...> wrote:
Hi,

I'm using TT for my View templates, and I'm experimenting with
Template::Plugin::Latex for generating pdf reports.  Now I'm generating
pdfs (which is nice!) but not redirecting to them at the end, so I
suspect I'm using the wrong approach.

Right now I've added OUTPUT_PATH => 'root/tmp' to lib/WsdSis/View/TT.pm
and when I hit the /report/test it using the following template (without
a wrapper)

[%
 META no_wrapper = 1;
 META title = 'Latex';
-%]
[% USE Latex(output='example.pdf') -%]
[% FILTER latex %]
\documentclass[letterpaper]{article}
\begin{document}
...LaTeX document...
\end{document}
[% END -%]

which produces root/tmp/example.pdf but leaves me looking at a blank
page on the screen.  I realize my view is supposed to provide the final
view and I've redirected that way to the pdf file, is there a quick way
to redirect to that or do I need a different approach (similar to the
Catalyst::View::Email::Template example in the book?).

Any pointers would be much appreciated...

Thanks,
Steve


--
Steve Rippl
Technology Director
Woodland Public Schools
360 225 9451 x326

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/



--
Jason Galea
Web Developer

Ph 07 40556926
Mob 04 12345 534
www.eightdegrees.com.au


_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: generating and redirecting to pdfs

by Moritz Onken :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Am 21.10.2009 um 00:25 schrieb Steve Rippl:

> Hi,
>
> I'm using TT for my View templates, and I'm experimenting with
> Template::Plugin::Latex for generating pdf reports.  Now I'm  
> generating
> pdfs (which is nice!) but not redirecting to them at the end, so I
> suspect I'm using the wrong approach.

I'm doing the exact same thing, but without creating a temporary file:

my $template = "[% TT LATEX STUFF %]";
my $t = Template::Alloy->new;
my $out;
$t->process(\$template, {stash => 'bar'}, \$out) || die $@;
my $output = IO::String->new($out);
my $string = $output->string_ref;
$c->response->content_type('application/pdf');
$c->response->content_length( length $$string );
$c->res->body($$string);

You can probably skip the IO::String object...

That's about it.

cheers,
moritz

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: generating and redirecting to pdfs

by Steve Rippl-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, 2009-10-21 at 09:42 +1000, Jason Galea wrote:

>   my $doc = HTML::HTMLDoc->new( 'mode' => 'file', 'tmpdir' =>
> $self->config->{tmp_dir} );
>   $doc->set_page_size($self->config->{page_size});
>   $doc->set_header(@{$self->config->{header}});
>   $doc->set_footer(@{$self->config->{footer}});
>   $doc->set_html_content($output);
>
>   my $pdf = $doc->generate_pdf();
>
>   unless ( $c->response->content_type ) {
>     $c->response->content_type('text/pdf; charset=utf-8');
>   }
>
>   $c->res->header( 'Content-Disposition' =>
> 'attachment;filename='.$c->stash->{pdf_filename} );
>
>   $c->response->body($pdf->to_string());
>
>   return 1;
> }

Thanks for this!  I did get this working, the only change I seemed to
have to make was  

$c->response->content_type('application/pdf');

In order to get my browser to open it using a pdf reader (with
'text/pdf' it was using a text editor).

Thanks again!

Steve





_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: generating and redirecting to pdfs

by Jason Galea :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Sat, Oct 24, 2009 at 12:23 AM, Steve Rippl <rippls@...> wrote:

Thanks for this!  I did get this working, the only change I seemed to
have to make was

$c->response->content_type('application/pdf');

In order to get my browser to open it using a pdf reader (with
'text/pdf' it was using a text editor).


cool. ah, I did see/wonder about that. I've changed my code now. 'text/pdf' worked for me in my Lenny desktop but I had done any other testing..

thanks,

J

--
Jason Galea
Web Developer

Ph 07 40556926
Mob 04 12345 534
www.eightdegrees.com.au


_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: generating and redirecting to pdfs

by Aristotle Pagaltzis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

* Jason Galea <lists@...> [2009-10-21 01:50]:
>   $c->res->header( 'Content-Disposition' => 'attachment;filename='.$c->stash->{pdf_filename} );

This will break for filenames with spaces in them. For strict
correctness, you want this:

    ( my $pdf_filename = $c->stash->{ pdf_filename } ) =~ s!"!\\"!g;
    $c->res->header( 'Content-Disposition' => qq(attachment; filename="$pdf_filename") );

--
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1}
&Just->another->Perl->hack;
#Aristotle Pagaltzis // <http://plasmasturm.org/>

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Re: generating and redirecting to pdfs

by Jason Galea :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Sun, Oct 25, 2009 at 12:49 AM, Aristotle Pagaltzis <pagaltzis@...> wrote:
* Jason Galea <lists@...> [2009-10-21 01:50]:
>   $c->res->header( 'Content-Disposition' => 'attachment;filename='.$c->stash->{pdf_filename} );

This will break for filenames with spaces in them. For strict
correctness, you want this:

   ( my $pdf_filename = $c->stash->{ pdf_filename } ) =~ s!"!\\"!g;
   $c->res->header( 'Content-Disposition' => qq(attachment; filename="$pdf_filename") );

hmm.. I'm missing something here.. won't that simply escape double quotes and not affect spaces?


--
Jason Galea
Web Developer

Ph 07 40556926
Mob 04 12345 534
www.eightdegrees.com.au


_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: generating and redirecting to pdfs

by hkclark :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Oct 20, 2009 at 6:25 PM, Steve Rippl <rippls@...> wrote:
Hi,

I'm using TT for my View templates, and I'm experimenting with
Template::Plugin::Latex for generating pdf reports.  Now I'm generating
pdfs (which is nice!) but not redirecting to them at the end, so I
suspect I'm using the wrong approach.

Right now I've added OUTPUT_PATH => 'root/tmp' to lib/WsdSis/View/TT.pm
and when I hit the /report/test it using the following template (without
a wrapper)

[%
 META no_wrapper = 1;
 META title = 'Latex';
-%]
[% USE Latex(output='example.pdf') -%]
[% FILTER latex %]
\documentclass[letterpaper]{article}
\begin{document}
...LaTeX document...
\end{document}
[% END -%]

which produces root/tmp/example.pdf but leaves me looking at a blank
page on the screen.  I realize my view is supposed to provide the final
view and I've redirected that way to the pdf file, is there a quick way
to redirect to that or do I need a different approach (similar to the
Catalyst::View::Email::Template example in the book?).

Any pointers would be much appreciated...

Thanks,
Steve


--
Steve Rippl
Technology Director
Woodland Public Schools
360 225 9451 x326


Hi Steve,

It sounds like you probably want to stick with something that's specific to LaTeX, so this may be of no help for you, but I thought I would pass it along just in case.  I have recently used Jon Allen's Catalyst::View::PDF::Reuse and found it to be excellent.  It's great for situations where you want to have a fancy "background PDF" that you then want to superimpose text on top of.

Jon also has a nice presentation on the package at:
http://www.pennysarcade.co.uk/files/Creating_PDF_files_with_Catalyst.pdf

Regards,
Kennedy
 

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: generating and redirecting to pdfs

by Steve Rippl-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, 2009-10-26 at 18:49 -0400, hkclark@... wrote:


> Hi Steve,
>
> It sounds like you probably want to stick with something that's
> specific to LaTeX, so this may be of no help for you, but I thought I
> would pass it along just in case.  I have recently used Jon Allen's
> Catalyst::View::PDF::Reuse and found it to be excellent.  It's great
> for situations where you want to have a fancy "background PDF" that
> you then want to superimpose text on top of.
>
> Jon also has a nice presentation on the package at:
> http://www.pennysarcade.co.uk/files/Creating_PDF_files_with_Catalyst.pdf
>
> Regards,
> Kennedy

Thanks for the input.  I came across Catalyst::View::PDF::API2 but couldn't
get that to work for some reason.  I hadn't tried ..PDF::Reuse but right now
HTML::HTMLDoc is working pretty well for me.

Thanks as well to Moritz for his insight...

Steve Rippl

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: generating and redirecting to pdfs

by Aristotle Pagaltzis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

* Jason Galea <lists@...> [2009-10-26 23:45]:
> On Sun, Oct 25, 2009 at 12:49 AM, Aristotle Pagaltzis <pagaltzis@...>wrote:
> > * Jason Galea <lists@...> [2009-10-21 01:50]:
> > >   $c->res->header( 'Content-Disposition' => 'attachment;filename='.$c->stash->{pdf_filename} );
> >
> > This will break for filenames with spaces in them. For strict
> > correctness, you want this:
> >
> >    ( my $pdf_filename = $c->stash->{ pdf_filename } ) =~ s!"!\\"!g;
> >    $c->res->header( 'Content-Disposition' => qq(attachment; filename="$pdf_filename") );
                                                                         ^             ^
                                                                         ^             ^

> hmm.. I'm missing something here...

Yup.

> won't that simply escape double quotes and not affect spaces?

No.

:-)

Regards,
--
Aristotle Pagaltzis // <http://plasmasturm.org/>

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: generating and redirecting to pdfs [OT]

by Jolly.Tall :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Steve Rippl wrote:
> On Mon, 2009-10-26 at 18:49 -0400, hkclark@... wrote:
[..]
> Thanks for the input.  I came across Catalyst::View::PDF::API2 but couldn't
> get that to work for some reason.  I hadn't tried ..PDF::Reuse but right now
> HTML::HTMLDoc is working pretty well for me.

Just as an aside, I've been using HTMLDOC server-side for years and have
only just noticed HTML::HTMLDoc. It's a neat wrapper but I haven't found
a way of setting header & footer size (equivalent of --headfootsize
option), except to directly access the objects private parts:

$htmldoc->_set_doc_config('headfootsize', 9)

which breaks all the conventions, but works. I notice the H::H package
hasn't been updated since 2005 so I guess it's safe enough to do this.
Anyone encountered this issue before?
--
Richard Jones

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/