good PDFs from a bunch of MS .doc and .xls files. Company's business is
built around MS and MS-like products, and all internal documents are in
MS formats. So, the first obvious for my chief solution was buying
something like Adobe Acrobat/Distiller, but that was a bad way to spend
company's money.
Here goes I, in shiny blue suit with purple coak and letter 'S' on my
chest. It was like:
I: - Hey, boss, you look upset, what's up?
B: - I need to convert a heaps of those docs and spreadsheets to PDF,
and I dunno how to do it without spending a lot uf bucks.
I: - No problem! Try OpenOffice 2, it got a pretty nice 'save to PDF'
feature.
While IT-chief was installing and probing OOo2, I searched for the
possibility of batch conversion, and here what I've found:
1. open www.google.com
2. type "openoffice batch command line"
3. push "I'm feeling lucky"
4. follow the instructions
By doing this, you find yourself at
http://www.xml.com/pub/a/2006/01/11/from-microsoft-to-openoffice.html
which contain a pretty good example of using macroses in OpenOffice to
convert documents from one supported format to another. Create
conversion macros, save it, call OpenOffice binary from console with the
proper command line parameters telling which macros should be used
against which file -- and you get a nice PDF from your .doc or .xls!
Combined with Windows cmd's "for" call the above done the thing.
Some caveats about the code posted at the link below: using it to
convert .xls to PDF could (or could not) throw an error - that's because
a "writer_pdf_Export" filter is used. Change it to "calc_pdf_Export" for
.xls files and all should goes fine.
-- Update:
Here's the code:
Macros code
' Based on code from http://www.oooforum.org/forum/viewtopic.phtml?t=3772
' Save document as an Acrobat PDF file.
Sub SaveAsPDF( cFile )
cURL = ConvertToURL( cFile )
' Open the document. Just blindly assume that the document
' is of a type that OOo will correctly recognize and open
' without specifying an import filter.
' MsgBox(cFile)
' MsgBox(cURL)
oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, _
Array(MakePropertyValue( "Hidden", True ) _
,MakePropertyValue( "ReadOnly", True )))
cFile = Left( cFile, Len( cFile ) - 4 ) + ".pdf"
cURL = ConvertToURL( cFile )
Select Case LCase(Right(cFile,3))
Case "xls" ' Excel file.
oDoc.storeToURL( cURL, Array(_
MakePropertyValue( "FilterName", "calc_pdf_Export" ) _
,MakePropertyValue( "CompressMode", "0")))
Case Else
oDoc.storeToURL( cURL, Array(_
MakePropertyValue( "FilterName", "writer_pdf_Export" ) _
,MakePropertyValue( "CompressMode", "0")))
End Select
oDoc.close( True )
End Sub
' ------ unneeded save options skipped -------
Function MakePropertyValue( Optional cName As String, Optional uValue ) _
As com.sun.star.beans.PropertyValue
Dim oPropertyValue As New com.sun.star.beans.PropertyValue
If Not IsMissing( cName ) Then
oPropertyValue.Name = cName
EndIf
If Not IsMissing( uValue ) Then
oPropertyValue.Value = uValue
EndIf
MakePropertyValue() = oPropertyValue
End FunctionBat-file code
@echo off
for /r . %%f in (*.doc *.xls) do call :convert "%%f"
exit /b
:convert
echo converting %1
set ooobinary="C:\Program Files\OpenOffice.org 2.2\program\soffice.exe"
if exist "%temp%\convertme.pdf" del /q "%temp%\convertme.pdf"
copy /y %1 "%temp%\convertme%~x1">nul || echo Error copying %1 to %temp%! && pause && exit /b 1
%ooobinary% -invisible macro:///Standard.MyConversions.SaveAsPDF("%temp%\convertme%~x1")
if exist "%temp%\convertme.pdf" copy /y "%temp%\convertme.pdf" "%~p1\%~n1.pdf">nul && del /q "%temp%\convertme.pdf" "%temp%\convertme%~x1"
No comments:
Post a Comment