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

I have a hash called %data which have information of Students like Name, Class etc. I want to print those contents into PDF file. I have created a PDF file using PDF::API2::Simple module.

The foreach loop looks like this:

foreach my $keys( sort { $data{$a} <=> $data{$b}} keys %data ){ $pdf->text($data{$keys}{'Name'}); }

$pdf has:

my $pdf = PDF::API2::Simple->new( file => 'file.pdf' }

But its not getting printed in PDf file. Please suggest what is the method to print data into PDF file.

2017-12-11 Athanasius added code and paragraph tags

Replies are listed 'Best First'.
Re: How can I print hash content to pdf file ?
by Corion (Patriarch) on Dec 10, 2017 at 09:18 UTC

    Have you checked that you get the correct data and can print it to the screen instead of a PDF file?

    use strict; use PDF::API2::Simple; my %data = (...); #my $pdf = PDF::API2::Simple->new( file => 'file.pdf' } foreach my $keys( sort { $data{$a} <=> $data{$b}} keys %data ){ print $data{$keys}{'Name'}, "\n"; }

    The next thing would be to check that you can actually print to the PDF file. Try printing a plain list to a PDF file:

    use strict; use PDF::API2::Simple; my @names = (Diana Bruce Clark); my $pdf = PDF::API2::Simple->new( file => 'file.pdf' } foreach my $name( @names ){ $pdf->text($name}); }

    One of the two programs should work for you and should help you determine where your problem lies, in the data or in writing the data to the PDF.

      Yes, I tried printing a plain text something like this: $pdf->text( 'Hello PERL', x => 300, y => 300 ); The plain text printing perfectly. Is there any syntax to print the hash ?

        If you need to supply coordinates, why don't you supply them when you print in your loop?

        Try maybe printing stuff into separate lines?

        my $offset = 300; for my $line (@names) { $pdf->text( "Hello PERL", x => 300, y => $offset ); $offset = $offset + 50; # move to "next line" };

        I chose 50 by random, maybe you want 12 (points) or 24.

Re: How can I print hash content to pdf file ?
by poj (Abbot) on Dec 10, 2017 at 12:59 UTC

    You could create a table of the data with PDF::Table

    #!/usr/bin/perl use strict; use PDF::API2; use PDF::Table; # test hash my @cols = qw(name class other); my %data = (); for my $i ('001'..'100'){ for (@cols){ $data{'id'.$i}{$_} = $_.$i; } }; # create data array my @tbl; push @tbl ,\@cols; for my $id (sort keys %data){ push @tbl, [ @{$data{$id}}{@cols} ]; } # create pdf my $pdf = new PDF::API2(-file => "table_of_data.pdf"); $pdf->mediabox(595, 842); # A4 my $page = $pdf->page; my $pdftable = new PDF::Table; my (undef, undef, $boty) = $pdftable->table( $pdf, $page, \@tbl, x => 50, w => 495, start_y => 800, next_y => 800, start_h => 750, next_h => 750, padding => 5, padding_right => 10, ); $pdf->saveas();
    poj
Re: How can I print hash content to pdf file ?
by choroba (Cardinal) on Dec 10, 2017 at 10:23 UTC
    Crossposted to StackOverflow. It's considered polite to inform about crossposting to avoid duplicate work of people not attending both sites.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,