convert .doc to .pdf after adding line numbers


One of our latest projects involves the conversion of a document to pdf after adding line numbers. After some research I decided that the fastest way to do it while keeping formatiing is through a LibreOffice (formerly headless invocation.

I started by writing a macro for LibreOffice in Basic to open the document, insert the line numbers and save to pdf. I modified the code given here and this is the result

REM ***** BASIC *****

Sub addLineNumbers(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. oDoc = StarDesktop.loadComponentFromURL(cURL, "_blank", 0, Array())

'insert line numbers oDoc.getLineNumberingProperties().Interval = 1 oDoc.getLineNumberingProperties().IsOn = True

Dim comps comps = split (cFile, ".") If UBound(comps) > 0 Then comps(UBound(comps)) = "pdf" cfile = join (comps, ".") Else cfile = cFile + ".pdf" Endif

cURL = ConvertToURL(cFile)

' Save the document using a filter. Dim args(0) as new args(0).Name = "FilterName" args(0).Value = "writerpdfExport" oDoc.storeToURL(cURL,args())


End Sub

Subsequently I needed to invoke the macro from the command line (and from the php exec() command) This code (and this website) to the rescue. Note there is a portable version of LibreOffice for use in shared environments for download if you follow the link.
libreoffice -invisible -nofirststartwizard -headless -norestore "macro:///Standard.Module1.addLineNumbers(/home/qwazix/Documents/outofbounds/test/test.odt)
In the meantime I stumbled upon a genius idea that adds line numbers to existing pdf files. Have a look at it.

Additional resources you may find useful for php+pdf