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

Hi, I have one xml file, I extracted some comments and saved in pdf file.I written code like this
#!/usr/bin/perl use warnings; use strict; use PDF::API2; use PDF::API2::Page; use XML::LibXML::Reader; use Data::Dumper; my $file; open( $file, 'formal.xml'); my $reader = XML::LibXML::Reader->new( IO => $file ) or die ("unab +le to open file"); my %nums; while ($reader->nextElement( 'Number' )) { my $number = $reader->copyCurrentNode(1)->textContent; $reader->nextElement( 'address' ); my $node = $reader->copyCurrentNode(1); my $infohash = { house => $node->getElementsByTagName( 'housenumber' )[0]->tex +tContent, street => $node->getElementsByTagName( 'streetname' )[0]->tex +tContent, }; $nums{$number} = $infohash; } "syntax error here "}" my $pdf = PDF::API2->new(); # $pdf->mediabox('Letter'); my $font = $pdf->corefont('Times-Roman'); my $page = $pdf->page(); $page->mediabox('Letter'); my $cnt=0; for my $line (split /\n/, Dumper(%nums)) { if ($cnt > 46) { $page = $pdf->page(); $cnt=0; } my $text = $page->text(); $text->font($font,14); $text->translate(72, 720-$cnt*14); $text->text($line); ++$cnt; } $pdf->saveas('svr.pdf');
when I run this code it extracting and printing extracted tags as it is in pdf file like as shown below.
$VAR1 = '24'; $VAR2 = '<Address> <housenumber="120"/> <streetname="xxx"/> </Address>'; $VAR3 = '25'; $VAR4 = '<Address> <housenumber="150"/> <streetname="xxx"/> </Address>'; $VAR5 = '27'; $VAR6 = '<Address> <housenumber="140"/> <streetname="xxx"/> </Address>';
like that but i need to print data in pdf like this
number: 24, address information of the student. Information:Address, housenumber="120", streetname="xxx". number: 25, address information of the student. Information:Address, housenumber="150", streetname="xxx". number: 27, address information of the student. Information:Address, housenumber="140", streetname="xxx".
I need to print output like this in pdf file. In my Written code I am printing xml tags as it is. What should i do to print like, help me with script. I cahnged little bit and i am getting syntax errors at " ")[" near and "}" before pdf starts.

Replies are listed 'Best First'.
Re: perl script to print data like this in pdf.
by roboticus (Chancellor) on Oct 20, 2011 at 15:09 UTC

    veerubiji:

    You can make your program output the data on the console in the format you want, right? If so, it's a simple step to making a PDF document look just like it. The bit I did for you with Data::Dumper is a straight ripoff from the PDF::API2 documentation.

    If you can't write your perl script to output the data on the console in the format you want, then you ought to start with that first.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Hi I modified but i am getting syntax errores at near ")[" and also at end "}".

      my %nums; while ($reader->nextElement( 'Number' )) { my $number = $reader->copyCurrentNode(1)->textContent; $reader->nextElement( 'address' ); my $node = $reader->copyCurrentNode(1); my $infohash = { house => $node->getElementsByTagName( 'housenumber' )[0]->tex +tContent, street => $node->getElementsByTagName( 'streetname' )[0]->tex +tContent, }; $nums{$number} = $infohash; }

        veerubiji:

        The syntax is nearly there. Try it this way:

        my %nums; while ($reader->nextElement('Number')) { my $number = $reader->copyCurrentNode(1)->textContent; $reader->nextElement('address'); my $node = $reader->copyCurrentNode(1); my $infohash = { house => $node->getElementsByTagName( 'housenumber' )->[0]->t +extContent, street => $node->getElementsByTagName( 'streetname' )->[0]->t +extContent, }; $nums{$number} = $infohash; }

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: perl script to print data like this in pdf.
by choroba (Cardinal) on Oct 20, 2011 at 15:09 UTC
    Just replace $VAR1 = 'XX' with number: XX, and so on. You might want to study perlre. But, as was suggested before by Jenda, parsing the whole XML into a data structure should make your code easier to understand and maintain.
Re: perl script to print data like this in pdf.
by afoken (Chancellor) on Oct 21, 2011 at 08:30 UTC

    See also perl script to convert xml to pdf.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)