0

I have a capacitor ionic project where i'm creating a custom capacitor plugin to preview PDF . Base64 is logging correctly (tried opening using base64 in browser and pdf opens fine) but the PDF doesn't preview in simulator . Any help will be highly appreciated.

Link to my file : https://github.com/Richa-Singh-1/capacitor-pdf-viewer/blob/main/ios/Plugin/PdfViewer.swift

P.S : I'm quite naive for Swift .

import Foundation
import PDFKit
@objc public class PdfViewer: UIViewController, PDFViewDelegate {
    var pdfView: PDFView = PDFView()
    public override func viewDidLoad() {
        super.viewDidLoad()
        pdfView = PDFView()
        self.view.addSubview(pdfView)
    }

    @objc public func loadPDF(url: String) -> Void {
        guard let pdfURL = URL(string: url) else {
            print("Error: cannot create URL")
            return
        }
        var pdfURLRequest = URLRequest(url: pdfURL)
        pdfURLRequest.httpMethod = "GET"
        let session = URLSession.shared
        let task = session.dataTask(with: pdfURLRequest){
            (data, response, error)in
            guard let data = data else {
                return
            }
            do {
                print("base64 ------> ", data.base64EncodedString())
                // Approach 1
                // Load using PDFKit with data
                DispatchQueue.main.async {
                    //                pdfView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor).isActive = true
                    //                pdfView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor).isActive = true
                    //                pdfView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
                    //                pdfView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor).isActive = true
                    self.pdfView.translatesAutoresizingMaskIntoConstraints = false
                    self.pdfView.autoScales = true
                    self.pdfView.displayMode = .singlePageContinuous
                    self.pdfView.displayDirection = .vertical
                    let doc = PDFDocument(data: data)
                    self.pdfView.document = doc
                }
richa Singh
  • 435
  • 2
  • 5
  • 10
  • you can use `PDFjs` to view your document. no need o write a plugin for that. There are other document view plugin also available. – Najam Us Saqib Aug 24 '21 at 06:48
  • I don't want to download the pdf and pdfjs doesn't show control options in simulator. i want to just preview file with data (file stream/data or temp url). – richa Singh Aug 24 '21 at 07:25

1 Answers1

0

You could simply implement PDF preview with QLPreviewController and QLPreviewControllerDataSource. Also you could change some behaviour using delegate.

Note: If you have only 1 element you don't need to use index parameter at all in previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem method

import UIKit
import Quicklook


class YourViewController: UIViewController {
    // better to inject or use some service
    private let previewViewController = QLPreviewController()

    override func viewDidLoad() {
        super.viewDidLoad()
        previewViewController.dataSource = self // Setting the dataSource and delegate if needed
    }

    override viewDidAppear() {
        super.viewDidAppear()
        present(previewViewController.dataSource, animated: true)
    }
}

extension YourViewController: QLPreviewControllerDataSource {
    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1 // number of PDF items
    }

     func previewController(_ controller: QLPreviewController, 
                        previewItemAt index: Int) -> QLPreviewItem {
        guard let url = Bundle.main.url(forResource: String(index), withExtension: "pdf") else {
            fatalError("Could not load \(index).pdf")
        } // Your URL

        return url as QLPreviewItem
    }
}

WARNING : There is an issue related to QLPreviewController in case of pushing in navigation stack so it will be better to present it. Check relevant answer Quicklook/QLPreviewController shows a blank page instead of pdf on ios 8 but works fine on iOS7

Stanislav Marynych
  • 188
  • 1
  • 1
  • 14