Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreGraphics PDF has logged an error. Set environment variabe "CG_PDF_VERBOSE" to learn more

Tags:

ios

flutter

dart

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?

like image 648
Pritish Avatar asked Dec 17 '22 18:12

Pritish


1 Answers

Step 1: Edit your scheme as shown enter image description here

Step 2: Add a new environment variable as such: enter image description here

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.

like image 82
deniz Avatar answered May 25 '23 19:05

deniz