-
Notifications
You must be signed in to change notification settings - Fork 94
/
CHMExporter.m
102 lines (90 loc) · 2.93 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
//
// CHMExporter.m
// ichm
//
// Created by Robin Lu on 11/4/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import <WebKit/WebKit.h>
#import "CHMExporter.h"
#import "CHMTableOfContent.h"
#import "CHMDocument.h"
@implementation CHMExporter
- (id)initWithCHMDocument:(CHMDocument*)doc toFileName:(NSString*)filename WithPageList:(NSArray*)list
{
document = doc;
[document retain];
pageList = list;
pageCount = 0;
curPageId = 0;
webView = [[WebView alloc] init];
[webView setPolicyDelegate:document];
[webView setFrameLoadDelegate:self];
[webView setResourceLoadDelegate:document];
CFURLRef fileURL = CFURLCreateWithFileSystemPath(NULL,
(CFStringRef) filename, kCFURLPOSIXPathStyle, false);
NSPrintInfo * sharedInfo = [document printInfo];
NSMutableDictionary *printInfoDict = [NSMutableDictionary dictionaryWithDictionary: [sharedInfo dictionary]];
[printInfoDict setObject:NSPrintSaveJob
forKey:NSPrintJobDisposition];
tmpFileName = [NSString stringWithFormat:@"%@/ichm-export.pdf", NSTemporaryDirectory()];
[tmpFileName retain];
[printInfoDict setObject:tmpFileName forKey:NSPrintSavePath];
printInfo = [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
[printInfo setHorizontalPagination: NSAutoPagination];
[printInfo setVerticalPagination: NSAutoPagination];
[printInfo setVerticallyCentered:NO];
NSSize pageSize = [printInfo paperSize];
pageRecct = CGRectMake(0 , 0, pageSize.width , pageSize.height);
ctx = CGPDFContextCreateWithURL(fileURL, &pageRecct, NULL);
CFRelease(fileURL);
[self retain];
return self;
}
- (void)export
{
if (curPageId == [pageList count])
{
[self release];
[document endExportProgressSheet:nil];
return;
}
LinkItem *page = [pageList objectAtIndex:curPageId];
NSURL *url = [document composeURL:[page path]];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[[webView mainFrame] loadRequest:req];
double rate = 100.0 * curPageId / [pageList count];
[document exportedProgressRate:rate PageCount:pageCount];
}
- (void)dealloc
{
CGPDFContextClose(ctx);
[tmpFileName release];
[printInfo release];
[webView release];
[document release];
[super dealloc];
}
#pragma mark WebFrameLoadDelegate
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
NSView *docView = [[[webView mainFrame] frameView] documentView];
NSPrintOperation *op = [NSPrintOperation printOperationWithView:docView
printInfo:printInfo];
[op setShowPanels:NO];
[op runOperation];
NSURL *url = [NSURL fileURLWithPath:tmpFileName];
CGPDFDocumentRef pdfDoc = CGPDFDocumentCreateWithURL((CFURLRef)url);
size_t count = CGPDFDocumentGetNumberOfPages(pdfDoc);
for (size_t i = 0; i< count ;++i) {
CGPDFPageRef page = CGPDFDocumentGetPage(pdfDoc, i+1);
CGContextBeginPage(ctx, &pageRecct);
CGContextDrawPDFPage(ctx, page);
CGContextEndPage(ctx);
++pageCount;
}
CGPDFDocumentRelease(pdfDoc);
curPageId += 1;
[self export];
}
@end