I am trying to embed iOS PDFKit in my Flutter App. Following is my code
public class PDFCustomView: NSObject, FlutterPlatformView {
let frame: CGRect
let viewId: Int64
init(_ frame: CGRect,viewId: Int64, args: Any?){
self.frame = frame
self.viewId = viewId
}
public func view() -> UIView {
let pdfView = PDFView(frame: frame)
if let path = Bundle.main.path(forResource: "studenthandbook2018", ofType: "pdf") {
let url = URL(fileURLWithPath: path)
if let pdfDocument = PDFDocument(url: url) {
pdfView.displayMode = .singlePageContinuous
pdfView.autoScales = true
pdfView.document = pdfDocument
}
}
return PDFView()
}
}
public class PdfViewFactory: NSObject, FlutterPlatformViewFactory {
public func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?)
-> FlutterPlatformView {
return PDFCustomView(frame,viewId: viewId,args: args)
}
}
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
let viewFactory = PdfViewFactory()
registrar(forPlugin: "iospdf").register(viewFactory, withId: "PDFIOSView")
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
I also included
<key>io.flutter.embedded_views_preview</key>
<true/>
in info.plist
Instead of returning PDFView
, I tried returning UISLider
and it works properly in my app but PDFkit gives error saying CoreGraphics PDF has logged an error. Set environment variabe "CG_PDF_VERBOSE" to learn more.
Is this a Flutter bug or am I doing something wrong on my part?
Step 1: Edit your scheme as shown
Step 2: Add a new environment variable as such:
Step 3: See what the error is showing on the XCode logs. In my case it was printing
missing or invalid CIDToGIDMap entry.
Quick search suggests that the pdf file that i am using might be missing some embedded fonts.
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