我试图在OS X上将HTML转换为PDF文件。经过大量挖掘后,我发现最好的方法是使用NSPrintOperation
。这是我用来将HTML字符串转换为PDF的代码:
WebView *webview = [[WebView alloc] init];
[webview.mainFrame loadHTMLString:htmlString baseURL:nil];
NSDictionary *printOpts = @{
NSPrintJobDisposition: NSPrintSaveJob,
NSPrintSavePath: outputFilePath
};
NSPrintInfo *printInfo = [[NSPrintInfo alloc] initWithDictionary:printOpts];
printInfo.horizontalPagination = NSAutoPagination;
printInfo.verticalPagination = NSAutoPagination;
NSPrintOperation *printOp = [NSPrintOperation
printOperationWithView:webview.mainFrame.frameView.documentView
printInfo:printInfo];
printOp.showsPrintPanel = NO;
printOp.showsProgressPanel = NO;
[printOp runOperation];
此代码 生成PDF文件,但PDF文件为空。是因为WebView
没有框架吗?我已尝试使用dataWithPDFInsideRect:
,但这也无效。
我应该使用A4纸张大小的框架初始化webview吗?或者是其他地方的问题?
答案 0 :(得分:3)
我知道这是一个老线程,但我想发布我今天提出的解决方案。由于WebViews异步加载,关键是设置一个帧加载委托回调,然后运行运行循环,直到帧完成加载。
下面的代码创建了一个全新的WebView,然后将HTML文件加载到其中:
// Clear the loaded flag
_webViewIsLoaded = NO;
// Fetch the default paper size and init print settings
NSPrintInfo * sharedInfo = [NSPrintInfo sharedPrintInfo];
NSMutableDictionary * sharedDict = [sharedInfo dictionary];
NSMutableDictionary * printInfoDict = [NSMutableDictionary dictionaryWithDictionary:sharedDict];
NSRect printRect = NSZeroRect;
printRect.size = [sharedInfo paperSize];
// Create a new web view
WebView * printView = [[WebView alloc] initWithFrame:printRect];
[printView setFrameLoadDelegate:self];
// Configure the WebView
WebPreferences * preferences = [[WebPreferences alloc] initWithIdentifier:@"com.id.here"];
[preferences setShouldPrintBackgrounds:YES];
[preferences setAllowsAnimatedImageLooping:NO];
[preferences setJavaScriptCanOpenWindowsAutomatically:NO];
[preferences setAutosaves:NO];
[preferences setJavaEnabled:NO];
[preferences setJavaScriptEnabled:NO];
[preferences setCacheModel:WebCacheModelDocumentViewer]; // most memory-efficient setting
[preferences setPlugInsEnabled:NO]; // disable plugins
[printView setPreferences:preferences];
// Load the HTML file
NSURL * url = [NSURL fileURLWithPath:htmlPath];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[[printView mainFrame] loadRequest:request];
[printInfoDict setObject:NSPrintSaveJob
forKey:NSPrintJobDisposition];
[printInfoDict setObject:saveUrl forKey:NSPrintJobSavingURL];
NSPrintInfo * printInfo = [[NSPrintInfo alloc] initWithDictionary:printInfoDict];
[printInfo setHorizontalPagination:NSFitPagination];
[printInfo setVerticalPagination:NSAutoPagination];
[printInfo setVerticallyCentered:NO];
[printInfo setHorizontallyCentered:YES];
// Wait for the frame to finish loading
while(!_webViewIsLoaded)
{
@autoreleasepool
{
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.005]];
}
}
NSPrintOperation * printOp = [NSPrintOperation printOperationWithView:[[[printView mainFrame] frameView] documentView]
printInfo:printInfo];
[printOp setShowsProgressPanel:YES];
[printOp setShowsPrintPanel:NO];
[printOp runOperation];
接下来,我的帧加载委托方法在帧完全加载后设置一个标志,并调整帧的高度以匹配内容的高度:
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)webFrame
{
_webViewIsLoaded = YES;
NSRect webFrameRect = [[[webFrame frameView] documentView] frame];
NSRect webViewRect = [webView frame];
// Resize the webview so it fits all of its contents
NSRect newWebViewRect = NSMakeRect(webViewRect.origin.x,
webViewRect.origin.y - (NSHeight(webFrameRect) - NSHeight(webViewRect)),
NSWidth(webViewRect),
NSHeight(webFrameRect));
[webView setFrame:newWebViewRect];
}
这适用于将HTML文档保存为PDF。
答案 1 :(得分:0)
试试这个:
NSPrintOperation *printOp = [NSPrintOperation
printOperationWithView:[[[webview mainFrame] frameView] documentView] printInfo:printInfo];
而不是:
NSPrintOperation *printOp = [NSPrintOperation printOperationWithView:webview.mainFrame.frameView.documentView
printInfo:printInfo];
这应该有效:)
修改强> 这是我使用的完整代码
NSString *htmlString = @"<html><head></head><body><h1>Hello, World</h1></body></html>";
WebView *webview = [[WebView alloc] init];
[webview.mainFrame loadHTMLString:htmlString baseURL:nil];
NSString* outputFilePath = @"/Users/YOUR-USERNAME/Documents/MyAwesomePDF.pdf";
NSURL *url = [[NSURL alloc] initWithString:outputFilePath];
NSDictionary *printOpts = @{
NSPrintJobDisposition: NSPrintSaveJob,
NSPrintSaveJob: url
};
NSPrintInfo *printInfo = [[NSPrintInfo alloc] initWithDictionary:printOpts];
printInfo.horizontalPagination = NSAutoPagination;
printInfo.verticalPagination = NSAutoPagination;
NSPrintOperation *printOp = [NSPrintOperation
printOperationWithView:[[[webview mainFrame] frameView] documentView] printInfo:printInfo];
printOp.showsPrintPanel = NO;
printOp.showsProgressPanel = NO;
[printOp runOperation];