forked from veryweblog/ichm
-
Notifications
You must be signed in to change notification settings - Fork 26
/
CHMExporter.m
149 lines (109 loc) · 4.22 KB
/
CHMExporter.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// CHMExporter.m
// ichm
//
// Created by Robin Lu on 11/4/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "CHMExporter.h"
#import "CHMTableOfContents.h"
#import "CHMDocument.h"
#import <CHMKit/CHMKit.h>
#define MD_DEBUG 0
#if MD_DEBUG
#define MDLog(...) NSLog(__VA_ARGS__)
#else
#define MDLog(...)
#endif
@interface CHMExporter ()
- (void)exportNextPage;
@end
@implementation CHMExporter
@synthesize delegate;
- (id)initWithDocument:(CHMDocument *)document destinationURL:(NSURL *)destinationURL pageList:(NSArray *)list {
if ((self = [super init])) {
pageList = list;
cumulativeExportedPDFPageCount = 0;
currentPageListItemIndex = 0;
webView = [[WebView alloc] init];
[webView setPolicyDelegate:document];
[webView setFrameLoadDelegate:self];
[webView setResourceLoadDelegate:document];
NSPrintInfo *sharedInfo = [document printInfo];
NSMutableDictionary *printInfoDict = [NSMutableDictionary dictionaryWithDictionary:[sharedInfo dictionary]];
[printInfoDict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
NSString *tempDirPathTemplate = [NSTemporaryDirectory() stringByAppendingPathComponent:@"com.markdouma.iChm.export.XXXXXXXX"];
char *tempDirPath = mkdtemp((char *)[tempDirPathTemplate fileSystemRepresentation]);
if (tempDirPath) tempDirPathTemplate = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:tempDirPath length:strlen(tempDirPath)];
tempDirURL = [[NSURL fileURLWithPath:tempDirPathTemplate] retain];
tempFileURL = [[tempDirURL URLByAppendingPathComponent:@"ichm-export.pdf"] retain];
[printInfoDict setObject:tempFileURL forKey:NSPrintJobSavingURL];
printInfo = [[NSPrintInfo alloc] initWithDictionary:printInfoDict];
[printInfo setHorizontalPagination:NSAutoPagination];
[printInfo setVerticalPagination:NSAutoPagination];
[printInfo setVerticallyCentered:NO];
NSSize pageSize = [printInfo paperSize];
pageRect = CGRectMake(0, 0, pageSize.width, pageSize.height);
ctx = CGPDFContextCreateWithURL((CFURLRef)destinationURL, &pageRect, NULL);
}
return self;
}
- (void)dealloc {
delegate = nil;
CGPDFContextClose(ctx);
[printInfo release];
[webView release];
[tempDirURL release];
[tempFileURL release];
[super dealloc];
}
- (void)cleanup {
NSError *error = nil;
if (![[NSFileManager defaultManager] removeItemAtURL:tempDirURL error:&error]) {
NSLog(@"[%@ %@] *** ERROR: failed to remove item at \%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), tempDirURL.path);
}
}
- (void)beginExport {
[delegate exporterDidBeginExporting:self];
[self exportNextPage];
}
- (void)exportNextPage {
if (currentPageListItemIndex == pageList.count) {
[self cleanup];
[delegate exporterDidFinishExporting:self];
return;
}
CHMLinkItem *item = [pageList objectAtIndex:currentPageListItemIndex];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL chm__ITSSURLWithPath:item.path]];
[[webView mainFrame] loadRequest:request];
[delegate exporter:self didExportPage:cumulativeExportedPDFPageCount percentageComplete:100.0 * currentPageListItemIndex / pageList.count];
}
#pragma mark - <WebFrameLoadDelegate>
- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
currentPageListItemIndex++;
[self exportNextPage];
}
- (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
currentPageListItemIndex++;
[self exportNextPage];
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
NSView *docView = [[[webView mainFrame] frameView] documentView];
NSPrintOperation *op = [NSPrintOperation printOperationWithView:docView printInfo:printInfo];
[op setShowsPrintPanel:NO];
[op setShowsProgressPanel:NO];
[op runOperation];
CGPDFDocumentRef pdfDoc = CGPDFDocumentCreateWithURL((CFURLRef)tempFileURL);
size_t count = CGPDFDocumentGetNumberOfPages(pdfDoc);
for (size_t i = 0; i < count; ++i) {
CGPDFPageRef page = CGPDFDocumentGetPage(pdfDoc, i + 1);
CGContextBeginPage(ctx, &pageRect);
CGContextDrawPDFPage(ctx, page);
CGContextEndPage(ctx);
++cumulativeExportedPDFPageCount;
}
CGPDFDocumentRelease(pdfDoc);
currentPageListItemIndex++;
[self exportNextPage];
}
@end