Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Manipulating MSWord with Win32::OLE to convert a word document to pdf

by ruhk (Scribe)
on Sep 10, 2003 at 20:00 UTC ( #290514=perlquestion: print w/ replies, xml ) Need Help??
ruhk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I would just like to warn you that I am a bit new at perl and am still trying to get the hang of it, so please be kind. After a couple days of searching, I can find no better way of automatically converting a .doc file to .pdf than just have perl control word and let the Adobe PDF Writer do it for me. First, the code:
#!/usr/bin/perl -w use strict; use Cwd; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; use Win32::OLE::Variant; my $dir = shift || cwd(); my $word; eval {$word = Win32::OLE->GetActiveObject('Word.Application')}; die "Word not installed" if $@; unless (defined $word) { $word = Win32::OLE->new('Word.Application', 'Quit') or die "Cannot start Word"; } Win32::OLE->Option(Warn => 3); print STDERR "Open test.doc\n"; my $doc = $word->{'Documents'}->Open("$dir/test.doc"); print STDERR "save test.pdf\n"; $word->{'ActivePrinter'} = 'Acrobat PDFWriter'; $doc->PrintOut({ PrintToFile => 1, OutputFileName => "$dir/test.pdf", Background => 0, Append => 0, Range => wdPrintAllDocument, Item => wdPrintDocumentContent, Copies => 1, PageType => wdPrintAllPages, }); undef $doc; undef $word;
This code seems to work except it outputs a pdf file that is 0 bytes. Any ideas why? Ive read all the documentation on Win32::OLE I can find however I find no listing of commands I can use such as the PrintOut command. Any ideas? Apologies ahead of time for my lack of perl skills.

Comment on Manipulating MSWord with Win32::OLE to convert a word document to pdf
Download Code
Re: Manipulating MSWord with Win32::OLE to convert a word document to pdf
by VSarkiss (Monsignor) on Sep 10, 2003 at 20:26 UTC

    Your code looks OK from this distance. I'd first suggest trying to output a few characters from your document on STDERR, just to make sure you're really opening the file as you intended. (I think, but I'm not sure, that if the file doesn't exist, Word will treat it as a "New" rather than "Open".)

    As for commands you can use, those will not be in the Win32::OLE documents, which only outline how to use the calls to various COM objects. What you're really after is the Word documentation itself. You can translate the VBA calls in the documentation pretty much directly to Perl. You may need to install additional Word documents to get the "Developer's Reference".

    HTH

Re: Manipulating MSWord with Win32::OLE to convert a word document to pdf
by davis (Vicar) on Sep 11, 2003 at 11:40 UTC

    Here's some old code which I had lying around. It might do exactly what you're looking for.

    use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; use Getopt::Std; my %opts; getopts('o:i:', \%opts); my $infile = $opts{i} || die "No input File specified - use -i file\n +"; my $outfile = $opts{o} || die "No output File specified - use -o file\ +n"; my $Word = Win32::OLE->new('Word.Application', 'Quit'); # $Word->{'Visible'} = 1; # if you want to see what's going on $Word->Documents->Open($infile) || die("Unable to open document ", Win32::OLE->LastError()); $Word->{DisplayAlerts}=-1; $Word->ActiveDocument->PrintOut({ Background => 0, Append => 0, Range => wdPrintAllDocument, Item => wdPrintDocumentContent, Copies => 1, PageType => wdPrintAllPages, OutputFileName => $outfile, PrintToFile => 1, }); $Word->ActiveDocument->Close(0); unlink $outfile; #The pdf creation appends ".pdf" to $outfile, but c +reates an empty $outfile as well

    Then again, it might not. I've given up using Adobe Acrobat, and I'm now using Samba and ghostscript to do the same thing for free. See here if you're interested.


    davis
    It's not easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
      Hi, I've been using a slightly modified verison of this code, the problem I'm having is when Word tries to print Acrobat Distiller produces an error: When you create a postscript file you have to send the host fonts. I can change this property manually when running word, but I can't find how to automate it, any ideas folks?

        I wouldn't use Samba anymore. I'd use PDFCreator now, and this message looks to point to a very easily scriptable method.
        cheers


        davis
        Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.
Re: Manipulating MSWord with Win32::OLE to convert a word document to pdf
by guha (Priest) on Sep 11, 2003 at 15:53 UTC

    I have not used Acrobat Writer myself so I can't vouch for that the problem isn't due to incorrect usage, but there is another angle on this.

    Which version of w32 and Word are you running on?

    I've had problems with code running fine on w98 and Office 97, but fails on XP and Office 2000.

    Haven't really determined the exact culprit but the solution was to access the Wordconstants in this manner instead.

    use Win32::OLE; use Win32::OLE::Const; #Note const not loaded here . . . my $wdConst = Win32::OLE::Const->Load("Microsoft Word"); #const loaded + explicitely . . $doc->PrintOut({ PrintToFile => 1, OutputFileName => "$dir/test.pdf", Background => 0, Append => 0, Range => $wdConst->{wdPrintAllDocument}, Item => $wdConst->{wdPrintDocumentContent}, Copies => 1, PageType => $wdConst->{wdPrintAllPages}, });

    Note this is untested, coming directly from my old and crufty memory, so check the docs for exact syntax.

    HTH

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://290514]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-09-21 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls