Converting Documents to PDF

Discuss the word processor
Post Reply
Arak
Posts: 11
Joined: Wed Jan 23, 2008 3:00 pm

Converting Documents to PDF

Post by Arak »

Hi!!

Is there anyway to convert documents, which oowriter can open, to PDF from the commmand line on a linux system? I want this done without opening an oowriter window. I need to write a script which automates the conversion of documents to PDF.

oowriter has a comman line option -p using which a file can be sent directly to a printer. Using this option, is it possible to print to a postscript file?? If this is possible, I could convert the resulting .ps file to a .pdf file.


Thank you in advance for your help.
OOo 2.0.2 / Ubuntu 6.06.2 LTS
User avatar
floris v
Volunteer
Posts: 4638
Joined: Wed Nov 28, 2007 1:21 pm
Location: Netherlands

Re: Converting Documents to PDF

Post by floris v »

LibreOffice 25.8.4.2 on Ubuntu Linux
If your problem has been solved or your question has been answered, please edit the first post in this thread and add [Solved] to the title bar.
Nederlandstalig forum
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Converting Documents to PDF

Post by acknak »

Have you read the Forum Survival Guide?

Did you try a search? This comes up a lot, although I think this might be the first time in this forum.

As far as I know, you need a macro to actually drive the ouptut to PDF.
AOO4/LO5 • Linux • Fedora 23
sybille
Volunteer
Posts: 122
Joined: Sat Jan 05, 2008 12:21 pm
Location: France

Re: Converting Documents to PDF

Post by sybille »

I use a macro that is adapted from an oooforum post. I think I had to make an adjustment or two to it, but I did this ages ago and didn't document my changes, and I don't remember what, if anything, I did. Anyway, I've been using it in this form for a couple of years now and on two different Linux distros.

I call the macros with Nautilus-actions, to convert on demand by right-clicking on a file in Nautilus (and since I do this often enough, I run the OOo quickstarter to speed things up). But of course the macros could be called from a script.

Here's 1) the macro and 2) an example of a command I use to run it to convert to pdf:
1)

Code: Select all

' 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.
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, _
            Array(MakePropertyValue( "Hidden", True ),))

   cFile = Left( cFile, Len( cFile ) - 4 ) + ".pdf"
   cURL = ConvertToURL( cFile )
   
   ' Save the document using a filter.   
   oDoc.storeToURL( cURL, Array(_
            MakePropertyValue( "FilterName", "writer_pdf_Export" ),)
   
   oDoc.close( True )
End Sub

' Save document as a Microsoft Word file. 
Sub SaveAsDoc( cFile ) 
   ' mostly a copy of SaveAsPDF
   cURL = ConvertToURL( cFile )
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, (_
            Array(MakePropertyValue( "Hidden", True ),))


   cFile = Left( cFile, Len( cFile ) - 4 ) + ".doc"
   cURL = ConvertToURL( cFile )
   
   oDoc.storeToURL( cURL, Array(_
            MakePropertyValue( "FilterName", "MS Word 97" ),)
   oDoc.close( True )

End Sub


' Save document as an OpenOffice 2 file. 
Sub SaveAsOOO( cFile ) 
   ' mostly a copy of SaveAsPDF. Save as an OpenOffice file. 
   cURL = ConvertToURL( cFile )
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, _
            Array(MakePropertyValue( "Hidden", True ),))

   ' Set output file extension based on lower-case 
   ' version of input extension.
   Select Case LCase(Right(cFile,3))
     Case "ppt"         ' PowerPoint file.
       cFileExt = "odp"
     Case "doc"         ' Word file.
       cFileExt = "odt"
     Case "rtf"         ' Rich Text file.
       cFileExt = "odt"
     Case "xls"         ' Excel file.
       cFileExt = "ods"
     Case Else
       cFileExt = "xxx"
    End Select
       
   cFile = Left( cFile, Len( cFile ) - 3 ) + cFileExt
   cURL = ConvertToURL( cFile )
   
   oDoc.storeAsURL( cURL, Array() )
   oDoc.close( True )

End Sub


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 Function
2)

Code: Select all

ooffice -headless "macro:///Standard.MyConversions.SaveAsPdf(%u)"
You see that I called the module for the macros "MyConversions," following the suggestion of this xml.com article, which is another good reference. The %u is a nautilus-actions variable that stands for the gnome-vfs URI of the file to be converted. You'd need to substitute the appropriate variable for the filename in your script.
If your problem has been solved, please edit this thread's initial post and add "[Solved]" to the subject line. Thanks!
-------
About Ubuntu Linux
Zotero, for research and bibliography management with OOo.
OOo 2.4.X on Ubuntu 8.x + None needed :)
Arak
Posts: 11
Joined: Wed Jan 23, 2008 3:00 pm

Re: Converting Documents to PDF

Post by Arak »

