On 12 Nov 2007 at 13:35, Giuseppe Castagno wrote:

> Hi,
> 
> mike scott wrote:
...
> > I've been working on code for automatically generating a document, and 
> > converting to pdf. I'm currently using the storeToURL method, 
...
> > However, I've just found I also need to do a 'booklet' conversion as 
> > well - A5 pages, printed 2-up on A4, and the pages correctly ordered.  
> > Now, this is an option that seems to exist, interactively, only under 
> > the 'print' menu, not on 'export as pdf'.  So:
> > 1) is this available at all for pdf conversion?
> > 2) if so, how do I use it from within OOo basic?
> 
> currently PDF export filter doesn't take into account the printer
> settings.

At the risk of giving people indigestion I'm going to publish here the 
code I've come up with. Not in expectation of comment or prizes 
(certainly no marks here for style :-)  ) --- just that I think it 
should be archived somewhere to perhaps save someone else the agony.

I'm appending a (not well-written) OOo macro that does a print-to-file 
on a postscript printer (using assorted bits from the 'net), plus a 
(freebsd-checked) shell script that runs that macro, and munges the 
resulting postscript using the ghostscript tools into a booklet form.

It still has bugs, largely because PS printers seem to be rather 
restricted in the page sizes they're happy with.  So no warranty; it 
just fulfils my immediate purpose, and may be food for thought.

If you don't like code, stop reading now :-)




=====================================
keywords - Openoffice pdf booklet postscript
=====================================

sub TEST
        PrintDocument( "/home/mike/PDFTEST/test.odt", 
"/home/mike/PDFTEST/xyzzy.ps", "IIISi P2", "50x50")
End Sub


' CARE: NO ERROR CHECKING HERE. Well, very little, anyway!
' Note in particular the file names MUST have a full path name, or 
ConvertToURL will fail
' Paper sizes may be a3/4/5, b4/5, letter, legal, or WxH  (eg 110x220 
in mm)
' NB the WxH construction doesn't work. I think problems with printer 
drivers
' (esp under XP) 
Sub PrintDocument( cInputFile, cOutputFile, cPrinter, cPapersize )
   ' 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.
   cURL = ConvertToURL( cInputFile )
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, _
            Array(MakePropertyValue( "Hidden", True ),))

   cURL = ConvertToURL( cOutputFile )

' select principle print options - print to file
   Dim mPrintopts(0) as New com.sun.star.beans.PropertyValue
   mPrintopts(0).Name = "FileName"
   mPrintopts(0).Value = cOutputFile


'select printer and media.  Set paper size - note lamentable lack of 
error checking here.
   Dim mPrinter(1) As New com.sun.star.beans.PropertyValue
   mPrinter(0).Name = "Name"
   mPrinter(0).value = cPrinter
   
   oDoc.Printer = mPrinter()

   Dim PSize As New com.sun.star.awt.Size                                       
' in mm/100
   cPapersize = LCase(cPapersize)
   Select Case cPapersize
   Case "a3"
      mPrinter(1).Name = "PaperFormat"
      mPrinter(1).Value = com.sun.star.view.PaperFormat.A3
   Case "a4"
      mPrinter(1).Name = "PaperFormat"
      mPrinter(1).Value = com.sun.star.view.PaperFormat.A4
   Case "a5"
      mPrinter(1).Name = "PaperFormat"
      mPrinter(1).Value = com.sun.star.view.PaperFormat.A5
   Case "b4"
      mPrinter(1).Name = "PaperFormat"
      mPrinter(1).Value = com.sun.star.view.PaperFormat.B4
   Case "b5"
      mPrinter(1).Name = "PaperFormat"
      mPrinter(1).Value = com.sun.star.view.PaperFormat.B5
   Case "letter"
      mPrinter(1).Name = "PaperFormat"
      mPrinter(1).Value = com.sun.star.view.PaperFormat.LETTER
   Case "legal"
      mPrinter(1).Name = "PaperFormat"
      mPrinter(1).Value = com.sun.star.view.PaperFormat.LEGAL
   Case Else
      Dim xy(1)
      xy = Split(cPapersize, "x", 2)
      PSize.Width = Val(xy(0)) * 100
      PSize.Height = Val(xy(1)) * 100
      mPrinter(1).Name="PaperSize"
      mPrinter(1).Value = PSize
   End Select
   
   oDoc.Printer = mPrinter()

' and print it
   oDoc.Print(mPrintopts())

