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

perl script to convert xml to pdf

by veerubiji (Sexton)
on Oct 19, 2011 at 14:52 UTC ( #932426=perlquestion: print w/replies, xml ) Need Help??
veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have xml data that I extracted from large file and stored in a data as shown below

$var1={ 'stud.xml'={ '24'=>'<address> <streetname="xxxx"/> <housenum="138"/"> </address>' '20'=>'<address> <streetname="xxxx"/> <housenum="110"/"> </address>' } 'pav.xml'={ '26'=>'<address> <streetname="xxxx"/> <housenum="138"/"> </address>' '27'=>'<address> <streetname="xxxx"/> <housenum="110"/"> </address>' } . . . }
now I need to convert this XML data into pdf form.I heard that I can do by using PDF::API2 module. How to install that module because I am using strawberry perl.Give some examples that convert xml to pdf using this module.

Replies are listed 'Best First'.
Re: perl script to convert xml to pdf
by roboticus (Chancellor) on Oct 19, 2011 at 15:42 UTC


    I can't help you with the module installation (I don't use strawberry perl), but here's a way to convert your data from an XML file into a PDF:

    use strict; use warnings; use Data::Dumper; use PDF::API2; my $var1 = <<<<< set up your data here >>>>> my $pdf = PDF::API2->new(); $pdf->mediabox('Letter'); my $font = $pdf->corefont('Helvetica-Bold'); my $page = $pdf->page(); my $cnt=0; for my $line (split /\n/, Dumper($var1)) { 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('veerubiji.pdf');


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

      Hi, Thanks for your reply I understand your code and I executed, its generating pdf with input xml data as I posted in the question. It printing XML data as it is in pdf. just like dumping. If I want to print XML data more readable in pdf. not like XMl data as it is. what should I do.


        Yes, that's exactly what it does. You didn't specify *any* requirements on the output, so I did the minimum I could to satisfy them. Of course, PerlMonks isn't a code writing service, so had you put any nontrivial requirements in, I probably would have passed over this thread.

        In any case, I can't imagine that there's a "general purpose XML to PDF" procedure that would be useful, as there are myriad forms of XML documents, and even more ways to present the data. So you're going to have to get more detailed requirements from your user(s), and figure out how to turn them into code.

        First you must figure out what you want:

        1. Get to know the XML data you've got as input. (What elements are there? What are the rules specifying when an element is present or absent? What types of data and/or attributes can/will exist?)
        2. Figure out what you want the output to look like (Here you need to determine formatting, spacing, and everything else. Which elements indicate the start of a new page? Which elements need bold/underline/italics/colored text/background? Which sections are formatted as tables? Which as plaintext? etc.).
        3. Then figure out how to transform bits of your input to bits of your output. (For example: <FROB> element introduces a table where <Gragnar> introduces a row and <Plook> indicates a data cell. If the Plook element has an attribute="snog", then add a red background, otherwise use blue. The first row is a header which we ..... etc.)

        Now that you know what sorts of transformations you need to perform, you need to:

        1. Read the XML document (using your favorite XML parser),
        2. Perform the transformations you figured out (you can use XSLT, perl, or whatever),
        3. Then write the transformed bits to the PDF pages.


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

Re: perl script to convert xml to pdf
by Jenda (Abbot) on Oct 20, 2011 at 09:35 UTC

    Wouldn't it be much easier if the data looked like this?!?

    $var1={ 'stud.xml'={ '24' => { streetname => "xxxx", housenum => "138" }, '20' => { streetname => "xxxx", housenum => "110" } }, 'pav.xml'={ '26' => { streetname => "xxxx", housenum => "138" }, '27' => { streetname => "xxxx", housenum => "110" }, }, . . . }

    How do you extract the data anyway? You are not attempting to extract data from XML with regexps I hope!

    Enoch was right!
    Enjoy the last years of Rome.

Re: perl script to convert xml to pdf
by GrandFather (Sage) on Oct 19, 2011 at 20:45 UTC

    To install the module use cpan. If you've tried and failed then tell us how cpan failed for you.

    True laziness is hard work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://932426]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2017-03-24 00:09 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (294 votes). Check out past polls.