-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrintController.m
84 lines (69 loc) · 2.12 KB
/
PrintController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// PrintController.m
// Kineo
//
// Created by Ben Yellin on 3/25/11.
// Copyright 2011 Been Yelling. All rights reserved.
//
#import "PrintController.h"
#import "FlipSeries.h"
#import "PrintView.h"
#import "NSFileManager+TemporaryDirectory.h"
#import <Quartz/Quartz.h>
@interface PrintController (Private)
- (void)updatePDF;
- (NSString *)saveTemporaryPDF;
@end
@implementation PrintController
@synthesize flipSeries;
- (id)initWithFlipSeries:(FlipSeries *)series {
if (self = [super initWithWindowNibName:@"Print"]) {
flipSeries = series;
hiddenPrintView = [[PrintView alloc]
initWithFlipSeries:series];
[self.window setTitle:series.title];
[pdfView setAutoScales:YES];
[pdfView setDisplayMode:kPDFDisplaySinglePage];
[self updatePDF];
}
return self;
}
- (void)windowDidLoad {
[self.window setAspectRatio:NSMakeSize(1.0, 1.2941)]; // doesn't work
[hiddenPrintView setFlipSeries:flipSeries];
}
- (IBAction)openInPreview:(id)sender {
NSString *tempPath = [self saveTemporaryPDF];
[[NSWorkspace sharedWorkspace] openFile:tempPath];
}
- (NSString *)saveTemporaryPDF {
NSString *tempPath = [[NSFileManager defaultManager] createTemporaryFile];
[self saveToFile:tempPath];
return tempPath;
}
- (IBAction)save:(id)sender {
NSSavePanel *savePanel = [NSSavePanel savePanel];
int result;
[savePanel setRequiredFileType:@"pdf"];
result = [savePanel runModal];
if (result == NSOKButton) {
[self saveToFile:[savePanel filename]];
}
}
- (void)saveToFile:(NSString *)path {
[[pdfView document] writeToFile:path];
}
- (IBAction)print:(id)sender {
NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:pdfView];
NSPrintPanel *printPanel = [printOperation printPanel];
[printPanel setOptions:NSPrintPanelShowsCopies | NSPrintPanelShowsPaperSize |
NSPrintPanelShowsOrientation | NSPrintPanelShowsScaling | NSPrintPanelShowsPreview];
[printPanel runModal];
}
- (void)updatePDF {
NSData *pdfData = [hiddenPrintView dataWithPDFInsideRect:[hiddenPrintView frame]];
PDFDocument *document = [[PDFDocument alloc] initWithData:pdfData];
[pdfView setDocument:document];
[document release];
}
@end