Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have unicode strings like this "\x{0052}\x{0044}\x{0024}" which come from the database.

When I print these onto a pdf file (using PDF::API2), the literal strings are printed instead of the actual symbols.

If I put these strings inside double quotes in perl code, they are printed out correctly. But when they come from the database, they are printed literally.

How do I print the actual symbols of these unicode values from the database?

Thank you in anticipation.

Replies are listed 'Best First'.
Re: Print unicode strings to pdf
by thanos1983 (Parson) on Mar 26, 2018 at 13:21 UTC

    Hello Anonymous Monk,

    Read this previously answered question PDF::API2 / unicode characters it is considered solved.

    Update: Also read this question PDF::API2 printing non ascii characters.

    Update2: Running this sample of code from PDF::API2 / unicode characters using your sample of unicode characters is producing:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use PDF::API2;
    
    # Create a blank PDF file
    my $pdf = PDF::API2->new();
    
    # Add a blank page
    my $page = $pdf->page();
    
    my $font = $pdf->ttfont('DejaVuSans.ttf');
    
    my $ustring = "Sample: \x{0052}\x{0044}\x{0024}";
    
    # Add some text to the page
    my $text = $page->text();
    
    $text->font($font, 20);
    $text->translate(80, 710);
    $text->text($ustring);
    
    # Save the PDF
    $pdf->saveas('test.pdf');
    
    __END__
    
    Sample: RD$
    

    Update3: I verified that the output is correct from this unicode table (Unicode-Table).

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Print unicode strings to pdf
by Corion (Patriarch) on Mar 26, 2018 at 13:29 UTC

    In the case that thanos1983's reply is not what you're looking for, maybe showing us some code would help us understand the situation better.

    I think that you currently have code like the following:

    #!perl -w use strict; my $hardcoded_string_in_perl = "\x{0052}\x{0044}\x{0024}"; my $string_as_read_from_database = '\x{0052}\x{0044}\x{0024}'; print $hardcoded_string_in_perl; # RDS print $string_as_read_from_database; # \x{0052}\x{0044}\x{0024} print_to_pdf( $hardcoded_string_in_perl ); # works and shows the corre +ct characters in the PDF print_to_pdf( $string_as_read_from_database ); # works and shows the c +orrect characters in the PDF

    If that is true, you will likely just need to convert the backslashed escape sequences to Perl characters. For example using something like the following subroutine:

    sub unescape { my( $str ) = @_; $str =~ s!\\x\{([0-9a-f]{4})\}!chr(hex $1)!ge; $str }; print unescape('\x{0052}\x{0044}\x{0024}');
      Hello Corion,

      With your unescaping it works! :) Thank you so much.

      This is what I see when I do mysql SELECT:
      +-----+------+-----------------------+ | 1 | \x{043b}\x{0432} | | 2 | \x{0042}\x{0073} | | 3 | \x{005a}\x{0024} | +-----+------+-----------------------+
      How should I save the unicode strings in the database so that the unescaping is not necessary?

        You haven't told us what database you are talking to and what you are using to load data into the database. Also, we will need to know what the column type is you're inserting into.

        When loading the data from Perl, you will need to use Encode to decode the data from its source representation to Unicode.

        You will likely need to tell the database driver when loading data into the database and when reading data out of the database that it should consider the column as UTF-8 encoded Unicode (if that's what your original data is).

        Alternatively, encode the data to UTF-8 encoded octets and then write those raw octets into the database. You will lose the ability to query the data from within the database in a nice way. LIKE queries and UPPER() will likely not work in the way you expect them.

        When reading from the database, set up your database so it decodes the data from your database format to Unicode in Perl.

        If you are using MySQL, read DBD::mysql for some UTF-8 options.

Re: Print unicode strings to pdf
by QM (Parson) on Mar 27, 2018 at 07:44 UTC
    It's going to be a long day for me. I clicked on this thinking it said "Pin unicorn earrings to pod".

    Time to get my coffee, and my glasses.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

A reply falls below the community's threshold of quality. You may see it by logging in.