Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

generating and redirecting to pdfs

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


rippls at woodlandschools

Oct 20, 2009, 3:25 PM

Post #1 of 11 (651 views)
Permalink
generating and redirecting to pdfs

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[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


lists at eightdegrees

Oct 20, 2009, 4:42 PM

Post #2 of 11 (618 views)
Permalink
Re: generating and redirecting to pdfs [In reply to]

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[at]woodlandschools.org>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[at]lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>



--
Jason Galea
Web Developer

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


onken at houseofdesign

Oct 21, 2009, 4:16 AM

Post #3 of 11 (599 views)
Permalink
Re: generating and redirecting to pdfs [In reply to]

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[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


rippls at woodlandschools

Oct 23, 2009, 7:23 AM

Post #4 of 11 (581 views)
Permalink
Re: generating and redirecting to pdfs [In reply to]

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[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


lists at eightdegrees

Oct 23, 2009, 1:09 PM

Post #5 of 11 (577 views)
Permalink
Re: generating and redirecting to pdfs [In reply to]

On Sat, Oct 24, 2009 at 12:23 AM, Steve Rippl <rippls[at]woodlandschools.org>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


pagaltzis at gmx

Oct 24, 2009, 7:49 AM

Post #6 of 11 (558 views)
Permalink
Re: generating and redirecting to pdfs [In reply to]

* Jason Galea <lists[at]eightdegrees.com.au> [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[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


lists at eightdegrees

Oct 26, 2009, 3:41 PM

Post #7 of 11 (516 views)
Permalink
Re: Re: generating and redirecting to pdfs [In reply to]

On Sun, Oct 25, 2009 at 12:49 AM, Aristotle Pagaltzis <pagaltzis[at]gmx.de>wrote:

> * Jason Galea <lists[at]eightdegrees.com.au> [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


hkclark at gmail

Oct 26, 2009, 3:49 PM

Post #8 of 11 (517 views)
Permalink
Re: generating and redirecting to pdfs [In reply to]

On Tue, Oct 20, 2009 at 6:25 PM, Steve Rippl <rippls[at]woodlandschools.org>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


rippls at woodlandschools

Oct 26, 2009, 8:43 PM

Post #9 of 11 (512 views)
Permalink
Re: generating and redirecting to pdfs [In reply to]

On Mon, 2009-10-26 at 18:49 -0400, hkclark[at]gmail.com 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[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


pagaltzis at gmx

Oct 27, 2009, 2:40 AM

Post #10 of 11 (498 views)
Permalink
Re: generating and redirecting to pdfs [In reply to]

* Jason Galea <lists[at]eightdegrees.com.au> [2009-10-26 23:45]:
> On Sun, Oct 25, 2009 at 12:49 AM, Aristotle Pagaltzis <pagaltzis[at]gmx.de>wrote:
> > * Jason Galea <lists[at]eightdegrees.com.au> [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[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


ra.jones at dpw

Oct 27, 2009, 2:48 AM

Post #11 of 11 (499 views)
Permalink
Re: generating and redirecting to pdfs [OT] [In reply to]

Steve Rippl wrote:
> On Mon, 2009-10-26 at 18:49 -0400, hkclark[at]gmail.com 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[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.