Saving as pdf on Mac

Hello,

The print dialog on Mac gives an option to save as .pdf

How could I save a document as .pdf programatically, (without going through the dialog), the name to be saved as would be provided to that command as a string.

Thanks.

Lennox

Mac with Carbon or Cocoa?
Do you use MBS Plugins?

I made a blog post for this recently:
http://www.mbsplugins.de/archive/2014-02-24/Print_to_PDF_File_on_Mac_OS_X_/monkeybreadsoftware_blog_xojo

Also we have cross platform DynaPDF Plugin for creating PDF in code.

You did not say which type of document it is, so i think you mean text documents

duplicate the saved document temporary and use a shell and cupsfilter

[code] dim myfile, tempfile, endfile as folderitem

dim myfilepath as string = // path to your saved document

myfile=GetFolderItem(myfilepath, FolderItem.PathTypeNative)
tempfile = SpecialFolder.Temporary.Child(“temp.rtf”)
endfile = SpecialFolder.Desktop.Child(“myfilename.pdf”) // your path to save PDF

dim s as string = "cupsfilter "+ tempfile.ShellPath _

  • " > " + endfile.ShellPath

dim mshell as new Shell

if myfile<>nil and myfile.Exists then
myfile.CopyFileTo tempfile // duplicate to Temp
else
MsgBox (“myfile not exists”)
end

if tempfile<>nil and tempfile.Exists then
mshell.Execute s
tempfile.Delete // delete temporary File
else
MsgBox (“tempfile not exists”)
end[/code]

Thanks Axel,

OK, it is a file that is being generated from my app, it contains plain text, formatted text and graphics.

Any suggestions for this?

What I had in mind was when print is called, the dialog box is presented and one of the choices in the PDF menu is “Saveas PDF”.
If that is now selected another window appears requesting a name and location for the output.

Mac already has that capability.

My question is that somehow in my print routine if I can 'Piggyback" that capability and indicate that I want a .PDF file with a name MyPDFGeneratedFile.Text and a location which I will specifywhich will have the appropriate name.
Maybe an applescript can do it.

Thanks.

Lennox

something like this

–Choose “PDF” > “Save as PDF”
click at position of first UI element of (first UI element of sheet 1 of window 1 whose buttons is not {}) whose UI elements is not {}
key code {125, 49}
delay 1
– Choose the desktop as save locaton (Command-D)
keystroke “d” using command down
–Save
keystroke “s” using command down

copy the Code to AppleScriptEditor
(change als PDF sichern … to the Version of your language) maybe Save As PDF…

Open a File in TextEdit and run the Code
(it saves a pdf to the desktop)

set SaveFolder to path to desktop folder as text tell application "TextEdit" to activate tell application "System Events" tell process "TextEdit" keystroke "p" using command down tell front window repeat until exists sheet 1 delay 0.02 end repeat tell sheet 1 click menu button "PDF" repeat until exists menu 1 of menu button "PDF" delay 0.02 end repeat click menu item "als PDF sichern …" of menu 1 of menu button "PDF" end tell end tell repeat until exists sheet 1 of sheet 1 of front window delay 0.2 end repeat end tell tell process "System Events" keystroke return end tell end tell

Thanks Axel,

My file has graphics and formatted text, I print the file directly from my app programatically, via a print method.

What I have found is this using CUPS 1.7.2 CUPS http://www.cups.org

If you have an application that generates output specifically for your
printer then you need to use the “-oraw” or “-l” options:

lp -o raw filename
lpr -l filename

This will prevent the filters from misinterpreting your print
file.

OK, so I use a print method from my app…

g = openprinter(MyAppprnPrinterSetup)

How can I use the above (lp -o raw filename or lpr -l filename) in my print method?

Thanks.

Lennox

You probably have to drive the entire printing session via CUPS at that point to pass it options

Thanks Norman,

Any suggestions on how to start that process?

Lennox

read the cups docs

Thanks,
Seems to be very much c-programming.
Lennox

[quote=78865:@Lennox Jacob]Thanks,
Seems to be very much c-programming.
Lennox[/quote]
You can use it from the command line if you want - a shell would suffice
But CUPS isn’t what generates the PDF on Mac OS X when you “Print to PDF”
Thats something completely different and without diving way deep into Core Graphics you won’t get this to print to pdf

OK Norman,
Thanks.
Lennox

Dave S has a class for that!
https://forum.xojo.com/6205-pdf-class/

I posted some code on these very forums that will enable you to save a Canvas as a PDF file…
https://forum.xojo.com/7378-app-design-help-needed/
you’ll need to scroll down.

Kennox, did you searched how to do that using, say, AppleScript ?

I recall a thread in the AppleScript list years ago, but I do not recall the solution.

OK Emile,

Axel Schneider post above works, but I have to do some fine tuning which I am in the process of doing now.

This is as far as I get, still working on it…

  1. I click print in my app and the print dialog with the PDF button appears
  2. I click Print Preview and the data to be printed is now displayed in Preview
  3. I run the script that Axel Schneider posted above, (changed TextEdit to Preview), since the document is now displayed in Preview and not TextEdit …
  4. the Applescript then “do its thing” and the pdf is saved with the name of the document.

Thanks Axel, I am still fine tuning it.

Lennox

make a menu handler for “Print” in your app with shortcut (command+P) and change “TextEdit” in the script to the name of your app.

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:

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

$archiver NSKeyedArchiver $objects $null $class CF$UID 8 NSAttributes CF$UID 2 $class CF$UID 7 NS.keys CF$UID 3 CF$UID 4 NS.objects CF$UID 5 CF$UID 6 NSJobDisposition NSSavePath NSPrintSaveJob PATHTOPDF $classes NSMutableDictionary NSDictionary NSObject $classname NSMutableDictionary $classes NSPrintInfo NSObject $classname NSPrintInfo $top root CF$UID 1 $version 100000 [/code]

Then use something like the following Xojo code to create your PDF:

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

It’s exactly the ‘piggyback’ solution you were looking for.

If you have the MBS plugins, then Christian’s method is prettier, of course.

Cool. Didn’t know you can include it in the SetupString. My code does similar, but with official methods.

a.NativePath should be pdfFile.NativePath, oops.