Perl 脚本打印这样的数据

发布于 2024-12-10 17:56:28 字数 2513 浏览 0 评论 0原文

可能的重复:
在 Perl 中,如何使用 XML 文件中的数据生成 PDF 文件?

我有一个 xml 文件,我提取了一些注释并保存在 pdf 文件中。当我运行此代码时,我编写了这样的脚本

#!/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 ("unable to open file");

my %nums;
while ($reader->nextElement( 'Number' ) ) {
    my $number = $reader->readInnerXml();
    $reader->nextElement( 'address' ); 
    my $info = $reader->readOuterXml(); 
    $nums{$number} = $info;
}

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');

,它提取并打印提取的 内容标签与 pdf 文件中一样如下图所示。

   $VAR1 = '24';
   $VAR2 = '<Address>
            <housenumber="120"/>
           <streetname="xxx"/>
            <information/>
           </Address>';
  $VAR3 = '25';
   $VAR4 = '<Address>
       <housenumber="150"/>
    <streetname="xxx"/>
      <information/>
       </Address>';
     $VAR5 = '27';
     $VAR6 = '<Address>
             <housenumber="140"/>
             <streetname="xxx"/>
             </information>
                </Address>';

像这样,但我需要像这样打印 pdf 中的数据,

  number: 24, address information of the student.
     Information:Address,
             housenumber="120",
              streetname="xxx",
               information. 
     number: 25, address information of the student.
     Information:Address,
             housenumber="150",
              streetname="xxx"
              information. 
     number: 27, address information of the student.
     Information:Address,
             housenumber="140",
              streetname="xxx"
               information.  

我需要像这样在 pdf 文件中打印输出。在我的书面代码中,我按原样打印 xml 标签。我应该做什么来打印,帮我写这个脚本。

Possible Duplicate:
In Perl, how can I produce a PDF file using data in an XML file?

I have one xml file, I extracted some comments and saved in pdf file.I written script 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 ("unable to open file");

my %nums;
while ($reader->nextElement( 'Number' ) ) {
    my $number = $reader->readInnerXml();
    $reader->nextElement( 'address' ); 
    my $info = $reader->readOuterXml(); 
    $nums{$number} = $info;
}

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"/>
            <information/>
           </Address>';
  $VAR3 = '25';
   $VAR4 = '<Address>
       <housenumber="150"/>
    <streetname="xxx"/>
      <information/>
       </Address>';
     $VAR5 = '27';
     $VAR6 = '<Address>
             <housenumber="140"/>
             <streetname="xxx"/>
             </information>
                </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",
               information. 
     number: 25, address information of the student.
     Information:Address,
             housenumber="150",
              streetname="xxx"
              information. 
     number: 27, address information of the student.
     Information:Address,
             housenumber="140",
              streetname="xxx"
               information.  

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 this script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

家住魔仙堡 2024-12-17 17:56:28

使用 XML::LibXML::Simple 来解析 XML 文件并这将允许您获取节点或属性的内容(不带标签)。

Use XML::LibXML::Simple instead to parse the XML file and this will allow you to get the content of the either the nodes or the attributes (without the tags).

灼疼热情 2024-12-17 17:56:28

您可以使用 $reader ->copyCurrentNode(1) 将元素及其子元素解析为 DOM 树,然后您可以从中提取所需的值,如下所示(警告:未经测试的代码):

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]->textContent,
        street => $node->getElementsByTagName( 'streetname'  )->[0]->textContent,
    };
    $nums{$number} = $infohash;
}

请参阅 XML: :LibXML::Element 了解有关如何从 DOM 中提取信息的更多信息。

You can use $reader->copyCurrentNode(1) to parse an element and its children into a DOM tree, from which you can then extract the values you want, like this (warning: untested code):

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]->textContent,
        street => $node->getElementsByTagName( 'streetname'  )->[0]->textContent,
    };
    $nums{$number} = $infohash;
}

See XML::LibXML::Element for more information on how to extract information from the DOM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文