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

Can Anyone help me by suggesting an approach or with a code snippet to convert .doc files to .pdf using Perl

Just to make sure I need to use only Perl no other stuff

  • Comment on Converting .doc files to .pdf using perl

Replies are listed 'Best First'.
Re: Converting .doc files to .pdf using perl
by LanX (Saint) on May 12, 2011 at 20:09 UTC
      > Just to make sure I need to use only Perl no other stuff
      Preferably without the need for a computer.
        > > Just to make sure I need to use only Perl no other stuff

        OK ...

        DB<100> $_="files.doc" DB<101> tr/cdo/fpd/ DB<102> print files.pdf

        ... done! :)

        > Preferably without the need for a computer.

        We could train rats ...

        Cheers Rolf

Re: Converting .doc files to .pdf using perl
by moritz (Cardinal) on May 12, 2011 at 20:21 UTC

      It came up with the following error when I executed the above code.

      system('unoconv', '-f', 'pdf', 'yourfile.doc') and die "Can't launch unoconv: $!";

        You need to install unoconv first. Take a look here. That line of code is just a system call - I guess a little "tongue in cheek" because you asked for a pure Perl solution...
Re: Converting .doc files to .pdf using perl
by Khen1950fx (Canon) on May 13, 2011 at 03:08 UTC
    Just to make sure I need to use only Perl no other stuff

    I reversed your thought process---instead of .doc to .pdf, think .pdf "from" .doc. This was my first stab at it:

    #!/usr/bin/perl use strict; use Text::FromAny; use HTML::FromText; use PDF::FromHTML; my $tFromAny = Text::FromAny->new( file => '/root/Desktop/basic.doc'); my $text = $tFromAny->text; text2html($text); my $pdf = PDF::FromHTML->new( encoding => 'utf-8' ); $pdf->load_file(\$text); $pdf->convert( FontUnicode => 'Helvetica', LineHeight => 10, Landscape => 1, ); $pdf->write_file('/root/Desktop/target.pdf');
      I wouldn't have understood the OP requirement to be that. Word can save files in .pdf format, so I would expect to produce the same results as that with Perl. .doc format is pretty unbreakable, whereas .docx can be parsed because it is nothing more than zipped tree of xml files. The only way I can think of is to automate Word into doing it from .doc, which is pretty horrible.

      One world, one people

Re: Converting .doc files to .pdf using perl
by believer (Sexton) on May 13, 2011 at 08:49 UTC
    Converting .doc to .pdf is a very specific task that is not implemented in pure Perl as far as I know. Why "only Perl"? Are you working on Windows or *nix?

      I am working on Windows.

        I suggest you search for a command line conversion program. You can call it from perl.