| 1 | # $File: //member/autrijus/DBIx-ReportBuilder/lib/DBIx/ReportBuilder/Render/Print.pm $ $Author: autrijus $ | ||
| 2 | # $Revision: #2 $ $Change: 8306 $ $DateTime: 2003/09/29 00:54:37 $ | ||
| 3 | |||
| 4 | package DBIx::ReportBuilder::Render::Print; | ||
| 5 | use base 'DBIx::ReportBuilder::Render'; | ||
| 6 | use strict; | ||
| 7 | use NEXT; | ||
| 8 | |||
| 9 | # XXX - eventually make this the baseclass for PDF, Print and MSWord | ||
| 10 | |||
| 11 | use File::Spec; | ||
| 12 | use File::Temp 'mkdtemp'; | ||
| 13 | use MIME::Base64 'decode_base64'; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my ($class, %args) = @_; | ||
| 17 | my $self = $class->SUPER::new( | ||
| 18 | %args, | ||
| 19 | twig_handlers => { | ||
| 20 | head => \&head, | ||
| 21 | body => sub { $_->set_att( dir => 'ltr' ) }, | ||
| 22 | header => sub { | ||
| 23 | $_->att('hidden') ? $_->delete : do { | ||
| 24 | $_->set_tag('div'); $_->set_att( type => 'header' ); | ||
| 25 | $_->set_att( style => 'border-bottom: 1px black solid' ) | ||
| 26 | if $_->att('separator'); | ||
| 27 | }; | ||
| 28 | }, | ||
| 29 | footer => sub { | ||
| 30 | $_->att('hidden') ? $_->delete : do { | ||
| 31 | $_->set_tag('div'); $_->set_att( type => 'footer' ); | ||
| 32 | $_->set_att( style => 'border-top: 1px black solid' ) | ||
| 33 | if $_->att('separator'); | ||
| 34 | }; | ||
| 35 | }, | ||
| 36 | preamble => sub { $_->att('hidden') ? $_->delete : $_->erase }, | ||
| 37 | content => sub { $_->att('hidden') ? $_->delete : $_->erase }, | ||
| 38 | postamble => sub { $_->att('hidden') ? $_->delete : $_->erase }, | ||
| 39 | var => \&var, | ||
| 40 | %{$args{twig_handlers}||{}}, | ||
| 41 | }, | ||
| 42 | start_tag_handlers => { | ||
| 43 | %{$args{start_tag_handlers}||{}}, | ||
| 44 | }, | ||
| 45 | end_tag_handlers => { | ||
| 46 | %{$args{end_tag_handlers}||{}}, | ||
| 47 | }, | ||
| 48 | twig_roots => { | ||
| 49 | 'html' => 1, | ||
| 50 | %{$args{twig_roots}||{}}, | ||
| 51 | }, | ||
| 52 | pretty_print => ($args{pretty_print} || 'indented_c'), | ||
| 53 | ); | ||
| 54 | |||
| 55 | return $self; | ||
| 56 | } | ||
| 57 | |||
| 58 | # XXX do a temporary juxtaposision | ||
| 59 | sub Render { | ||
| 60 | my $basedir = File::Spec->catdir(File::Spec->tmpdir, 'RG'); | ||
| 61 | mkdir $basedir, 0777 unless -d $basedir; | ||
| 62 | my $tmpdir = mkdtemp(File::Spec->catdir($basedir, 'XXXXXXXX')) or die $!; | ||
| 63 | |||
| 64 | my $out = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">' . | ||
| 65 | $_[0]->root->sprint; | ||
| 66 | |||
| 67 | my $cnt = 1; | ||
| 68 | while ($out =~ s{src="data:image/png;base64,([^"]+)"}{src="$cnt.png"}) { | ||
| 69 | open my $fh, ">$tmpdir/$cnt.png" or die $!; | ||
| 70 | binmode ($fh); | ||
| 71 | print $fh decode_base64($1); | ||
| 72 | $cnt++; | ||
| 73 | } | ||
| 74 | |||
| 75 | open my $fh, ">$tmpdir/out.html" or die $!; | ||
| 76 | print $fh $out; | ||
| 77 | close $fh; | ||
| 78 | |||
| 79 | my $absdir = File::Spec->rel2abs($tmpdir); | ||
| 80 | $absdir =~ s{^(\w):}{/$1:}g; | ||
| 81 | $absdir =~ s{\\}{/}g; | ||
| 82 | my $convert = File::Spec->catfile( | ||
| 83 | $basedir, substr($tmpdir, -8) . '.htm' | ||
| 84 | ); | ||
| 85 | open $fh, ">$convert" or die $!; | ||
| 86 | binmode($fh); | ||
| 87 | my $macro = +PrintConverter(); | ||
| 88 | $macro =~ s/\$PATH/$absdir/g; | ||
| 89 | print $fh $macro; | ||
| 90 | close $fh; | ||
| 91 | |||
| 92 | if ($^O eq 'MSWin32') { | ||
| 93 | for (1..60) { | ||
| 94 | last if -s "$tmpdir/out.prn"; | ||
| 95 | sleep 1; | ||
| 96 | } | ||
| 97 | sleep 1; | ||
| 98 | } | ||
| 99 | else { | ||
| 100 | system( | ||
| 101 | ($ENV{SWRITER} || ($^O eq 'MSWin32' | ||
| 102 | ? "C:/Progra~1/OpenOf~1.0/program/soffice.exe" | ||
| 103 | : "/usr/local/OpenOffice.org1.1.0/program/swriter")), | ||
| 104 | $convert, | ||
| 105 | ); | ||
| 106 | } | ||
| 107 | for (1..10) { | ||
| 108 | last if -s "$tmpdir/out.prn"; | ||
| 109 | sleep 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | unlink "$tmpdir/out.prn"; | ||
| 113 | unlink "$tmpdir/out.html"; | ||
| 114 | rmdir $tmpdir; | ||
| 115 | return 1; | ||
| 116 | } | ||
| 117 | |||
| 118 | my %VarAtt = ( | ||
| 119 | date => [qw( type datetime sdnum 1028;1033;MM/DD/YYYY ) ], | ||
| 120 | time => [qw( type datetime sdnum 1028;1033;HH:MM:SS ) ], | ||
| 121 | page => [qw( type page subtype random format arabic ) ], | ||
| 122 | page_count => [qw( type docstat subtype page format arabic ) ], | ||
| 123 | ); | ||
| 124 | |||
| 125 | sub var { | ||
| 126 | my $self = shift; | ||
| 127 | my $item = $_; | ||
| 128 | $self->NEXT::var(@_); | ||
| 129 | $item->set_tag('sdfield'); | ||
| 130 | $item->set_att(@{ $VarAtt{$item->att('name')} || [] }); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub head { | ||
| 134 | my $self = shift; | ||
| 135 | $_->insert_new_elt( | ||
| 136 | 'meta' => { | ||
| 137 | 'http-equiv' => 'content-type', | ||
| 138 | 'content' => 'text/html; charset=utf-8', | ||
| 139 | } | ||
| 140 | ); | ||
| 141 | $_->insert_new_elt( | ||
| 142 | style => {}, $self->HeadDimensions($_) | ||
| 143 | ); | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | use constant PrintConverter => q{ | ||
| 149 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | ||
| 150 | <HTML> | ||
| 151 | <HEAD> | ||
| 152 | <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8"> | ||
| 153 | <TITLE>Print Converter</TITLE> | ||
| 154 | <META HTTP-EQUIV="CONTENT-SCRIPT-TYPE" CONTENT="text/x-StarBasic"> | ||
| 155 | <SCRIPT LANGUAGE="StarBasic"> | ||
| 156 | <!-- | ||
| 157 | ' $LIBRARY: Standard | ||
| 158 | ' $MODULE: HTML2Print | ||
| 159 | Sub HTML2Print | ||
| 160 | Dim oDesktop As Object | ||
| 161 | Dim sInDir As String | ||
| 162 | Dim sOutDir As String | ||
| 163 | Dim sFile As String | ||
| 164 | Dim sURL As String | ||
| 165 | Dim sPrint As String | ||
| 166 | Dim oDocument As Object | ||
| 167 | |||
| 168 | On Error Resume Next | ||
| 169 | |||
| 170 | sInDir = "file://$PATH/" | ||
| 171 | sOutDir = "file://$PATH/" | ||
| 172 | |||
| 173 | Dim aLoad(0) As New com.sun.star.beans.PropertyValue | ||
| 174 | aLoad(0).Name = "Hidden" | ||
| 175 | aLoad(0).Value = True | ||
| 176 | |||
| 177 | Dim aPrint(0) As New com.sun.star.beans.PropertyValue | ||
| 178 | |||
| 179 | oDesktop = createUnoService("com.sun.star.frame.Desktop") | ||
| 180 | sFile = Dir(sInDir+"*.html") | ||
| 181 | |||
| 182 | While sFile <> "" | ||
| 183 | sURL = sInDir + sFile | ||
| 184 | oDocument=oDesktop.loadComponentFromURL(sURL, "_blank", 0, aLoad()) | ||
| 185 | sPrint = sOutDir + Left(sFile, Len(sFile)-5) + ".prn" | ||
| 186 | oDocument.print(aPrint()) | ||
| 187 | oDocument.storeToURL(sPrint,aStore()) | ||
| 188 | oDocument.dispose() | ||
| 189 | sFile = Dir | ||
| 190 | Wend | ||
| 191 | StarDesktop.currentComponent.dispose() | ||
| 192 | End Sub | ||
| 193 | |||
| 194 | ' --> | ||
| 195 | </SCRIPT> | ||
| 196 | </HEAD> | ||
| 197 | <BODY LANG="" DIR="LTR" SDONLOAD="Standard.HTML2Print.HTML2Print"></BODY> | ||
| 198 | </HTML> | ||
| 199 | }; |