convert .doc to .pdf after adding line numbers

19.01.12

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 OpenOffice.org) 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 com.sun.star.beans.PropertyValue args(0).Name = "FilterName" args(0).Value = "writerpdfExport" oDoc.storeToURL(cURL,args())

oDoc.close(True)

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

http://stackoverflow.com/questions/7364/pdf-editing-in-php http://framework.zend.com/manual/en/zend.pdf.usage.html http://www.setasign.de/support/manuals/fpdf-tpl/fpdf-tpl/fpdf-tpl-gettempaltesize/ http://www.fpdf.de/funktionsreferenz/?funktion=GetX http://www.phplivedocx.org/2009/02/06/convert-doc-to-pdf-in-php/ http://stackoverflow.com/questions/4416667/php-pdf-template-library-with-pdf-output http://geekswithblogs.net/robertphyatt/archive/2011/11/19/converting-.docx-to-pdf-or-.doc-to-pdf-or-.doc.aspx http://superuser.com/questions/250086/what-is-needed-to-invoke-libreoffice-running-just-the-macro-without-the-gui http://www.togaware.com/linux/survivor/ConvertMSWord.html http://codesnippets.services.openoffice.org/Writer/Writer.StoreWriterAsPDF.snip