问题
In my cocoa application I obtain a pdf document from my webview. I need to print that pdf. I read that there is NSPrintOperation printOperationWithView method, but this method prints a view of the document. Is there anyway for printing directly from a file url? How can I achieve this correctly?
This is my code:
NSString *fileName = [NSString stringWithFormat:@"%@/mypdf.pdf", documentsDirectory];
NSData *pdfFinal = [[[[webView mainFrame] frameView] documentView] dataWithPDFInsideRect:[[[webView mainFrame] frameView] documentView].frame];
PDFDocument *doc = [[PDFDocument alloc] initWithData:pdfFinal];
[doc writeToFile:fileName];
Need help please. Thanks in advance.
回答1:
Read this answer: Printing without an NSView.
It explains how you can take a PDF file stored in an NSData structure and send it to a printer. So the only challenge left (other than cleaning up that code which it definitely not production quality) would be to get your PDF file into an NSData stream. That shouldn't be very difficult.
In fact, you could change part of the code and instead of using a data provider that gets information from an NSData stream, construct one that reads directly from file using:
CGDataProviderRef CGDataProviderCreateWithFilename ( const char *filename );
That should be even more straightforward if you already have the PDF file on disk somewhere.
来源:https://stackoverflow.com/questions/29750605/cocoa-osx-print-pdf-document