gpt4 book ai didi

html - 为什么当我尝试将 WebView 保存为 PDF 时 NSPrintOperation 创建一个空 PDF?

转载 作者:太空宇宙 更新时间:2023-11-04 16:24:53 25 4
gpt4 key购买 nike

我正在尝试将 HTML 转换为 OS X 上的 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 吗?或者是其他地方的问题?

最佳答案

我知道这是一个旧话题,但我想发布我今天想出的解决方案。由于 WebView 是异步加载的,关键是设置一个框架加载委托(delegate)回调,然后通过运行循环运行,直到框架加载完成。

我下面的代码创建了一个新的 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];

接下来,我的框架加载委托(delegate)方法会在框架完全加载后设置一个标志,并调整框架的高度以匹配内容的高度:

-(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 应该很有效。

关于html - 为什么当我尝试将 WebView 保存为 PDF 时 NSPrintOperation 创建一个空 PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26230344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com