4

在我的 Mac 应用程序中,我有一个显示一些 html 内容的 web 视图。我从该 web 视图创建了一个 PDFDocument,然后我想打印该文档。所以我从文档中创建了一个 PDFView,然后我调用了 NSPrintOperation printOperationWithView。当显示打印面板时,除了显示为空白的页面预览之外,所有显示都是正确的,但是如果我按下详细信息按钮,面板会刷新并且页面预览会正确显示。

我该如何解决这个问题?请需要帮助。提前致谢。

这是我的问题的一个例子:

1- 打印面板显示空白页预览。接下来我按显示详细信息。

在此处输入图像描述

2-刷新面板后,页面预览正确显示。

在此处输入图像描述

这是我的代码:

NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
[printInfo setTopMargin:0.0];
[printInfo setBottomMargin:0.0];
[printInfo setLeftMargin:0.0];
[printInfo setRightMargin:0.0];
[printInfo setHorizontalPagination:NSFitPagination];
[printInfo setVerticalPagination:NSAutoPagination];
[printInfo setVerticallyCentered:NO];
[printInfo setHorizontallyCentered:YES];

NSData *pdfFinal = [[[[webView mainFrame] frameView] documentView] dataWithPDFInsideRect:[[[webView mainFrame] frameView] documentView].frame];

PDFDocument *doc = [[PDFDocument alloc] initWithData:pdfFinal];
PDFView *pdfView = [[PDFView alloc] init];
[pdfView setDocument:doc];

NSPrintOperation *op;
op = [NSPrintOperation printOperationWithView:pdfView.documentView printInfo:printInfo];

[op setShowsProgressPanel:YES];
[op setShowsPrintPanel:YES];
[op runOperation];
4

2 回答 2

2

这个链接:

PDFDocument *doc = ...;

// Invoke private method.
// NOTE: Use NSInvocation because one argument is a BOOL type. Alternately, you could declare the method in a category and just call it.
BOOL autoRotate = NO; // Set accordingly.
NSMethodSignature *signature = [PDFDocument instanceMethodSignatureForSelector:@selector(getPrintOperationForPrintInfo:autoRotate:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:@selector(getPrintOperationForPrintInfo:autoRotate:)];
[invocation setArgument:&printInfo atIndex:2];
[invocation setArgument:&autoRotate atIndex:3];
[invocation invokeWithTarget:doc];

// Grab the returned print operation.
void *result;
[invocation getReturnValue:&result];

NSPrintOperation *op = (__bridge NSPrintOperation *)result;
[op setShowsPrintPanel:YES];
[op setShowsProgressPanel:YES];
[op runOperation];

这适用于从 10.4 到 10.10 (Yosemite) 的 OSX。

编辑:您也可以看到类似答案,但代码行数更少。

于 2015-09-25T20:29:05.620 回答
1

猜测一下,因为PDFView是 的子类NSView,其指定的初始化程序是-initWithFrame:,不是-init,您的调用PDFView *pdfView = [[PDFView alloc] init]可能不允许 PDFView 设置其初始状态,尽管随后对其机器的调用可能会神奇地为您解决此状态,但NSView和子类当您不使用正确的指定初始化程序(这意味着它的框架和边界等于 )时,往往会表现得很奇怪(特别是在绘图方面NSZeroRect)。

尝试使用-initWithFrame:一些合理的非零矩形。

更新

好吧,只是在黑暗中的狂野射击,但文档NSPrintOperation-runOperation阻塞主线程并建议使用-runOperationModalForWindow:delegate:didRunSelector:contextInfo:以避免完全阻塞主线程。是否有可能通过阻塞主线程来阻止其他东西进行其初始工作(我称之为未记录的行为或 API 错误,但是......)?尝试实现模态版本,看看是否有帮助。如果没有,我实际上会向 Apple 提交错误报告。

于 2015-05-06T16:09:58.140 回答