Cocoa osx PDFView NSPrintOperation PrintPanel does not show page preview

In my Mac app, I have a webview that shows some html content. I am creating a PDFDocument from this web view and then I want to print this document. So I create a PDFView from the document, and then I call NSPrintOperation printOperationWithView. When the print panel is displayed, everything is displayed correctly, except for the preview of the page, which appears blank, but if I click the Details button, the panel will be updated and the page preview will be displayed correctly.

How can i solve this? Need help please. Thanks in advance.

This is an example of my problem:

1- A blank page view is displayed on the print panel. Then click on Details.

enter image description here

2- After updating the panel, the page preview is displayed correctly.

enter image description here

This is my code:

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];
+3
source share
2 answers

From this link:

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];

This works on OSX from 10.4 to 10.10 (Yosemite).

EDIT: You can also see this answer, similar, but with fewer lines of code.

+2
source

, PDFView NSView, -initWithFrame:, -init, PDFView *pdfView = [[PDFView alloc] init] PDFView , , NSView ( ), ( , NSZeroRect).

-initWithFrame: .

, , NSPrintOperation , -runOperation -runOperationModalForWindow:delegate:didRunSelector:contextInfo:, . , , - ( API, ...)? , . , Apple.

+1

Source: https://habr.com/ru/post/1737696/


All Articles