2

I want to print a PDF file from WebView as there is a print button in WebView and when I click it, I'll get the URL of PDF and its content type as application/pdf. I searched so much and exhausted for how to print a PDF file in Objective C - cocoa. All I saw is to print content from a view and it'll print all the WebView

[NSPrintOperation printOperationWithView:webView]

But I need to print the PDF I got, not the entire WebView

Please help me...

NB Swift coders: Please don't put your code here. I don't know swift

Edit 1: No iOS codes please. Its about macOS

Edit 2: From the screenshot, when I click print, I get that PDF in the PDF viewer in the WebView. I need to print it directly. Thats my scenario.

Printing PDF that we get from web

soorej babu
  • 337
  • 2
  • 16
  • Do you want to print a pdf or do you want to download and save a file or do you want to print the downloaded file? Which data do you have and which step is the problem? – Willeke Jan 10 '19 at 10:32
  • I want to print a pdf – soorej babu Jan 10 '19 at 10:36
  • With which data? – Willeke Jan 10 '19 at 10:40
  • I have a url which contains a pdf file. I need to print it. Thats where I got stuck. – soorej babu Jan 10 '19 at 10:43
  • 1
    @soorejbabu, can you save the pdf locally or in memory? I understand the question is not really related to webview. So, if you can load the pdf from its URL in the WebView to a NSData object or PDFDocument, then you can print it easily using coca printing api. This is an example of how to print a PDFDocument from file URL. http://www.danandcheryl.com/2010/05/how-to-print-a-pdf-file-using-cocoa Let me know! – Hichem BOUSSETTA Jan 10 '19 at 11:16
  • Duplicate of [Printing without an NSView](https://stackoverflow.com/questions/4881635/printing-without-an-nsview) – Willeke Jan 10 '19 at 12:13
  • Duplicate of [cocoa osx print pdf document](https://stackoverflow.com/questions/29750605/cocoa-osx-print-pdf-document) – Willeke Jan 10 '19 at 12:14
  • @HichemBOUSSETTA I gone through the link and it worked. But it need to modify as there will be exception appear if we print it or cancel it. So slightly modified the code. Anyway it helped me a lot. – soorej babu Jan 14 '19 at 11:24
  • @soorejbabu that's great! – Hichem BOUSSETTA Jan 14 '19 at 12:48

1 Answers1

0

did you try this? It explains how to render PDF document from a UIWebView on iOS. If it works on iOS, my first guess would we that it should on macos.

Good luck! Hichem

Creating PDF file from UIWebView

Hichem BOUSSETTA
  • 1,627
  • 1
  • 17
  • 22
  • Oh no... This question is not about iOS/CocoaTouch. Its about macOS/Cocoa. At least look those tags please – soorej babu Jan 10 '19 at 10:25
  • And it's about rendering the entire WebView. – Willeke Jan 10 '19 at 10:27
  • 1
    After a small search I can see that iOS Print Rendering api does not exist on Mac, so the solution is not so obvious :/ You can take a look at this thread. It is related to the same subject and can give you more insight and sample code to generate the PDF on MAC. [https://stackoverflow.com/questions/38947782/how-to-create-a-pdf-in-swift-with-cocoa-mac] – Hichem BOUSSETTA Jan 10 '19 at 10:27