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

Re: perl script to convert xml to pdf

by roboticus (Chancellor)
on Oct 19, 2011 at 15:42 UTC ( #932436=note: print w/replies, xml ) Need Help??

in reply to perl script to convert xml to pdf


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.

Replies are listed 'Best First'.
Re^2: perl script to convert xml to pdf
by veerubiji (Sexton) on Oct 20, 2011 at 09:48 UTC
    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.

        hi please can you provide any example for me because I am very new to this type of concepts and I am very beginner to Perl. I have XMl data like above question ,I need to print in pdf file like this

        student number: 24 address: house number="128", street name= "xxxx", studeent number: 26 address: house number="179", street name= "xxxx",

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://932436]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2018-03-23 13:59 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (293 votes). Check out past polls.