【发布时间】:2014-10-07 07:03:48
【问题描述】:
我正在尝试在 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 吗?还是其他地方有问题?
【问题讨论】:
标签: html objective-c cocoa pdf webview