Does anybody have a example applescript studio project that will show me how to create a PDF of the contents of text fields in a NIB and also keeping their format? Im looking to do this when a button is clicked.
Thanks
Does anybody have a example applescript studio project that will show me how to create a PDF of the contents of text fields in a NIB and also keeping their format? Im looking to do this when a button is clicked.
Thanks
Actually i have changed the issue a bit.
I have a window “summary” that has a scroll view in it. I am looking to have a button “print” that will automatically save the window to a PDF without dialog.
Any idea’s?
Thank You
Hi,
as far as I know there is no way with AppleScript (Studio)
I guess the easiest way is with Cocoa using dataWithPDFInsideRect: of NSView
Can this be mixed into a current applescript studio project? I looked at the link you supplied me and I have no idea how to do it, but if I can use that in my current project it will be worth learning.
easiest version:
Create a Objective-C class in XCode with both -h and .m files, name it “SKPrintPDF”
Replace the contents of the SKPrintPDF.h with
[code]#import <Cocoa/Cocoa.h>
@interface SKPrintPDF : NSObject {}
@end[/code]
Replace the contents of the SKPrintPDF.m with
[code]#import “SKPrintPDF.h”
@implementation SKPrintPDF
@end[/code]
in the AppleScript part, connect the textView (not the scrollView !) to the awakeFromNib handler
property TextView : null
on awake from nib theObject
if name of theObject is "TextView" then
set TextView to theObject
else if name of theObject is "whatever" then
-- do other initialization
end if
end awake from nib
If the name of your textView is not “TextView”, change it the awakeFromNib handler
Then call the method with
call method "printPDFforTextView:" of class "SKPrintPDF" with parameter TextView
Dude, you are awesome. Thanks so much! that worked great. I do have 2 questions pertaining to that solution.
[data writeToFile:[@"~/Desktop/textTest.pdf" stringByExpandingTildeInPath] atomically:YES];
I want to pass a variable that has the name of the file that I want to create. Instead of always having a stacking name. My variable name is ReportName
Thanks again for all the help.
[code]#import <Cocoa/Cocoa.h>
@interface SKPrintPDF : NSObject {}
@end
#import “SKPrintPDF.h”
@implementation SKPrintPDF
@end[/code]
call method "printPDFforTextView:inPath:" of class "SKPrintPDF" with parameters {TextView, POSIXPath}
the path (POSIXpath) must be a POSIX path slash separated.
Only NSWindow and NSView (including subclasses like NSTextView or NSTableView) respond to the dataWithPDFInsideRect: method. You could use a custom view which contains all elements you want to be printed
This is pure genius! Thank you so much Stefan!
So does this mean I can export the data source of a table view into a PDF? So i would send it the contents of the data source in the order I want? I’ll test it and see if it works before bugging you too much with questions…
Regards,
Fred
Browser: Safari 531.9
Operating System: Mac OS X (10.6)
Fred,
As I am looking to do the same thing with a tableview, the solution you arrive at would be of great interest to me, please share.
Thanks,
Mark
Yes, the PDF will be created from the specified view
I’ll test this soon for sure. I have a 2000+ entries datasource and I would like to give the user the option to export the whole thing as a PDF. I am just worried about the layout, I’m guessing it will use the current layout of my table view to export.
Browser: Safari 531.9
Operating System: Mac OS X (10.6)
Of course, the PDF is like a “sophisticated” screenshot of the current view
Well, it worked like a charm!
Stefan, correct me if I am wrong, but I modified your code to send it an NSTableView instead of an NSTextView… since I wanted to export a table view. It made sense to me to do so without even testing your original code. Anyways…
2 questions Stefan: I successfully exported the table view content, but there was no headers, so I tried feeding it the scroll view containing the table view, and it worked but was limited to what I could see on screen. I also have looked at the documentation on dataWithPDFInsideRect: method, and I see there is no option to feed it but a rect. Do you think there would be a way to have the header & the table view content? maybe going negative in the top part of the bounds?
Second question is I see that with a lot of entries to export (~2000), the PDF is huge (24 wide by 200 inches high), and in Acrobat Pro, everything is invisible. Preview works fine, and I can search in the PDF for text. But would there be a way to divide the PDF in multiple pages? That was another reason why I have looked at the dataWithPDFInsideRect:'s documentation. Oddly, there is no problem in Acrobat with a smaller number of entries… Although I am not surprised, with each new version of Acrobat comes slowings, new bugs… it is way too complex I think, they need to clean it up!
As for the solution, here is the whole thing:
SKPrintPDF.h:
#import <Cocoa/Cocoa.h>
@interface SKPrintPDF : NSObject {}
+ (void)printPDFforTextView:(NSTextView *)textView inPath:(NSString *)path;
+ (void)printPDFforTableView:(NSTableView *)tableView inPath:(NSString *)path;
@end
SKPrintPDF.m:
#import "SKPrintPDF.h"
@implementation SKPrintPDF
+ (void)printPDFforTextView:(NSTextView *)textView inPath:(NSString *)path
{
NSData *data = [textView dataWithPDFInsideRect:[textView bounds]];
[data writeToFile:path atomically:YES];
}
+ (void)printPDFforTableView:(NSTableView *)tableView inPath:(NSString *)path
{
NSData *data = [tableView dataWithPDFInsideRect:[tableView bounds]];
[data writeToFile:path atomically:YES];
}
@end
and the Applescript studio code:
on exportDatabaseContentToPDF_textViewOrTableView_theViewToExport_thePOSIXPathToExportTo(textViewOrTableView, theViewToExport, thePOSIXPathToExportTo)
if textViewOrTableView is "tableview" then
call method "printPDFforTableView:inPath:" of class "SKPrintPDF" with parameters {theViewToExport, thePOSIXPathToExportTo}
else if textViewOrTableView is "textview" then
call method "printPDFforTextView:inPath:" of class "SKPrintPDF" with parameters {theViewToExport, thePOSIXPathToExportTo}
end if
end exportDatabaseContentToPDF_textViewOrTableView_theViewToExport_thePOSIXPathToExportTo
and here is how I call that routine:
my exportDatabaseContentToPDF_textViewOrTableView_theViewToExport_thePOSIXPathToExportTo("tableview", (table view "mainTV" of scroll view "mainSV" of window "mainWindow"), "/Users/username/Desktop/database.pdf")
As always, I am welcome to any improvements!
Fred
Browser: Safari 531.9
Operating System: Mac OS X (10.6)
Exactly, it’s sufficient to replace the NSTextView reference with the NSTableView reference.
My answers:
Alright, thank! I was not too much attached to the idea of having headers, but pages, oh yes! I will look into the link provided and get back to this post with any success I’ll have.
Thanks again Stefan!
Browser: Safari 531.9
Operating System: Mac OS X (10.6)
Finally got a solution that is familiar to the user and creates a multi-page PDF. And all that with no code at all!
For those interested, all happens in Interface Builder. First, I connected the Page Setup menu item of the File menu to Application instance in the MainMenu.xib (or whatever yours is called) and selecting runPageLayout: in the list. This will make the page setup window appear with no coding at all, and as far as I understand it, creates an NSPrintInfo object for your application.
Next I control-dragged the Print menu item from the File menu onto the Table View that you wish to be printed, selected the print: method from the list, and voilà ! You get the standard print dialog box familiar to all users, and you can also export to PDF, print or even fax. The only problem I found is that the table view layout is exported as is, which means that if your text is clipped or truncated in the cells of your table view, it will be too in the printout or PDF. Also, I couldn’t make the header print out with the table without it being limited only to the bounds you see on screen.
I’ll keep on pushing this thing further, could get a better result eventually.
Browser: Safari 531.9
Operating System: Mac OS X (10.6)
Well, got it working a little bit better. I kept on looking to improve the output of what I described in the previous post, and thanks to many other posts in the forums, i managed to get this helping:
on setupPrintInfoParameters()
set sharedPrintInfo to call method "sharedPrintInfo" of class "NSPrintInfo"
call method "setLeftMargin:" of sharedPrintInfo with parameters {18.0} -- margins are in points (72 = 1 inch)
call method "setRightMargin:" of sharedPrintInfo with parameters {18.0}
call method "setTopMargin:" of sharedPrintInfo with parameters {18.0}
call method "setBottomMargin:" of sharedPrintInfo with parameters {18.0}
--call method "setOrientation:" of sharedPrintInfo with parameters {1} -- 0 for portrait, 1 for landscape
call method "setHorizontalPagination:" of sharedPrintInfo with parameters {1} -- 1 = fit to page
call method "setVerticalPagination:" of sharedPrintInfo with parameters {0} -- 0 = auto paginate (2 = clip to page)
call method "setVerticallyCentered:" of sharedPrintInfo with parameters {1} -- 0 = false
call method "setHorizontalCentered:" of sharedPrintInfo with parameters {0} -- 1 = true
call method "setJobDisposition:" of sharedPrintInfo with parameters {"NSPrintSaveJob"}
end setupPrintInfoParameters
which is called at startup, before the window opens. Also, just to be on the safe side, I declared the sharedPrintInfo variable to be a global one.
The line where I set the setHorizontalPagination: to 1 (fit to page) has been the most helpful. If you play around with it you will see the difference.
Hope this helps someone else who would like to implement printing and PDF exporting in their app. Pretty simple actually, all you need is in this post and the previous one… good luck!
Browser: Safari 531.9
Operating System: Mac OS X (10.6)