Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pdfkit ios 11 to open pdf file?

I am trying to open PDF file using new iOS 11 framework pdfkit.

But I am unable to do that.

So help me in opening the file using pdfkit framework.

Thank you in Advance.

like image 934
KAR Avatar asked Jul 28 '17 16:07

KAR


People also ask

How do I use PDFKit with Swift iOS projects?

In your Swift iOS project where you want to display a PDF, usually in a UIViewController subclass, import the PDFKit module at the top of the Swift file: This will make the PDFKit framework available for use in the Swift file.

What is core graphics PDFKit for iOS?

However, the Core Graphics framework for iOS provides basic PDF handling without any UI. Using the Core Graphics PDF API directly to create a PDF viewer requires a lot of work, so the introduction of PDFKit for iOS saves a large amount of time.

What is PDFKit?

Source Code PDFKit A JavaScript PDF generation library for Node and the browser. Description PDFKit is a PDF document generation library for Node and the browser that makes creating complex, multi-page, printable documents easy.

What is the best way to add a PDF viewer to iOS?

Apple's PDFKit framework is an easy way to add a basic PDF viewer, but if you need more robust functionality, like annotation creation and editing, real-time collaboration, form filling, text search, page manipulation, or others, you would have to implement those features yourself.


2 Answers

For new PDFKit,

you need to import PDFKit

Then create a view of PDFView and give a PDFDocument to view's document. Check the below code for reference.

let pdfView = PDFView(frame: self.view.bounds)
let url = Bundle.main.url(forResource: "pdfFile", withExtension: "pdf")
pdfView.document = PDFDocument(url: url!)
self.view.addSubview(pdfView)
like image 27
Parth Adroja Avatar answered Sep 22 '22 07:09

Parth Adroja


Here is Objective-C based example

  1. Ensure PDFKit framework is added to the project enter image description here

  2. Import the framework header #import <PDFKit/PDFKit.h>

  3. Create PDFDocument instance. In my case, it was using NSURL

    // fileURL is NSURL file path instance created using base64 pdf content PDFDocument *pdfDocument = [[PDFDocument alloc] initWithURL:fileURL];

  4. Create PDFView instance and assign the pdfDocument document to it

    PDFView *pdfView = [[PDFView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)]; pdfView.document = pdfDocument; pdfView.displayMode = kPDFDisplaySinglePageContinuous; pdfView.autoScales = true; pdfView.layer.borderWidth = 2; pdfView.layer.borderColor = [UIColor blueColor].CGColor;

As done by many others, we were using webview for showing and interacting with PDF content. Reading through the framework and trying some samples, PDFKit comes out as a thoughtful design where multitude of features are available out of the box and eases development.

For example, we had AirPrint functionality in the application which had this check for determining printability

if ([UIPrintInteractionController canPrintData: fileData]) {

After replacing webview implementation with PDFKit, I just had to use dataRepresentation method to get fileData and the rest just worked as is

NSData *fileData = [pdfDocument dataRepresentation];

Please check this WWDC page for additional details

like image 133
bharatmeda Avatar answered Sep 22 '22 07:09

bharatmeda