From 389007ac0512ed52a2a9bf1bf46f680b5fbb7f57 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 25 Jun 2008 15:50:24 +0000 Subject: [PATCH] =?utf8?q?Wenn=20UTF-8=20als=20Datenbankcharset=20verwende?= =?utf8?q?t=20wird,=20so=20muss=20der=20ReportGenerator=20die=20an=20PDF::?= =?utf8?q?API2=20=C3=BCbergebenen=20Strings=20als=20UTF-8=20markieren=20(m?= =?utf8?q?it=20Perls=20Encode-Modul).?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/ReportGenerator.pm | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/SL/ReportGenerator.pm b/SL/ReportGenerator.pm index 2273c2e6b..f5550a62d 100644 --- a/SL/ReportGenerator.pm +++ b/SL/ReportGenerator.pm @@ -1,5 +1,6 @@ package SL::ReportGenerator; +use Encode; use IO::Wrap; use List::Util qw(max); use Text::CSV_XS; @@ -406,6 +407,15 @@ sub _cm2bp { return $_[0] * 72 / 2.54; } +sub _decode_text { + my $self = shift; + my $text = shift; + + $text = decode('UTF-8', $text) if ($self->{text_is_utf8}); + + return $text; +} + sub generate_pdf_content { eval { require PDF::API2; @@ -427,6 +437,9 @@ sub generate_pdf_content { my $num_columns = scalar @visible_columns; my $num_header_rows = 1; + my $font_encoding = $main::dbcharset || 'ISO-8859-15'; + $self->{text_is_utf8} = $font_encoding =~ m/^utf-?8$/i; + foreach $name (@visible_columns) { push @column_props, { 'justify' => $self->{columns}->{$name}->{align} eq 'right' ? 'right' : 'left' }; } @@ -440,7 +453,7 @@ sub generate_pdf_content { foreach $name (@visible_columns) { $column = $self->{columns}->{$name}; - push @{ $data_row }, $column->{text}; + push @{ $data_row }, $self->_decode_text($column->{text}); push @{ $cell_props_row }, {}; } @@ -454,7 +467,7 @@ sub generate_pdf_content { push @cell_props, $cell_props_row; foreach my $custom_header_col (@{ $custom_header_row }) { - push @{ $data_row }, $custom_header_col->{text}; + push @{ $data_row }, $self->_decode_text($custom_header_col->{text}); my $num_output = ($custom_header_col->{colspan} * 1 > 1) ? $custom_header_col->{colspan} : 1; if ($num_output > 1) { @@ -472,7 +485,7 @@ sub generate_pdf_content { foreach my $row_set (@{ $self->{data} }) { if ('HASH' eq ref $row_set) { if ($row_set->{type} eq 'colspan_data') { - push @data, [ $row_set->{data} ]; + push @data, [ $self->_decode_text($row_set->{data}) ]; $cell_props_row = []; push @cell_props, $cell_props_row; @@ -496,7 +509,7 @@ sub generate_pdf_content { my $col_idx = 0; foreach my $col_name (@visible_columns) { my $col = $row->{$col_name}; - push @{ $data_row }, join("\n", @{ $col->{data} }); + push @{ $data_row }, $self->_decode_text(join("\n", @{ $col->{data} })); $column_props[$col_idx]->{justify} = 'right' if ($col->{align} eq 'right'); @@ -554,7 +567,7 @@ sub generate_pdf_content { $pdf->mediabox($paper_width, $paper_height); my $font = $pdf->corefont(defined $pdfopts->{font_name} && $supported_fonts{lc $pdfopts->{font_name}} ? ucfirst $pdfopts->{font_name} : 'Verdana', - '-encoding' => $main::dbcharset || 'ISO-8859-15'); + '-encoding' => $font_encoding); my $font_size = $pdfopts->{font_size} || 7; my $title_font_size = $font_size + 1; my $padding = 1; @@ -567,7 +580,7 @@ sub generate_pdf_content { my $top_text_height = 0; if ($self->{options}->{top_info_text}) { - my $top_text = $self->{options}->{top_info_text}; + my $top_text = $self->_decode_text($self->{options}->{top_info_text}); $top_text =~ s/\r//g; $top_text =~ s/\n+$//; @@ -614,7 +627,7 @@ sub generate_pdf_content { my $curpage = $pdf->openpage($page_num); if ($pdfopts->{number}) { - my $label = $main::locale->text("Page #1/#2", $page_num, $pdf->pages()); + my $label = $self->_decode_text($main::locale->text("Page #1/#2", $page_num, $pdf->pages())); my $text_obj = $curpage->text(); $text_obj->font($font, $font_size); @@ -623,12 +636,13 @@ sub generate_pdf_content { } if ($opts->{title}) { + my $title = $self->_decode_text($opts->{title}); my $text_obj = $curpage->text(); $text_obj->font($font, $title_font_size); - $text_obj->translate(($paper_width - $margin_left - $margin_right) / 2 + $margin_left - $text_obj->advancewidth($opts->{title}) / 2, + $text_obj->translate(($paper_width - $margin_left - $margin_right) / 2 + $margin_left - $text_obj->advancewidth($title) / 2, $paper_height - $margin_top); - $text_obj->text($opts->{title}, '-underline' => 1); + $text_obj->text($title, '-underline' => 1); } } -- 2.20.1