in reply to Re^2: PDF::API2 printing non ascii characters
in thread PDF::API2 printing non ascii characters

Hello again Anonymous Monk,

In this case you can use URI::Escape. See sample bellow:

#!/usr/bin/perl
use strict;
use warnings;
use URI::Escape;
use feature 'say';

my $str = "Character one: ω character two: ∞";
my $hex_code = uri_escape( $str );
say $hex_code;

my $string = uri_unescape( $hex_code );
say $string;

__END__

$ perl test.pl
Character%20one%3A%20%CF%89%20character%20two%3A%20%E2%88%9E
Character one: ω character two: ∞

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^4: PDF::API2 printing non ascii characters
by vr (Curate) on Mar 13, 2018 at 16:21 UTC

    Hi, thanos1983. I'd like to point out, that use of non-latin1 characters in Perl source, without use utf8; is misleading. Perhaps your example might be modified to this (note the differences, while output is deceptively the same):

    #!/usr/bin/perl
    use strict;
    use warnings;
    use URI::Escape;
    use feature 'say';
    
    use utf8;
    use Encode qw/ encode decode /;
    binmode STDOUT, ':utf8';
    
    my $str = "Character one: ω character two: ∞";
    my $hex_code = uri_escape_utf8( $str );
    say $hex_code;
    
    my $string = decode 'UTF-8', uri_unescape( $hex_code );
    say $string;
    
    __END__
    
    

    To answer Anonymous Monk's direct question:

    use strict; use warnings; use PDF::API2; use URI::Escape; use Encode qw/ decode /; my $percent_encoded_str = '%CF%89%20%E2%88%9E'; my $octets = uri_unescape $percent_encoded_str; my $proper_unicode_str = decode 'UTF-8', $octets; my $pdf = PDF::API2-> new; my $page = $pdf-> page; my $text = $page-> text; my $ttf_font = $pdf-> ttfont( 'DejaVuSans.ttf' ); $text-> font( $ttf_font, 20 ); $text-> translate( 50, 700 ); $text-> text( $proper_unicode_str ); $pdf-> saveas( 'test.pdf' );

      Hello vr,

      Indeed I got mislead due to the output. You are absolutely right. Thanks for the note.

      Seeking for Perl wisdom...on the process of learning...not there...yet!

      Wow, thanks!!!

      I can finally output those non ascii characters correctly in pdf :)))

Re^4: PDF::API2 printing non ascii characters
by Anonymous Monk on Mar 13, 2018 at 16:22 UTC

    I'm scratching my head now with the code below:

    #sometext is a web input my $line = uri_escape($sometext); # $line prints $VAR1 = 'Hello%20%CF%89%20%E2%88%9E'; $line = uri_unescape($line); # $line prints Hello ω ∞
    instead of Hello ω ∞

    What am I missing?

      Hello Anonymous Monk,

      Try something like that:

      #!/usr/bin/perl
      use utf8;
      use strict;
      use warnings;
      use URI::Escape;
      use feature 'say';
      use Encode qw/ decode /;
      binmode STDOUT, ':utf8';
      
      sub nonDecodedUri {
          return uri_unescape( shift );
      }
      
      sub decodedUri {
          return decode 'UTF-8', uri_unescape( shift );
      }
      
      say nonDecodedUri('Hello%20%CF%89%20%E2%88%9E');
      say decodedUri('Hello%20%CF%89%20%E2%88%9E');
      
      __END__
      
      $ perl test.pl
      Hello ω ∞
      Hello ω ∞
      

      Notice the comments of fellow Monk vr on his answer above.

      Hope this helps, BR.

      Seeking for Perl wisdom...on the process of learning...not there...yet!

        Thank you so much. Really appreciate all the solutions here :)))