1

I have been stuck at this for too long.

I am generating PDF invoices using PDF::Reuse and Template::Toolkit. Now here is the issue; when I retrieve a DB record (using DBIx) that contains a utf8 character, the PDF gets corrupted. Here is the code that generates the PDF:

sub create_pdf {                                                                                       
    my ( $self, $pdf_template, $vars ) = @_;                                                           

    my $path = $ENV{NEW_DIR} . '/OTTCatalyst/root/templates/pdf/';                                     


    my $tt = Template->new({                                                                           
        INCLUDE_PATH => $path,                                                                         
        PLUGIN_BASE  => 'OTT::Template::Plugin',                                                       
        PRE_CHOMP    => 3,                                                                             
        POST_CHOMP   => 3,                                                                             
        LOAD_PERL    => 1,                                                                             
    });                                                                                                

    my $template = read_file( $path.$pdf_template, binmode => ':utf8' );                               

    my ( $output, $stdout, $stderr );                                                                  

    {                                                                                                  
        local $CWD = $path;                                                                            
        capture { $tt->process( \$template, $vars, \$output, binmode => ':utf8') } \$stdout, \$stderr; 
    }                                                                                                  

    throw_fatal 'There is an error with the template: '.$tt->error if not $stdout;                     
    return $stdout;                                                                                    
}         

$vars is the variable that contains the DBIx row object that contains the data with the UTF8 character in it.

To be more exact the variable that contains the utf8 character is:

$vars->{invoice}->customer->addr1

if I just display the addr1 as is, the PDF will get corrupted. If I wrap it around:

encode_utf8($vars->{invoice}->customer->addr1);

It will show: III Å ̈ 1/1, 2nd floor

What is should show is: III Ũ 1/1, 2nd floor

Can anybody help me with this. If anything is unclear please let me know.

Thanks in advance

1 Answer 1

1

You probably need to decode the data that you are reading from your database. If you read the documentation for your DBD (I don't know which database you're using) then there's probably an option for auto-decoding any text fields as they are fetched.

1
  • Thanks @Dave Looks like the problem lies within PDF::Reuse. Using PDF::API2 has solved my problem. PDF::Reuse does not have good support for utf8 character. I.e. if I manually put 'III Ũ 1/1, 2nd' in my TT file then the PDF generated will be corrupt. However the same does not happen with PDF::API2. Vote up for your help.
    – Hamster
    Jun 27, 2014 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.