[SOLVED] How can I upload a PDF Firebase Storage?


This Content is from Stack Overflow. Question asked by user17864647

I’m currently trying to get a PDF from the current view, then set it as a variable, which I can then upload to firebase storage, and get back the link.

I’ve already accomplished producing the PDF with the code below:

  func exportToPDF() {

      let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
      let outputFileURL = documentDirectory.appendingPathComponent("SwiftUI.pdf")

      //Normal with
      let width: CGFloat = 8.5 * 72.0
      //Estimate the height of your view
      let height: CGFloat = 1000
      let charts = SmartSheet()

      let pdfVC = UIHostingController(rootView: charts)
      pdfVC.view.frame = CGRect(x: 0, y: 0, width: width, height: height)

      //Render the view behind all other views
      let rootVC = UIApplication.shared.windows.first?.rootViewController
      rootVC?.view.insertSubview(pdfVC.view, at: 0)

      //Render the PDF
    let pdfRenderer = UIGraphicsPDFRenderer(bounds: CGRect(x: 0, y: 0, width: 8.5 * 72.0, height: height))
    DispatchQueue.main.async {
        do {
            try pdfRenderer.writePDF(to: outputFileURL, withActions: { (context) in
                pdfVC.view.layer.render(in: context.cgContext)
            print("wrote file to: (outputFileURL.path)")
        } catch {
            print("Could not create PDF file: (error.localizedDescription)")


I just need to know how to upload it to firebase.

FYI – I’ve been able to upload UIImages using this code:

    func upload(image: UIImage) {
        // Create a storage reference
      let storageRef = Storage.storage().reference().child("squawks/(v.FirstName)-sqauwk.jpg")

        // Resize the image to 200px with a custom extension

        // Convert the image into JPEG and compress the quality to reduce its size
        let data = image.jpegData(compressionQuality: 0.2)

        // Change the content type to jpg. If you don't, it'll be saved as application/octet-stream type
        let metadata = StorageMetadata()
        metadata.contentType = "(v.FirstName)-sqauwk/jpg"

        // Upload the image
        if let data = data {
            storageRef.putData(data, metadata: metadata) { (metadata, error) in
                if let error = error {
                    print("Error while uploading file: ", error)

                if let metadata = metadata {
                    print("Metadata: ", metadata)

    func getPhotoURL(completion: @escaping (String) -> Void) {
      // Create a reference to the file you want to download
      let starsRef = Storage.storage().reference().child("squawks/(v.FirstName)-sqauwk.jpg")

      // Fetch the download URL
      starsRef.downloadURL { url, error in
        if let error = error {
          // Handle any errors
          print("error getting link: (error)")
        } else {
          // Get the download URL for 'images/stars.jpg'

//On click of button:
            var strURL = ""
            upload(image: inputImage!) //inputImage is a UIImage
            DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
              getPhotoURL() { url in
                strURL = url
                print("STORAGE URL: (strURL)")



Change upload to

func upload(_ pdf: URL) {
   let data  = try! Data(contentsOf:pdf)
   let storageRef = Storage.storage().reference().child("squawks/\(pdf.lastPathComponent)")
   // .... go on

This Question was asked in StackOverflow by user17864647 and Answered by Sh_Khan It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?