root/trunk/plagger/lib/Plagger/Plugin/Publish/PDF.pm

Revision 163 (checked in by miyagawa, 15 years ago)

Add Publish::PDF stub. It uses Audrey's PDF::FromHTML module to create PDF files, but there should be easier way to do that on Win32 / Mac OSX platform.

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin::Publish::PDF;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use File::Spec;
6 use PDF::FromHTML;
7
8 sub register {
9     my($self, $context) = @_;
10     $context->register_hook(
11         $self,
12         'publish.feed' => \&feed,
13     );
14 }
15
16 sub feed {
17     my($self, $context, $args) = @_;
18
19     my $dir = $self->conf->{dir};
20     unless (-e $dir && -d _) {
21         mkdir $dir, 0755 or $context->error("mkdir $dir: $!");
22     }
23
24     my $file = File::Spec->catfile($dir, $args->{feed}->id . ".pdf");
25     my $body = $self->templatize($context, $args);
26     utf8::encode($body);
27
28     $context->log(info => "Writing PDF to $file");
29
30     my $pdf = PDF::FromHTML->new;
31     $pdf->load_file(\$body);
32     $pdf->convert();
33     $pdf->write_file($file);
34 }
35
36 sub templatize {
37     my($self, $context, $args) = @_;
38
39     # HACK: share the template with Gmail ... for now
40     my $tt = $context->template();
41     $tt->process('gmail_notify.tt', $args, \my $out)
42         or $context->error($tt->error);
43
44     $out;
45 }
46
47 1;
48
49 __END__
50
51 =head1 NAME
52
53 Plagger::Plugin::Publish::PDF - Publish feeds as PDF
54
55 =head1 SYNOPSIS
56
57   - module: Publish::PDF
58     config:
59       dir: /var/web/pdfs
60
61 =head1 DESCRIPTION
62
63 This plugin creates PDF files which you can be view and print with
64 Adobe Reader.
65
66 =head1 AUTHOR
67
68 Tatsuhiko Miyagawa
69
70 =head1 SEE ALSO
71
72 L<Plagger>, L<PDF::FromHTML>
73
74 =cut
Note: See TracBrowser for help on using the browser.