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
}