It doesn't seem to be working. The SaveAsOOO function is working. But, when I use the SaveAsPDF function, it exits with the error message:

BASIC runtime error.
An Exception occurred
Type:com.sun.star.task.ErrorCodeIOException
Message:.

Any help would be appreciated.
OOo 2.0.2 / Ubuntu 6.06.2 LTS
sybille
Volunteer
Posts: 122
Joined: Sat Jan 05, 2008 12:21 pm
Location: France

Re: Converting Documents to PDF

Post by sybille »

What is the exact command you're running to call the macro?

Are there any spaces in the name of the file you're trying to convert?
If your problem has been solved, please edit this thread's initial post and add "[Solved]" to the subject line. Thanks!
-------
About Ubuntu Linux
Zotero, for research and bibliography management with OOo.
OOo 2.4.X on Ubuntu 8.x + None needed :)
Arak
Posts: 11
Joined: Wed Jan 23, 2008 3:00 pm

Re: Converting Documents to PDF

Post by Arak »

The command I am using is given below:
ooffice2 -invisible "macro:///Standard.MyConversions.SaveAsPDF(/home/bob/temp/sample.doc)"

BTW, the above command does not work on my computer, but it does work on a computer running OOo 2.3.0 on Ubuntu 7.10

And no, there are no spaces in the file names I used.
OOo 2.0.2 / Ubuntu 6.06.2 LTS
sybille
Volunteer
Posts: 122
Joined: Sat Jan 05, 2008 12:21 pm
Location: France

Re: Converting Documents to PDF

Post by sybille »

Arak wrote:BTW, the above command does not work on my computer, but it does work on a computer running OOo 2.3.0 on Ubuntu 7.10.
Sorry to be stating the obvious here, but what are the differences between your machine and the one where the macro is working? You haven't mentioned which Linux and which OOo you're using on the problematic computer. The desktop environment that's running would be useful information, also, or if it's a headless install, how you have configured things so far...

Maybe try "file:///home/bob/temp/sample.doc" instead, i.e. the complete URI for the file.
If your problem has been solved, please edit this thread's initial post and add "[Solved]" to the subject line. Thanks!
-------
About Ubuntu Linux
Zotero, for research and bibliography management with OOo.
OOo 2.4.X on Ubuntu 8.x + None needed :)
Arak
Posts: 11
Joined: Wed Jan 23, 2008 3:00 pm

Re: Converting Documents to PDF

Post by Arak »

I am using openoffice 2.0.2 on ubuntu 6.06.2 LTS. I am using the version of openoffice supplied by ubuntu.


I will try what you asked me to try and get back to you.
OOo 2.0.2 / Ubuntu 6.06.2 LTS
sybille
Volunteer
Posts: 122
Joined: Sat Jan 05, 2008 12:21 pm
Location: France

Re: Converting Documents to PDF

Post by sybille »

You could also try using "soffice" to run the macro:
soffice "macro:///Standard.MyConversions.SaveAsPDF(file:///home/bob/temp/sample.doc)"

Also, I just noticed that you were using "ooffice2" rather than "ooffice" - is that right for the version of OOo that comes with Dapper/6.06? I think it would tell you "command not found" if the command was incorrect, though.
If your problem has been solved, please edit this thread's initial post and add "[Solved]" to the subject line. Thanks!
-------
About Ubuntu Linux
Zotero, for research and bibliography management with OOo.
OOo 2.4.X on Ubuntu 8.x + None needed :)
Arak
Posts: 11
Joined: Wed Jan 23, 2008 3:00 pm

Re: Converting Documents to PDF

Post by Arak »

Hi!!

The command I used was ooffice and not ooffice2

By the way, when it gives that error message, it says that the error is in the following line in the function SaveAsPDF
oDoc.storeToURL( cURL, Array(_
OOo 2.0.2 / Ubuntu 6.06.2 LTS
sybille
Volunteer
Posts: 122
Joined: Sat Jan 05, 2008 12:21 pm
Location: France

Re: Converting Documents to PDF

Post by sybille »

I see the same result as you if I change a document's template and then try to run the macro without restarting OOo. Once I completely quit OOo, the conversion macro works fine for all types of documents. So, is there any possibility that OOo is still running in between your attempts to run the macro?

I've never used the macro with the same configuration you're using: OOo 2.0.2 on Ubuntu 6.06.2 LTS. I did use it with that version of OOo, but on Gentoo rather than Ubuntu. I've not yet had any problems with the Ubuntu build of OOo, but I often see the recommendation to try an official build of OOo from Sun instead of the Ubuntu versions. I don't know if that would be worth trying in your case.
If your problem has been solved, please edit this thread's initial post and add "[Solved]" to the subject line. Thanks!
-------
About Ubuntu Linux
Zotero, for research and bibliography management with OOo.
OOo 2.4.X on Ubuntu 8.x + None needed :)
Post Reply