In my job, we only send documents to clients in pdf formats. Sometimes we have to send several documents at once and it takes a good time to open each one and save it as a pdf, it’s not a very pleasant task.
In order to minimize this problem, I’ve created a simple powershell script that opens a Word document and saves it as pdf without any user interaction. With such script, I can convert a lot of files at once, just like this:
ls . *.doc* –Recurse | %{ ~\scripts\doc2ps1.ps1 $_.fullname }With this command, I list recursively each doc/docx file in a directory and save each one as PDF.
By default, Office 2007 apps are not able to save files as PDF, so you have to install the add-in from Microsoft first to use the script. The add-in is available here.
Bellow is the script, it’s also available in my PowerShell scripts repository at GitHub (http://github.com/dougfernando/utility-scripts/tree/master) as doc2pdf.ps1.param (
[string]$source = $(Throw "You have to specify a source path."))
$extensionSize = 3
if ($source.EndsWith("docx")) {
$extensionSize = 4
}
$destiny = $source.Substring(0, $source.Length - $extensionSize) + "pdf"
$saveaspath = [ref] $destiny
$formatPDF = [ref] 17
$word = new-object -ComObject "word.application"
$doc = $word.documents.open($source)
$doc.SaveAs($saveaspath, $formatPDF)
$doc.Close()
echo "Converted file: $source"
ps winword | kill
An important note, the script closes every Word instance in the end, so save any work previous using it.


25 comments:
Hello
your script is great . At the moment I need to convert docx files to word97-2003 format and was just wondering how you worked out that pdf format is [ref] 17?
thanks
Dermot
Damn, truly great topic. How will I find your subscription?
Kate Watcerson
bug finders
Hi,
Why did you kill the word processes at the end ?
I'll try to explain why it is 17 and not 18 or whatever.
If you look at the Document.SaveAs Method http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas(v=vs.80).aspx, you'll notice that its attribute are passed by reference. Here we need the FileFormat. Going to the description of the WdSaveFormat Enumeration http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdsaveformat.aspx , we notice that the wdFormatPDF, an enumberation member responsible for saving to PDF, has a number 0f 17 (I don't know why the Member table does not have a column with numbers, I think the leftmost column was supposed to have these numbers but eventually was published empty).
PDF => 17
Other numberers can be discovered
using .net reflector on Microsoft.Office.Interop.Word.dll and searching for wdsaveformat
Thanks... very helpful.
There's a little error in my env.
line 15,no ref.
Hi,
I am not able to convert RTF files greater than 160MB, it throws up the following exception, even though other RTF's in the same folder converts just fine.
Exception calling "SaveAs" with "2" argument(s): "Command failed"
At C:\test\rtf2pdf.ps1:15 char:1
+ $doc.SaveAs($saveaspath, $formatPDF)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : COMException
Sometimes it says it converted sucessfuuly but i am not able to locate the PDF.
Converted file: C:\source\huge\RTFtable-lisitng-Huge.rtf
Superb, what a blog it is! This blog provides useful data to
us, keep it up.
my page; http://www.grupogedai.com.br/content/ways-create-pr-release
Nice post. I learn something totally new and challenging on websites I stumbleupon every day.
It will always be interesting to read through articles from other
writers and practice something from their sites.
Here is my website: manufacturing information
Hello to every body, it's my first pay a visit of this web site; this blog consists of remarkable and truly excellent material for readers.
Look into my web-site; publishing news
Good article. I certainly love this site. Stick with it!
Also visit my page ... finance article
I blog quite often and I seriously appreciate your content.
The article has truly peaked my interest. I'm going to book mark your website and keep checking for new information about once per week. I subscribed to your Feed too.
Here is my homepage; 50.co.za
What's up Dear, are you in fact visiting this web page on a regular basis, if so after that you will definitely obtain pleasant experience.
Also visit my weblog ... Motorcycle Articles
If you would like to increase your knowledge only keep visiting this web page and be updated with the most
recent news update posted here.
Feel free to visit my website http://www.boersenkreis-halle.de/wiki/index.php?title=Methods_to_promote_your_small_business_with_press_announcements
I blog frequently and I genuinely thank you for your content.
Your article has really peaked my interest.
I'm going to bookmark your blog and keep checking for new information about once per week. I subscribed to your RSS feed as well.
Here is my webpage; Public Relations
What's up all, here every person is sharing these knowledge, therefore it's
fastidious to read this weblog, and I used to visit this website everyday.
Here is my weblog; clubitaliani.com
Hey there! Are you aware should they make any plugins to assist
with SEO? I’m looking to get my blog to rank in te se's for some targeted keywords but I’m perhaps not seeing excellent gains. Once you learn of any please share. Be thankful!
my homepage: public relation
As the admin of this web page is working, no uncertainty very soon it
will be famous, due to its quality contents.
Feel free to visit my webpage; Real Estate News
Right here is the perfect website for anybody who hopes
to find out about this topic. You understand a whole lot its almost tough to argue
with you (not that I actually will need to…HaHa).
You certainly put a brand new spin on a topic that's been discussed for decades. Great stuff, just excellent!
Also visit my page financial business news
I don't know whether it's just me or if perhaps everybody else encountering problems
with your site. It appears as if some of the written text within your content
are running off the screen. Can someone else please provide feedback and
let me know if this is happening to them as well?
This could be a issue with my web browser because I've had this happen before. Thanks
Feel free to surf to my site; art on Line
When I originally left a comment I seem to have clicked on the -Notify
me when new comments are added- checkbox and now whenever a comment
is added I recieve four emails with the exact same comment.
There has to be a means you can remove me from that service?
Thank you!
Have a look at my website: auto news
Hello would you mind stating which blog platform you're using? I'm going to start my own blog soon but
I'm having a tough time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I'm looking for something completely unique.
P.S Sorry for being off-topic but I had to ask!
My site: icsci.it
Hi I am so happy I found your web site, I really found you by error, while I was searching on Digg for something else, Regardless I am here now and would just
like to say thanks for a incredible post and a all round enjoyable blog (I
also love the theme/design), I don’t have time to read it all at the
minute but I have saved it and also added your RSS feeds,
so when I have time I will be back to read much more, Please
do keep up the excellent jo.
Feel free to visit my page rv news
There’s a lot of folks that I do believe would enjoy your content.
Please allow me to know. Thank you
Here is my blog; communications and public relations
I hardly write remarks, but i did some searching and wound up here "Powershell script for converting Word documents to PDF format".
And I actually do have a couple of questions for you if
it's allright. Is it only me or does it give the impression like some of the responses look like left by brain dead people? :-P And, if you are posting on additional social sites, I would like to keep up with everything fresh you have to post. Could you make a list of the complete urls of all your shared sites like your linkedin profile, Facebook page or twitter feed?
Here is my blog post :: design communication
Post a Comment