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.
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.
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.
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.
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.
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)
Here is Objective-C based example
Ensure PDFKit framework is added to the project
Import the framework header
#import <PDFKit/PDFKit.h>
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];
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With