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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.