PDFKit unable to load PDF

I have implemented a PDFView in the app but it is unable to show the pdf downloaded from the web service. If I add a bundle resource in the project it loads it fine. But the one from the API is not loaded. I have added the code below to check further.

struct UploadQuotationView: View {
   
  let frId: String = "FR-DEMO-062021-00116"
  @State var downloadURL = URL(string: "https://www.apple.com")
//  @State var fileURL = Bundle.main.url(forResource: "c4611_sample_explain", withExtension: "pdf")
  @State var docSheet = false
  @State var pdfView = PDFKitRepresentedView(Data())
  @State var data = Data()

  var body: some View {
     
    
     
    VStack{
      Button("View PDF"){
         
        //downloadFile()
        downloadAndView(fileName: "Quotation\(frId)")
        //fileURL = Bundle.main.url(forResource: "fileSample", withExtension: "pdf")
         
      }
      .onAppear(){
//        downloadAndView(fileName: "Quotation\(frId)")
      }
      .padding()
      PDFKitRepresentedView(data)
        .cornerRadius(10)
        .padding()
        .shadow(radius: 10)
       
      Button("document picker"){
        docSheet.toggle()
      }
      .sheet(isPresented: $docSheet) {
        DocumentPickerView()
      }
      .padding()
    }
  }
   
   
   
  func downloadAndView(fileName:String) {
     
    guard let url = URL(string: "\(CommonStrings().apiURL)faultreport/quotation/\(frId)") else {return}
     
    var urlRequest = URLRequest(url: url)
    urlRequest.httpMethod = "GET"
    urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
    urlRequest.setValue(UserDefaults.standard.string(forKey: "token"), forHTTPHeaderField: "Authorization")
    urlRequest.setValue(UserDefaults.standard.string(forKey: "role"), forHTTPHeaderField: "role")
    urlRequest.setValue( UserDefaults.standard.string(forKey: "workspace"), forHTTPHeaderField: "workspace")
     
    print(urlRequest)
     
    let dataTask = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
      if let error = error {
        print("Request error: ", error)
        return
      }
       
      guard let response = response as? HTTPURLResponse else {
        print("response error: \(String(describing: error))")
        return
      }
       
      if response.statusCode == 200 {
         
        if let pdfData = data {
          print("success")
          //pdfView = PDFKitRepresentedView(pdfData)
          self.data = pdfData
        }
      }else{
        print("Error: \(response.statusCode). There was an error")
      }
    }
    dataTask.resume()
  }  
}
struct PDFKitRepresentedView: UIViewRepresentable {
   
  let pdfView = PDFView()
   
  let data: Data
  init(_ data: Data) {
    self.data = data
  }
   
  func makeUIView(context: UIViewRepresentableContext<PDFKitRepresentedView>) -> PDFKitRepresentedView.UIViewType {
    pdfView.document = PDFDocument(data: data)
    pdfView.displayMode = .singlePage
    pdfView.displayDirection = .horizontal
    return pdfView
  }
   
  func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PDFKitRepresentedView>) {
    pdfView.document = PDFDocument(data: data)
  }
   
}

There are some variables that you can modify as per your liking like the api URL and similar.

I have added the screenshot that shows the data is loaded in the "data" variable when the API request is success. Please check the screenshot.

Why did you comment out:              //pdfView = PDFKitRepresentedView(pdfData)

PDFKit unable to load PDF
 
 
Q