Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: PDF::API2 printing non ascii characters

by Anonymous Monk
on Mar 13, 2018 at 15:08 UTC ( #1210829=note: print w/replies, xml ) Need Help??


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

What if the submitted html input is "%CF%89%20%E2%88%9E" (ω ∞) instead of the numeric codes below?

ω ∞

How do I decode that before handing over to the pdf text method?

Replies are listed 'Best First'.
Re^3: PDF::API2 printing non ascii characters
by thanos1983 (Vicar) on Mar 13, 2018 at 15:40 UTC

    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!

      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 :)))

      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!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1210829]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-06-18 23:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (111 votes). Check out past polls.

    Notices?