' printing is asynchronous, so spin until complete......
' see http://www.oooforum.org/forum/viewtopic.phtml?t=4922
   bIsBusy = True
   While bIsBusy
      aPrintConditions = oDoc.getPrinter()

      For i = LBound( aPrintConditions ) To UBound( aPrintConditions )
         If aPrintConditions(i).Name = "IsBusy" Then
            bIsBusy = aPrintConditions(i).Value
            Exit For
         EndIf
      Next
      Wait 100    ' 1/10 sec; we have a superfluous wait at end.
   WEnd 

' .... and now we really can close the document
   oDoc.close(True)

End Sub


============================

#!/bin/sh
# convert odt to pdf with psbook and psnup processing
#
#usage: odtprt2file -i input -o output -p printer -s odtpagesize -n nup 
[-S finalpagesize] [-d ]"
#
#
# nb paper sizes. The OOo macro will recognize a3/4/5, b4/5, letter, 
legal (upper or lower case), or eg 123x456 (W by H in mm). This
# is a different set of descriptors than gs needs.
# ps2pdf requires the size in gs fashion as either -sPAPERSIZE=a4 (or 
whatever) or as -dDEVICEWIDTHPOINTS=w -dDEVICEHEIGHTPOINTS=h  (in 1/72 
inch)
# psnup needs the output either as -pa4  or  -wwidth -hheight (with 
units, eg -w5cm -h5cm) -- FIXME I'm not clear wther the inpt needs 
specifying!
#
# (odt) -> OOo -> (ps) -> psbook -> (sp) -> psnup -> (ps) -> ps2pdf -> 
(pdf)
#
# The WxY stuff seems top muck up the postscript code completely, nor 
will the printer
# driver produce the expected output. Don't use it!!!


PATH=$PATH:/usr/local/bin export PATH


usage() {
      echo "usage: odtprt2file -i input -o output -p printer -s 
odtpagesize -n nup [-S finalpagesize] [-x scaling] [-d]"
}



# parse args. Note in particular the printer name may well contain 
spaces
while test $# -gt 0 ; do
   case "$1" in
   -i)
      input="$2"; shift 2;;
   -o)
      output="$2"; shift 2;;
   -p)
      printer="$2"; shift 2;;
   -s)
      odtpagesize="$2"; shift 2;;
   -S)
      finalpagesize="$2"; shift 2;;
   -n)
      nup="$2"; shift 2;;
   -d)
      dflag=-d; shift;;
   -x)
      scaling="$2"; shift 2;;
   -D)
      debug=1; shift;;
   *)
      usage; exit 1;;
   esac
done

if [ -z "$nup" -o -z "$input" -o -z "$output" -o -z "$printer" -o -z 
"$odtpagesize" ] ; then
   usage
   exit 1
fi

if [ -z "$finalpagesize" ] ; then
   finalpagesize="a4"
fi

if [ -n "$scaling" ] ; then
   scaling="-s$scaling"
fi

# MUST have fully-qualified names for thre macro to work
if [ "`echo $input | sed 's+^.+/+`" != "$input" ] ; then
   input="$PWD/$input"
fi


if [ "`echo $output | sed 's+^.+/+`" !=  "$output" ] ; then
   output="$PWD/$output"
fi


tmp1=/tmp/tmp-1-$$.ps
tmp2=/tmp/tmp-2-$$.ps
if [ ! -z "$debug" ] ; then
   tmp1=$PWD/tmp1.ps
   tmp2=$PWD/tmp2.ps
fi

## cInputFile, cOutputFile, cPrinter, cPapersize
/usr/local/bin/openoffice.org-2.3.0-swriter -headless 
"macro:///Standard.Module1.PrintDocument(\"$input\", \"$tmp1\", 
\"$printer\", \"$odtpagesize\")"


psbook -q $tmp1 | psnup $dflag -$nup -p$finalpagesize -P$odtpagesize 
$scaling > $tmp2
####psbook $tmp1 | psnup -q $dflag -$nup -p$finalpagesize -s1 > $tmp2
ps2pdf -sPAPERSIZE=$finalpagesize $tmp2 $output

[ -z "$debug" ] && rm -f $tmp1 $tmp2





-- 
various incoming sites blocked because of spam; see 
http://www.scottsonline.org.uk for a list and openpgp crypto key
(key fingerprint 2ACC 9F21 5103 F68C 7C32 9EA8 C949 81E1 31C9 1364)
[EMAIL PROTECTED]    Mike Scott, Harlow, Essex, England

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to