Saving as pdf on Mac

Thank You for this.

I have integrated your pdfPrintSettings into the old ListboxPrinter from Alex Restrepo, works fine.

ListboxPrinter

dim LBPrinter As new ListBoxPrinter
LBPrinter.SaveAsPDF (Listbox1, true) // ListBox Name , fit width true/false

Hi Lars,

Would this work with Real Studio 2012 Release 2.1 using Cocoa (Beta) in the Build Settings … menu item?

Thanks.

Lennox

Yes. It works with Real Studio 2012 Release 2.1 using Cocoa (Beta) in the Build Settings … menu item
Thanks.
Lennox

I forgot one very important thing: Escaping the path name. This means you will get problems if your file name includes <,> or &.

Here’s the corrected code:

dim pageSetup as new PrinterSetup dim pdfFile as folderItem pdfFile = GetSaveFolderItem("","sample.pdf") pageSetup.SetupString = App.pdfPrintSettings.Replace("PATHTOPDF", pdfFile.NativePath.ReplaceAll("&","&amp;").ReplaceAll("<","<").ReplaceAll(">",">")) dim g as Graphics = OpenPrinter(pageSetup) If g <> Nil Then // Draw your pages here End If

@ Axel: Thanks for the ListBoxPrinter! Maybe you can update your code, too.

[quote=79094:@Lars Brennicke]You can do this without AppleScript or plugins in a Cocoa Xojo app.

First, create a new constant in your app, name it pdfPrintSettings and set it to exactly this value:

[/quote]

How can you control page size with this method?

  • Karen

[quote=79177:@Karen Atkocius]How can you control page size with this method?
[/quote]

If you use this method, you will always get the default paper size the user has selected in System Preferences. If that’s not what you want, you need to change the pdfPrintSettings :

[code]<?xml version="1.0" encoding="UTF-8"?>

$archiver NSKeyedArchiver $objects $null $class CF$UID 12 NSAttributes CF$UID 2 $class CF$UID 11 NS.keys CF$UID 3 CF$UID 4 CF$UID 5 NS.objects CF$UID 6 CF$UID 7 CF$UID 8 NSJobDisposition NSSavePath NSPaperSize NSPrintSaveJob PATHTOPDF $class CF$UID 10 NS.sizeval CF$UID 9 NS.special 2 {PDFPAPERSIZEX, PDFPAPERSIZEY} $classes NSValue NSObject $classname NSValue $classes NSMutableDictionary NSDictionary NSObject $classname NSMutableDictionary $classes NSPrintInfo NSObject $classname NSPrintInfo $top root CF$UID 1 $version 100000 [/code]

This is a version that also specified the paper size (in points). Just replace PDFPAPERSIZEX and PDFPAPERSIZEY with the correct values for your paper size. The following code displays the page setup dialog and then creates a PDF with the specified format:

  dim pdfPageSetup as new PrinterSetup
  dim dialogPageSetup as new PrinterSetup
  dim pdfFile as folderItem
  dim result as boolean
  dim setupString as String
  result = dialogPageSetup.PageSetupDialog
  pdfFile = GetSaveFolderItem("","sample.pdf")
  setupString = App.pdfPrintSetup
  setupString = setupString.Replace("PATHTOPDF", pdfFile.NativePath.ReplaceAll("&","&amp;").ReplaceAll("<","<").ReplaceAll(">",">"))
  setupString = setupString.Replace("PDFPAPERSIZEX",str(dialogPageSetup.PageWidth)).Replace("PDFPAPERSIZEY", str(dialogPageSetup.PageHeight))
  pdfPageSetup.SetupString = setupString
  dim g as Graphics = OpenPrinter(pdfPageSetup)
  If g <> Nil Then
    // Draw your pages here
  End If

Thanks, I’ve updated ListboxPrinter

@Lars Brennicke
is there a way to set the print job name using this method ?
what constant would one have to change to set this ?

where did you get the syntax of this printSetting constant ?
If you read the printsetting string from the print dialog it’s all garbage

thanks.

[quote=373901:@Jean-Yves Pochez]@Lars Brennicke
is there a way to set the print job name using this method ?
what constant would one have to change to set this ?
[/quote]

This line in the method should affect the pdf file name.
pdfFile = GetSaveFolderItem("",“sample.pdf”)

If you need to save it as something specific, skip GetSaveFolderItem and set pdfFile folderitem with an absolute path.

thanks, but that’s not what I’m asking.
in a normal printing dialog, I want to set the job print name, so that if the user chooses “open the pdf in preview” or “send pdf with mail” then I can set the name of the pdf file. I think I can use the same trick by changing the xml records in the printersetupstring, but I don’t understand how to do it.