[Solved] Print to PDFCreator w/out changing default printer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
9811%Deet
Posts: 7
Joined: Tue Feb 17, 2015 3:22 am

[Solved] Print to PDFCreator w/out changing default printer

Post by 9811%Deet »

Hello, you fellas helped me with a macro that pulls sheets based on a cell's contents a while back, and this has been working very well since:
(viewtopic.php?f=20&t=75413)

Code: Select all

Dim PrintRangeTmpl(0) as New com.sun.star.table.CellRangeAddress
PrintRangeTmpl(0).Sheet=0
PrintRangeTmpl(0).StartColumn = 0
PrintRangeTmpl(0).EndColumn = 13
PrintRangeTmpl(0).StartRow = 0 
PrintRangeTmpl(0).EndRow = 64  
oSheets = ThisComponent.Sheets
NumSheets = oSheets.Count

for i = 0 to NumSheets - 1
   oSheet = oSheets.getByIndex(i)
   oCell = oSheet.getCellrangeByName("A100")
   If oCell.String = "1" Then
      PrintRangeTmpl(0).Sheet = i
else
   oSheet.PrintAreas = Array()
   oSheet.isvisible = False
end If
next i
ThisComponent.Print(Array())

But now I'm interested in setting this code to print through a printer emulator called "PDFCreator". This will allow my macro to output the selected data to a PDF.

I've been able to change the printer to PDFCreator through commands like:

Code: Select all

rem ----------------------------------------------------------------------
dim args9(0) as new com.sun.star.beans.PropertyValue
args9(0).Name = "Printer"
args9(0).Value = "PDFCreator"

dispatcher.executeDispatch(document, ".uno:Printer", "", 0, args9())
...and this works fine to print the PDF, but the problem I'm having is that it changes the default printer, so that other print jobs from the same workbook will then default to the PDFCreator too.

If this workbook was only being used on one printer, it would be a simple matter to change the printer back, but the default printer will be named differently at every location where this workbook will be used. I need some way to change the printer to PDFCreator for this one macro, but then return the printer to whatever system default is defined afterward.

Any help would be greatly appreciated!
Last edited by 9811%Deet on Thu Jul 23, 2015 3:55 pm, edited 1 time in total.
Openoffice 4.1.1 - Windows 7 Professional
User avatar
Zizi64
Volunteer
Posts: 11358
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Print to PDFCreator without changing default printer.

Post by Zizi64 »

Why you want to use a virtual printer (in this case) for exporting a document to PDF format? You can control the PDF export function of the AOO by macros...
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
9811%Deet
Posts: 7
Joined: Tue Feb 17, 2015 3:22 am

Re: Print to PDFCreator without changing default printer.

Post by 9811%Deet »

The macro was originally written for a printer. Some of the users will be uploading the pdf to an online database. Others will be printing the documents for filling. In both cases, the formatting, page selection, etc... needs to be uniform.

I would assume we could use the existing macro along with PDFCreator to produce a uniform result. But the problems defining the default are messing things up.
Openoffice 4.1.1 - Windows 7 Professional
User avatar
Zizi64
Volunteer
Posts: 11358
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Print to PDFCreator without changing default printer.

Post by Zizi64 »

Maybe these topics will help you (please read all of posts, and see all of links and code snippets in these topics):

viewtopic.php?t=12982
viewtopic.php?f=20&t=56708
viewtopic.php?t=41554
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
Posts: 11358
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Print to PDFCreator without changing default printer.

Post by Zizi64 »

Here is Andrew Pitonyak's code for printer selection from the other (older) FORUM:
http://www.oooforum.org/forum/viewtopic.phtml?t=3388

Code: Select all

'******************************************************************
'Author: Andrew Pitonyak
'email:   andrew@pitonyak.org
Sub PrintCurrentDocument
  Dim mPrintopts1(), x as Variant
  'Dimensioned at 0, if you set any other properties, be certain to set this to a higher value....
  'Dim mPrintopts2(0) As New com.sun.star.beans.PropertyValue
  Dim oDocument As Object, oPrinter As Object

  oDocument = ThisComponent

  '***********************************
  'Do you want to choose a certain printer
 Dim mPrinter(0) As New com.sun.star.beans.PropertyValue
 mPrinter(0).Name="Name"
 mPrinter(0).value="Other printer"     ' insert name of your printer
 oDocument.Printer = mPrinter()

  '***********************************
  'To simply print the document do the following:
oDocument.Print(mPrintopts1())

  '***********************************
 End Sub 
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Print to PDFCreator without changing default printer.

Post by RoryOF »

In such a situation the methodology is simple: alter your macro to read the name of the installed printer and remember it. Now execute the body of your macro and before exiting restore the chosen printer.

You already have the code to set the printer to be PDFCreator; similar code can be used to set the original printer. You really only need to sort out how to read the name of the current printer, which a few minutes studying the API should sort out.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Zizi64
Volunteer
Posts: 11358
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Print to PDFCreator without changing default printer.

Post by Zizi64 »

Or you can use the pdf-filter option (without usage of any virtual printer) with some similar code:

Code: Select all

Sub Expert_PDF

	oSheet = ThisComponent.Sheets.getByName("Sheet1")
	REM Or you can choose other sheet
	ThisComponent.CurrentController.setActiveSheet(oSheet)
    
    MyExportArea = "$A1:F200" 
    REM Or you can 'calculate' the string based on other conditions
	
	document = ThisComponent.CurrentController.Frame
	oRange = oSheet.getCellRangeByName(MyExportArea)
	ThisComponent.CurrentController.Select(oRange)


	PDF_URL = "File://... !String!" REM You can determine it from the URL of the actual document, or you can use a fixed one.
	

Dim args2(1) as new com.sun.star.beans.PropertyValue
Dim Arg(0) as new com.sun.star.beans.PropertyValue
	
	Arg(0).Name = "Selection"
	Arg(0).Value = oRange

	args2(0).Name = "FilterName"
	args2(0).Value = "calc_pdf_Export"
	args2(1).Name = "FilterData"
	args2(1).Value = Arg()


	ThisComponent.storeToURL(PDF_URL,args2())	

End Sub
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
9811%Deet
Posts: 7
Joined: Tue Feb 17, 2015 3:22 am

Re: Print to PDFCreator without changing default printer.

Post by 9811%Deet »

RoryOF wrote:In such a situation the methodology is simple: alter your macro to read the name of the installed printer and remember it. Now execute the body of your macro and before exiting restore the chosen printer.

You already have the code to set the printer to be PDFCreator; similar code can be used to set the original printer. You really only need to sort out how to read the name of the current printer, which a few minutes studying the API should sort out.

That's all I needed.

Thank you!
Openoffice 4.1.1 - Windows 7 Professional
Post Reply