cocoa osx print pdf document

扶醉桌前 提交于 2019-12-24 03:32:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!